Skip to content

Commit 1648f40

Browse files
committed
Create a test case for mutated attributes
1 parent 5fd1dd8 commit 1648f40

7 files changed

+200
-0
lines changed

tests/Fixtures/Book.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace Nanigans\SingleTableInheritance\Tests\Fixtures;
5+
6+
7+
class Book extends Publication
8+
{
9+
protected static $singleTableType = 'book';
10+
}

tests/Fixtures/Publication.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace Nanigans\SingleTableInheritance\Tests\Fixtures;
3+
4+
use Illuminate\Database\Eloquent\Concerns\HasEvents;
5+
use Nanigans\SingleTableInheritance\Tests\Fixtures\UsesUuid;
6+
use Nanigans\SingleTableInheritance\SingleTableInheritanceTrait;
7+
use Illuminate\Database\Eloquent\Model as Eloquent;
8+
9+
/**
10+
* @property string name
11+
* @property Publisher publisher
12+
*/
13+
class Publication extends Eloquent
14+
{
15+
use SingleTableInheritanceTrait;
16+
protected static $singleTableTypeField = 'type';
17+
18+
protected $table = "publications";
19+
20+
protected static $singleTableSubclasses = [
21+
'Nanigans\SingleTableInheritance\Tests\Fixtures\Book',
22+
];
23+
24+
/**
25+
* @var array
26+
*/
27+
protected $fillable = [
28+
'publisher_id',
29+
'type',
30+
'name'
31+
];
32+
33+
protected $attributes = [
34+
'publisher_id' => '',
35+
'name' => ''
36+
];
37+
protected $casts = [
38+
'name' => 'string',
39+
'publisher_id' => 'string',
40+
];
41+
42+
public function publisher()
43+
{
44+
return $this->belongsTo(
45+
Publisher::class,
46+
'publisher_id',
47+
'id'
48+
);
49+
}
50+
51+
public function setPublisherIdAttribute($value)
52+
{
53+
$this->attributes['publisher_id'] = $value . "test";
54+
}
55+
56+
}

tests/Fixtures/Publisher.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Nanigans\SingleTableInheritance\Tests\Fixtures;
4+
5+
use Illuminate\Database\Eloquent\Concerns\HasEvents;
6+
use Illuminate\Database\Eloquent\Model as Eloquent;
7+
/**
8+
* @property string name
9+
*/
10+
class Publisher extends Eloquent
11+
{
12+
protected $table = "publishers";
13+
14+
/**
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'name'
19+
];
20+
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
4+
namespace Nanigans\SingleTableInheritance\Tests;
5+
6+
use Nanigans\SingleTableInheritance\Tests\Fixtures\Book;
7+
use Nanigans\SingleTableInheritance\Tests\Fixtures\Publication;
8+
use Nanigans\SingleTableInheritance\Tests\Fixtures\Publisher;
9+
10+
class SingleTableInheritanceMutatedPropertyTest extends TestCase
11+
{
12+
13+
public function testMutatedPropertyDirect() {
14+
$publisher_attr = ['name' => 'MyPublishingHouse'];
15+
$publisher = new Publisher($publisher_attr);
16+
$publisher->save();
17+
18+
$publication_attr = ['name' => 'MyBook', 'publisher_id' => $publisher->id, 'type' => 'book'];
19+
$book = new Book($publication_attr);
20+
$book->save();
21+
$publisher_id = $book->getAttributeValue('publisher_id');
22+
23+
$expected = $publisher->id . "test";
24+
25+
$this->assertEquals($expected, $publisher_id);
26+
27+
}
28+
29+
public function testMutatedPropertyFromBuilder() {
30+
$publisher_attr = ['name' => 'MyPublishingHouse'];
31+
$publisher = new Publisher($publisher_attr);
32+
$publisher->save();
33+
34+
$publication_attr = ['name' => 'MyBook', 'publisher_id' => $publisher->id, 'type' => 'book'];
35+
$parent = new Publication;
36+
$book = $parent->newFromBuilder($publication_attr);
37+
$book->save();
38+
$publisher_id = $book->getAttributeValue('publisher_id');
39+
$expected = $publisher->id . "test";
40+
41+
42+
$this->assertEquals($expected, $publisher_id);
43+
44+
}
45+
46+
}

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function setUp(): void {
2828
'Nanigans\SingleTableInheritance\Tests\Fixtures\MotorVehicle',
2929
'Nanigans\SingleTableInheritance\Tests\Fixtures\Car',
3030
'Nanigans\SingleTableInheritance\Tests\Fixtures\Truck',
31+
'Nanigans\SingleTableInheritance\Tests\Fixtures\Publication',
32+
'Nanigans\SingleTableInheritance\Tests\Fixtures\Publisher',
33+
'Nanigans\SingleTableInheritance\Tests\Fixtures\Book',
3134
'Nanigans\SingleTableInheritance\Tests\Fixtures\Bike'
3235
];
3336
// Reset each model event listeners.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
5+
class CreatePublicationTable extends Migration {
6+
7+
/**
8+
* Run the migrations.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
Schema::create('publications', function ($table){
15+
$table->increments('id');
16+
$table->string('name');
17+
$table->string('publisher_id');
18+
$table->string('type');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::drop('publications');
31+
}
32+
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
5+
class CreatePublishersTable extends Migration {
6+
7+
/**
8+
* Run the migrations.
9+
*
10+
* @return void
11+
*/
12+
public function up()
13+
{
14+
Schema::create('publishers', function ($table){
15+
$table->increments('id');
16+
$table->string('name');
17+
$table->timestamps();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::drop('publishers');
29+
}
30+
31+
}

0 commit comments

Comments
 (0)