From e6f7f670d49416e8763f928ced5f63dd42072edf Mon Sep 17 00:00:00 2001 From: Grant Date: Sun, 1 Nov 2020 09:50:40 -0500 Subject: [PATCH 1/4] Added test suite --- composer.json | 3 +- phpunit.xml.dist | 50 ++++++------- tests/ArchivableTest.php | 72 +++++++++++++++++++ .../Factories/ArchivableModelFactory.php | 25 +++++++ .../Factories/RegularModelFactory.php | 16 +++++ tests/TestCase.php | 61 ++++++++++++++++ tests/TestClasses/ArchivableModel.php | 16 +++++ tests/TestClasses/RegularModel.php | 13 ++++ 8 files changed, 228 insertions(+), 28 deletions(-) create mode 100644 tests/ArchivableTest.php create mode 100644 tests/Database/Factories/ArchivableModelFactory.php create mode 100644 tests/Database/Factories/RegularModelFactory.php create mode 100644 tests/TestCase.php create mode 100644 tests/TestClasses/ArchivableModel.php create mode 100644 tests/TestClasses/RegularModel.php 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..7e9341b --- /dev/null +++ b/tests/ArchivableTest.php @@ -0,0 +1,72 @@ +create(); + + $this->assertNull($model->fresh()->archived_at); + + $model->archive(); + + $this->assertNotNull($model->fresh()->archived_at); + } + + /** @test */ + 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 */ + 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 */ + function all_models_can_be_found_with_the_withArchived_scope() + { + ArchivableModel::factory()->archived()->create(); + ArchivableModel::factory()->create(); + + $this->assertCount(2, ArchivableModel::withArchived()->get()); + } + + /** @test */ + 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 */ + 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..ff8dc31 --- /dev/null +++ b/tests/TestClasses/ArchivableModel.php @@ -0,0 +1,16 @@ + Date: Sun, 1 Nov 2020 10:11:46 -0500 Subject: [PATCH 2/4] fixed styles --- tests/ArchivableTest.php | 12 ++++++------ tests/TestCase.php | 4 +--- tests/TestClasses/ArchivableModel.php | 3 --- tests/TestClasses/RegularModel.php | 2 -- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/tests/ArchivableTest.php b/tests/ArchivableTest.php index 7e9341b..2561768 100644 --- a/tests/ArchivableTest.php +++ b/tests/ArchivableTest.php @@ -9,7 +9,7 @@ class ArchivableTest extends TestCase { /** @test */ - function a_model_can_be_archived() + public function a_model_can_be_archived() { $model = ArchivableModel::factory()->create(); @@ -21,7 +21,7 @@ function a_model_can_be_archived() } /** @test */ - function a_model_can_be_unarchived() + public function a_model_can_be_unarchived() { $model = ArchivableModel::factory()->archived()->create(); @@ -33,7 +33,7 @@ function a_model_can_be_unarchived() } /** @test */ - function a_model_cannot_be_queried_normally_when_archived() + public function a_model_cannot_be_queried_normally_when_archived() { ArchivableModel::factory()->archived()->create(); @@ -45,7 +45,7 @@ function a_model_cannot_be_queried_normally_when_archived() } /** @test */ - function all_models_can_be_found_with_the_withArchived_scope() + public function all_models_can_be_found_with_the_withArchived_scope() { ArchivableModel::factory()->archived()->create(); ArchivableModel::factory()->create(); @@ -54,7 +54,7 @@ function all_models_can_be_found_with_the_withArchived_scope() } /** @test */ - function only_archived_models_can_be_found_with_the_onlyArchived_scope() + public function only_archived_models_can_be_found_with_the_onlyArchived_scope() { ArchivableModel::factory()->archived()->create(); ArchivableModel::factory()->create(); @@ -63,7 +63,7 @@ function only_archived_models_can_be_found_with_the_onlyArchived_scope() } /** @test */ - function models_without_the_archivable_trait_are_not_scoped() + public function models_without_the_archivable_trait_are_not_scoped() { RegularModel::factory()->create(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 1cffd6b..98d13d5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,6 +1,5 @@ Date: Sun, 1 Nov 2020 10:12:51 -0500 Subject: [PATCH 3/4] fixed readme --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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. From 6fcafb8d1fdaaa459aa4366915947ca50fac7848 Mon Sep 17 00:00:00 2001 From: Grant Date: Sun, 1 Nov 2020 10:14:01 -0500 Subject: [PATCH 4/4] fixed more styles --- tests/ArchivableTest.php | 1 - tests/TestCase.php | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/ArchivableTest.php b/tests/ArchivableTest.php index 2561768..b8b7ed8 100644 --- a/tests/ArchivableTest.php +++ b/tests/ArchivableTest.php @@ -1,6 +1,5 @@