From 2bfc1d7ffb88957b141174ae9544c604271d93ec Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 2 Apr 2023 13:29:23 +0000 Subject: [PATCH 01/15] Apply fixes from StyleCI --- migrations/2022_08_04_155610_create_attributes_table.php | 3 +-- src/Traits/Attributable.php | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/migrations/2022_08_04_155610_create_attributes_table.php b/migrations/2022_08_04_155610_create_attributes_table.php index 382a953..afefc96 100644 --- a/migrations/2022_08_04_155610_create_attributes_table.php +++ b/migrations/2022_08_04_155610_create_attributes_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class() extends Migration { /** * Run the migrations. * diff --git a/src/Traits/Attributable.php b/src/Traits/Attributable.php index 91e27ab..e10c92e 100644 --- a/src/Traits/Attributable.php +++ b/src/Traits/Attributable.php @@ -13,7 +13,7 @@ trait Attributable use HasRelationships; /** - * Get attributes + * Get attributes. * * @return MorphMany */ @@ -30,10 +30,10 @@ public function attributes() public function attachAttribute(string $title, string $value) { $attributes = [ - 'title' => $title, - 'value' => $value, + 'title' => $title, + 'value' => $value, 'attributable_id' => $this->getKey(), - 'attributable' => get_class($this), + 'attributable' => get_class($this), ]; return Attribute::query()->create($attributes); From 9789b6cd8a04a95022ab95e2f468cb1c3a3fb4ba Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Sun, 2 Apr 2023 17:05:30 +0330 Subject: [PATCH 02/15] install PEST --- composer.json | 8 +++++++- phpunit.xml | 17 +++++++++++++++++ tests/Pest.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 10 ++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 phpunit.xml create mode 100644 tests/Pest.php create mode 100644 tests/TestCase.php diff --git a/composer.json b/composer.json index 53589eb..dd12c55 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,10 @@ } }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } }, "extra": { "laravel": { @@ -42,5 +45,8 @@ "dev-main" : "0.x-dev" } } + }, + "require-dev": { + "pestphp/pest": "2.x-dev" } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..e57bdd1 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,17 @@ + + + + + ./tests + + + + + ./src + + + diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..5949c61 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,45 @@ +in('Feature'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..cfb05b6 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,10 @@ + Date: Sun, 2 Apr 2023 17:06:23 +0330 Subject: [PATCH 03/15] Create AttributeTest.php --- tests/AttributeTest.php | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/AttributeTest.php diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php new file mode 100644 index 0000000..a4abe2d --- /dev/null +++ b/tests/AttributeTest.php @@ -0,0 +1,2 @@ + Date: Sun, 2 Apr 2023 17:13:58 +0330 Subject: [PATCH 04/15] install pest-plugin-laravel --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dd12c55..001deb8 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,7 @@ } }, "require-dev": { - "pestphp/pest": "2.x-dev" + "pestphp/pest": "2.x-dev", + "pestphp/pest-plugin-laravel": "2.x-dev" } } From 95dd0c358cac73852c24da994c9241b7d48a77c8 Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Sun, 2 Apr 2023 17:38:07 +0330 Subject: [PATCH 05/15] change position of `require-dev` in composer --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 001deb8..d42f96f 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,10 @@ "php": "^7.3|7.4.*|8.0.*|8.1.*|8.2.*", "laravel/framework": "7.*|8.*|9.*|10.*" }, + "require-dev": { + "pestphp/pest": "2.x-dev", + "pestphp/pest-plugin-laravel": "2.x-dev" + }, "autoload": { "psr-4": { "Milwad\\LaravelAttributes\\": "src/" @@ -45,9 +49,5 @@ "dev-main" : "0.x-dev" } } - }, - "require-dev": { - "pestphp/pest": "2.x-dev", - "pestphp/pest-plugin-laravel": "2.x-dev" } } From 0b3b91de0817a0fea2ff2f2c77d300801041b714 Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Sun, 2 Apr 2023 17:40:25 +0330 Subject: [PATCH 06/15] add `autoload-dev` --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index d42f96f..41a7245 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,11 @@ "Milwad\\LaravelAttributes\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Milwad\\LaravelAttributes\\Tests\\": "tests/" + } + }, "config": { "sort-packages": true, "allow-plugins": { From 8e980b903c0080858ddcc9154206a9ccd40b99e6 Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Sun, 2 Apr 2023 17:53:29 +0330 Subject: [PATCH 07/15] add Product model and products migration --- ...023_04_02_141649_create_products_table.php | 32 +++++++++++++++++++ tests/SetUp/models/Product.php | 11 +++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/SetUp/migrations/2023_04_02_141649_create_products_table.php create mode 100644 tests/SetUp/models/Product.php diff --git a/tests/SetUp/migrations/2023_04_02_141649_create_products_table.php b/tests/SetUp/migrations/2023_04_02_141649_create_products_table.php new file mode 100644 index 0000000..dec6f10 --- /dev/null +++ b/tests/SetUp/migrations/2023_04_02_141649_create_products_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('title'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('products'); + } +}; diff --git a/tests/SetUp/models/Product.php b/tests/SetUp/models/Product.php new file mode 100644 index 0000000..5aaa60e --- /dev/null +++ b/tests/SetUp/models/Product.php @@ -0,0 +1,11 @@ + Date: Sun, 2 Apr 2023 17:59:20 +0330 Subject: [PATCH 08/15] install `orchestra/testbench` package with fix TestCase --- composer.json | 1 + tests/TestCase.php | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 41a7245..971b33d 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "laravel/framework": "7.*|8.*|9.*|10.*" }, "require-dev": { + "orchestra/testbench": "dev-develop", "pestphp/pest": "2.x-dev", "pestphp/pest-plugin-laravel": "2.x-dev" }, diff --git a/tests/TestCase.php b/tests/TestCase.php index cfb05b6..1746d42 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,40 @@ namespace Tests; -use PHPUnit\Framework\TestCase as BaseTestCase; +use Milwad\LaravelAttributes\LaravelAttributesServiceProvider; -abstract class TestCase extends BaseTestCase +abstract class TestCase extends \Orchestra\Testbench\TestCase { - // + /** + * Load package service provider. + * + * @param \Illuminate\Foundation\Application $app + * @return array + */ + protected function getPackageProviders($app) + { + return [LaravelAttributesServiceProvider::class]; + } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + */ + protected function getEnvironmentSetUp($app) + { + // Setup default database to use sqlite :memory: + $app['config']->set('database.default', 'testing'); + $app['config']->set('database.connections.testing', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } + + protected function setUp(): void + { + parent::setUp(); + $this->loadMigrationsFrom(__DIR__ . '/SetUp/migrations'); + } } From c09f7c68e7347effdc92a9c3da4e4ac497b3f80f Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Tue, 4 Apr 2023 17:12:05 +0330 Subject: [PATCH 09/15] fix namespace for `tests` directory and active TestCase in PEST --- tests/AttributeTest.php | 15 +++++++++++++++ tests/Pest.php | 20 +++----------------- tests/SetUp/{models => Models}/Product.php | 5 ++++- tests/TestCase.php | 2 +- 4 files changed, 23 insertions(+), 19 deletions(-) rename tests/SetUp/{models => Models}/Product.php (54%) diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php index a4abe2d..94785c7 100644 --- a/tests/AttributeTest.php +++ b/tests/AttributeTest.php @@ -1,2 +1,17 @@ create(['title' => 'milwad-dev']); + $product->attachAttribute('name', 'reza'); + + assertDatabaseCount('products', 1); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 5949c61..6326c03 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,7 @@ in('Feature'); +uses(TestCase::class)->in(__DIR__); /* |-------------------------------------------------------------------------- @@ -27,19 +29,3 @@ expect()->extend('toBeOne', function () { return $this->toBe(1); }); - -/* -|-------------------------------------------------------------------------- -| Functions -|-------------------------------------------------------------------------- -| -| While Pest is very powerful out-of-the-box, you may have some testing code specific to your -| project that you don't want to repeat in every file. Here you can also expose helpers as -| global functions to help you to reduce the number of lines of code in your test files. -| -*/ - -function something() -{ - // .. -} diff --git a/tests/SetUp/models/Product.php b/tests/SetUp/Models/Product.php similarity index 54% rename from tests/SetUp/models/Product.php rename to tests/SetUp/Models/Product.php index 5aaa60e..03d17a5 100644 --- a/tests/SetUp/models/Product.php +++ b/tests/SetUp/Models/Product.php @@ -1,11 +1,14 @@ Date: Tue, 4 Apr 2023 17:12:41 +0330 Subject: [PATCH 10/15] fix description for test --- tests/AttributeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php index 94785c7..f2d3b81 100644 --- a/tests/AttributeTest.php +++ b/tests/AttributeTest.php @@ -9,7 +9,7 @@ uses(RefreshDatabase::class); -test('test can attach attributes to product', function () { +test('test can attach attributes to model', function () { $product = Product::query()->create(['title' => 'milwad-dev']); $product->attachAttribute('name', 'reza'); From cd1e28817f421246c21c6c0bed31ef34fff87234 Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Tue, 4 Apr 2023 17:19:53 +0330 Subject: [PATCH 11/15] add `test can attach multiple attributes to model` with fix attachAttributes function --- src/Traits/Attributable.php | 11 +++++++++-- tests/AttributeTest.php | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Traits/Attributable.php b/src/Traits/Attributable.php index e10c92e..5ba4959 100644 --- a/src/Traits/Attributable.php +++ b/src/Traits/Attributable.php @@ -42,11 +42,18 @@ public function attachAttribute(string $title, string $value) /** * Attach multiple attributes. * - * @return bool + * @return $this */ public function attachAttributes(array $values) { - return Attribute::query()->insert($values); + foreach ($values as $value) { + $value['attributable_id'] = $this->getKey(); + $value['attributable'] = get_class($this); + + Attribute::query()->create($value); + } + + return $this; } /** diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php index f2d3b81..ba9b229 100644 --- a/tests/AttributeTest.php +++ b/tests/AttributeTest.php @@ -11,7 +11,23 @@ test('test can attach attributes to model', function () { $product = Product::query()->create(['title' => 'milwad-dev']); - $product->attachAttribute('name', 'reza'); + $product->attachAttribute('name', 'implicit value'); + + assertDatabaseCount('products', 1); +}); + +test('test can attach multiple attributes to model', function () { + $product = Product::query()->create(['title' => 'milwad-dev']); + $product->attachAttributes([ + [ + 'title' => 'milwad', + 'value' => 'developer', + ], + [ + 'title' => 'framework', + 'value' => 'laravel', + ] + ]); assertDatabaseCount('products', 1); }); From 9bfebee3c4d70dd121f334545ee37eab39aafd57 Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Tue, 4 Apr 2023 17:24:04 +0330 Subject: [PATCH 12/15] add `test attributes can retrieve in model relation` --- tests/AttributeTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php index ba9b229..cd306c2 100644 --- a/tests/AttributeTest.php +++ b/tests/AttributeTest.php @@ -6,6 +6,7 @@ use Milwad\LaravelAttributes\Tests\SetUp\Models\Product; use function Pest\Laravel\assertDatabaseCount; +use function PHPUnit\Framework\assertEmpty; uses(RefreshDatabase::class); @@ -31,3 +32,10 @@ assertDatabaseCount('products', 1); }); + +test('test attributes can retrieve in model relation', function () { + Product::query()->create(['title' => 'milwad-dev']); + $product = Product::query()->with('attributes')->first(); + + assertEmpty($product->attributes()->get()); +}); From b6f0373a064de6a83ca755a329ec1f7a788c5178 Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Tue, 4 Apr 2023 17:27:37 +0330 Subject: [PATCH 13/15] add `coverage` into gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4c9b1f6..5c27eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.lock /vendor /.idea .phpunit.result.cache +/coverage \ No newline at end of file From 37f7ca5dbcfe09c67f26b0ade38cfe5006aca22f Mon Sep 17 00:00:00 2001 From: miladev-ent Date: Tue, 4 Apr 2023 17:28:33 +0330 Subject: [PATCH 14/15] fix `php` and `framework` version in composer --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 971b33d..0804ab5 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ } ], "require": { - "php": "^7.3|7.4.*|8.0.*|8.1.*|8.2.*", - "laravel/framework": "7.*|8.*|9.*|10.*" + "php": "^8.0", + "laravel/framework": "9.*|10.*" }, "require-dev": { "orchestra/testbench": "dev-develop", From 520e0f99c7616ecdee7327c9604733b6021e83b3 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 4 Apr 2023 13:59:07 +0000 Subject: [PATCH 15/15] Apply fixes from StyleCI --- tests/AttributeTest.php | 2 +- tests/SetUp/Models/Product.php | 2 +- .../2023_04_02_141649_create_products_table.php | 3 +-- tests/TestCase.php | 11 ++++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/AttributeTest.php b/tests/AttributeTest.php index cd306c2..df3cb9b 100644 --- a/tests/AttributeTest.php +++ b/tests/AttributeTest.php @@ -27,7 +27,7 @@ [ 'title' => 'framework', 'value' => 'laravel', - ] + ], ]); assertDatabaseCount('products', 1); diff --git a/tests/SetUp/Models/Product.php b/tests/SetUp/Models/Product.php index 03d17a5..0c006da 100644 --- a/tests/SetUp/Models/Product.php +++ b/tests/SetUp/Models/Product.php @@ -11,4 +11,4 @@ class Product extends Model protected $table = 'products'; protected $fillable = ['title']; -} \ No newline at end of file +} diff --git a/tests/SetUp/migrations/2023_04_02_141649_create_products_table.php b/tests/SetUp/migrations/2023_04_02_141649_create_products_table.php index dec6f10..bdb04b7 100644 --- a/tests/SetUp/migrations/2023_04_02_141649_create_products_table.php +++ b/tests/SetUp/migrations/2023_04_02_141649_create_products_table.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class() extends Migration { /** * Run the migrations. * diff --git a/tests/TestCase.php b/tests/TestCase.php index 561aa48..bea3c86 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,7 +9,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase /** * Load package service provider. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app + * * @return array */ protected function getPackageProviders($app) @@ -20,22 +21,22 @@ protected function getPackageProviders($app) /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app */ protected function getEnvironmentSetUp($app) { // Setup default database to use sqlite :memory: $app['config']->set('database.default', 'testing'); $app['config']->set('database.connections.testing', [ - 'driver' => 'sqlite', + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => '', ]); } protected function setUp(): void { parent::setUp(); - $this->loadMigrationsFrom(__DIR__ . '/SetUp/migrations'); + $this->loadMigrationsFrom(__DIR__.'/SetUp/migrations'); } }