Skip to content

Commit

Permalink
Merge pull request #2 from K03413R/add_test_suite
Browse files Browse the repository at this point in the history
Added test suite
  • Loading branch information
joelbutcher authored Nov 7, 2020
2 parents 40eb501 + 6fcafb8 commit e9daba7
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 30 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ By default, the global scope of this trait uses the `withoutArchived` extension

### Testing

Currently, a test suite doesn't exist for this package. However, I will be writing tests in line with Laravel's testing schemes for traits. If you wish to contribute any unit tests of your own, please refer to the [contribution guides](#-contributing) below

```composer test```
### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"require-dev": {
"illuminate/pagination": "^8.0",
"orchestra/testbench": "^6.3",
"phpunit/phpunit": "^8.4|^9.0"
},
"autoload": {
Expand All @@ -35,7 +36,7 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test": "vendor/bin/phpunit --colors=always",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

},
Expand Down
50 changes: 23 additions & 27 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage/>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
71 changes: 71 additions & 0 deletions tests/ArchivableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace LaravelArchivable\Tests;

use LaravelArchivable\Tests\TestClasses\ArchivableModel;
use LaravelArchivable\Tests\TestClasses\RegularModel;

class ArchivableTest extends TestCase
{
/** @test */
public function a_model_can_be_archived()
{
$model = ArchivableModel::factory()->create();

$this->assertNull($model->fresh()->archived_at);

$model->archive();

$this->assertNotNull($model->fresh()->archived_at);
}

/** @test */
public function a_model_can_be_unarchived()
{
$model = ArchivableModel::factory()->archived()->create();

$this->assertNotNull($model->fresh()->archived_at);

$model->unarchive();

$this->assertNull($model->fresh()->archived_at);
}

/** @test */
public function a_model_cannot_be_queried_normally_when_archived()
{
ArchivableModel::factory()->archived()->create();

ArchivableModel::factory()->create();

$this->assertDatabaseCount('archivable_models', 2);

$this->assertCount(1, ArchivableModel::all());
}

/** @test */
public function all_models_can_be_found_with_the_withArchived_scope()
{
ArchivableModel::factory()->archived()->create();
ArchivableModel::factory()->create();

$this->assertCount(2, ArchivableModel::withArchived()->get());
}

/** @test */
public function only_archived_models_can_be_found_with_the_onlyArchived_scope()
{
ArchivableModel::factory()->archived()->create();
ArchivableModel::factory()->create();

$this->assertCount(1, ArchivableModel::onlyArchived()->get());
}

/** @test */
public function models_without_the_archivable_trait_are_not_scoped()
{
RegularModel::factory()->create();

$this->assertCount(1, RegularModel::all());
}
}
25 changes: 25 additions & 0 deletions tests/Database/Factories/ArchivableModelFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace LaravelArchivable\Tests\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use LaravelArchivable\Tests\TestClasses\ArchivableModel;

class ArchivableModelFactory extends Factory
{
protected $model = ArchivableModel::class;

public function archived()
{
return $this->state(function (array $attributes) {
return [
'archived_at' => now(),
];
});
}

public function definition()
{
return [];
}
}
16 changes: 16 additions & 0 deletions tests/Database/Factories/RegularModelFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace LaravelArchivable\Tests\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use LaravelArchivable\Tests\TestClasses\RegularModel;

class RegularModelFactory extends Factory
{
protected $model = RegularModel::class;

public function definition()
{
return [];
}
}
58 changes: 58 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace LaravelArchivable\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use LaravelArchivable\LaravelArchivableServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
public function setUp(): void
{
parent::setUp();

Factory::guessFactoryNamesUsing(
function (string $modelName) {
return 'LaravelArchivable\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory';
}
);
}

/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

Schema::create('archivable_models', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->timestamp('archived_at', 0)->nullable();
});

Schema::create('regular_models', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}

protected function getPackageProviders($app)
{
return [
LaravelArchivableServiceProvider::class,
];
}
}
13 changes: 13 additions & 0 deletions tests/TestClasses/ArchivableModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace LaravelArchivable\Tests\TestClasses;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use LaravelArchivable\Archivable;

class ArchivableModel extends Model
{
use Archivable;
use HasFactory;
}
11 changes: 11 additions & 0 deletions tests/TestClasses/RegularModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace LaravelArchivable\Tests\TestClasses;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class RegularModel extends Model
{
use HasFactory;
}

0 comments on commit e9daba7

Please sign in to comment.