diff --git a/README.md b/README.md index 14acd71..9ab6c67 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 958964d..643ca38 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ }, "require-dev": { "illuminate/pagination": "^8.0", + "orchestra/testbench": "^6.3", "phpunit/phpunit": "^8.4|^9.0" }, "autoload": { @@ -35,7 +36,7 @@ } }, "scripts": { - "test": "vendor/bin/phpunit", + "test": "vendor/bin/phpunit --colors=always", "test-coverage": "vendor/bin/phpunit --coverage-html coverage" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 22fe879..b58754e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,25 @@ - - - - tests - - - - - src/ - - - - - - - - - + + + + + tests + + + + + diff --git a/tests/ArchivableTest.php b/tests/ArchivableTest.php new file mode 100644 index 0000000..b8b7ed8 --- /dev/null +++ b/tests/ArchivableTest.php @@ -0,0 +1,71 @@ +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()); + } +} diff --git a/tests/Database/Factories/ArchivableModelFactory.php b/tests/Database/Factories/ArchivableModelFactory.php new file mode 100644 index 0000000..dcc9dd7 --- /dev/null +++ b/tests/Database/Factories/ArchivableModelFactory.php @@ -0,0 +1,25 @@ +state(function (array $attributes) { + return [ + 'archived_at' => now(), + ]; + }); + } + + public function definition() + { + return []; + } +} diff --git a/tests/Database/Factories/RegularModelFactory.php b/tests/Database/Factories/RegularModelFactory.php new file mode 100644 index 0000000..1a79adc --- /dev/null +++ b/tests/Database/Factories/RegularModelFactory.php @@ -0,0 +1,16 @@ +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, + ]; + } +} diff --git a/tests/TestClasses/ArchivableModel.php b/tests/TestClasses/ArchivableModel.php new file mode 100644 index 0000000..e3154cc --- /dev/null +++ b/tests/TestClasses/ArchivableModel.php @@ -0,0 +1,13 @@ +