From 1648f40dcb7b501add6ee81b9732a76bbd6684f2 Mon Sep 17 00:00:00 2001 From: Sarah Marsh Date: Fri, 3 Sep 2021 21:20:18 +0000 Subject: [PATCH] Create a test case for mutated attributes --- tests/Fixtures/Book.php | 10 ++++ tests/Fixtures/Publication.php | 56 +++++++++++++++++++ tests/Fixtures/Publisher.php | 21 +++++++ ...gleTableInheritanceMutatedPropertyTest.php | 46 +++++++++++++++ tests/TestCase.php | 3 + ..._09_03_210504_create_publication_table.php | 33 +++++++++++ ...1_09_03_210504_create_publishers_table.php | 31 ++++++++++ 7 files changed, 200 insertions(+) create mode 100644 tests/Fixtures/Book.php create mode 100644 tests/Fixtures/Publication.php create mode 100644 tests/Fixtures/Publisher.php create mode 100644 tests/SingleTableInheritanceMutatedPropertyTest.php create mode 100644 tests/migrations/2021_09_03_210504_create_publication_table.php create mode 100644 tests/migrations/2021_09_03_210504_create_publishers_table.php diff --git a/tests/Fixtures/Book.php b/tests/Fixtures/Book.php new file mode 100644 index 0000000..6789b23 --- /dev/null +++ b/tests/Fixtures/Book.php @@ -0,0 +1,10 @@ + '', + 'name' => '' + ]; + protected $casts = [ + 'name' => 'string', + 'publisher_id' => 'string', + ]; + + public function publisher() + { + return $this->belongsTo( + Publisher::class, + 'publisher_id', + 'id' + ); + } + + public function setPublisherIdAttribute($value) + { + $this->attributes['publisher_id'] = $value . "test"; + } + +} diff --git a/tests/Fixtures/Publisher.php b/tests/Fixtures/Publisher.php new file mode 100644 index 0000000..7152ca3 --- /dev/null +++ b/tests/Fixtures/Publisher.php @@ -0,0 +1,21 @@ + 'MyPublishingHouse']; + $publisher = new Publisher($publisher_attr); + $publisher->save(); + + $publication_attr = ['name' => 'MyBook', 'publisher_id' => $publisher->id, 'type' => 'book']; + $book = new Book($publication_attr); + $book->save(); + $publisher_id = $book->getAttributeValue('publisher_id'); + + $expected = $publisher->id . "test"; + + $this->assertEquals($expected, $publisher_id); + + } + + public function testMutatedPropertyFromBuilder() { + $publisher_attr = ['name' => 'MyPublishingHouse']; + $publisher = new Publisher($publisher_attr); + $publisher->save(); + + $publication_attr = ['name' => 'MyBook', 'publisher_id' => $publisher->id, 'type' => 'book']; + $parent = new Publication; + $book = $parent->newFromBuilder($publication_attr); + $book->save(); + $publisher_id = $book->getAttributeValue('publisher_id'); + $expected = $publisher->id . "test"; + + + $this->assertEquals($expected, $publisher_id); + + } + +} diff --git a/tests/TestCase.php b/tests/TestCase.php index dc84759..98f7328 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,6 +28,9 @@ public function setUp(): void { 'Nanigans\SingleTableInheritance\Tests\Fixtures\MotorVehicle', 'Nanigans\SingleTableInheritance\Tests\Fixtures\Car', 'Nanigans\SingleTableInheritance\Tests\Fixtures\Truck', + 'Nanigans\SingleTableInheritance\Tests\Fixtures\Publication', + 'Nanigans\SingleTableInheritance\Tests\Fixtures\Publisher', + 'Nanigans\SingleTableInheritance\Tests\Fixtures\Book', 'Nanigans\SingleTableInheritance\Tests\Fixtures\Bike' ]; // Reset each model event listeners. diff --git a/tests/migrations/2021_09_03_210504_create_publication_table.php b/tests/migrations/2021_09_03_210504_create_publication_table.php new file mode 100644 index 0000000..83b05ed --- /dev/null +++ b/tests/migrations/2021_09_03_210504_create_publication_table.php @@ -0,0 +1,33 @@ +increments('id'); + $table->string('name'); + $table->string('publisher_id'); + $table->string('type'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('publications'); + } + +} diff --git a/tests/migrations/2021_09_03_210504_create_publishers_table.php b/tests/migrations/2021_09_03_210504_create_publishers_table.php new file mode 100644 index 0000000..d2205fd --- /dev/null +++ b/tests/migrations/2021_09_03_210504_create_publishers_table.php @@ -0,0 +1,31 @@ +increments('id'); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('publishers'); + } + +}