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 diff --git a/composer.json b/composer.json index 53589eb..0804ab5 100644 --- a/composer.json +++ b/composer.json @@ -20,16 +20,29 @@ } ], "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", + "pestphp/pest": "2.x-dev", + "pestphp/pest-plugin-laravel": "2.x-dev" }, "autoload": { "psr-4": { "Milwad\\LaravelAttributes\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "Milwad\\LaravelAttributes\\Tests\\": "tests/" + } + }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } }, "extra": { "laravel": { 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/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/src/Traits/Attributable.php b/src/Traits/Attributable.php index 91e27ab..5ba4959 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); @@ -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 new file mode 100644 index 0000000..df3cb9b --- /dev/null +++ b/tests/AttributeTest.php @@ -0,0 +1,41 @@ +create(['title' => 'milwad-dev']); + $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); +}); + +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()); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..6326c03 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,31 @@ +in(__DIR__); + +/* +|-------------------------------------------------------------------------- +| 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); +}); diff --git a/tests/SetUp/Models/Product.php b/tests/SetUp/Models/Product.php new file mode 100644 index 0000000..0c006da --- /dev/null +++ b/tests/SetUp/Models/Product.php @@ -0,0 +1,14 @@ +id(); + $table->string('title'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('products'); + } +}; diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..bea3c86 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,42 @@ +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'); + } +}