Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a test case for mutated attributes #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/Fixtures/Book.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace Nanigans\SingleTableInheritance\Tests\Fixtures;


class Book extends Publication
{
protected static $singleTableType = 'book';
}
56 changes: 56 additions & 0 deletions tests/Fixtures/Publication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Nanigans\SingleTableInheritance\Tests\Fixtures;

use Illuminate\Database\Eloquent\Concerns\HasEvents;
use Nanigans\SingleTableInheritance\Tests\Fixtures\UsesUuid;
use Nanigans\SingleTableInheritance\SingleTableInheritanceTrait;
use Illuminate\Database\Eloquent\Model as Eloquent;

/**
* @property string name
* @property Publisher publisher
*/
class Publication extends Eloquent
{
use SingleTableInheritanceTrait;
protected static $singleTableTypeField = 'type';

protected $table = "publications";

protected static $singleTableSubclasses = [
'Nanigans\SingleTableInheritance\Tests\Fixtures\Book',
];

/**
* @var array
*/
protected $fillable = [
'publisher_id',
'type',
'name'
];

protected $attributes = [
'publisher_id' => '',
'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";
}

}
21 changes: 21 additions & 0 deletions tests/Fixtures/Publisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Nanigans\SingleTableInheritance\Tests\Fixtures;

use Illuminate\Database\Eloquent\Concerns\HasEvents;
use Illuminate\Database\Eloquent\Model as Eloquent;
/**
* @property string name
*/
class Publisher extends Eloquent
{
protected $table = "publishers";

/**
* @var array
*/
protected $fillable = [
'name'
];

}
46 changes: 46 additions & 0 deletions tests/SingleTableInheritanceMutatedPropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php


namespace Nanigans\SingleTableInheritance\Tests;

use Nanigans\SingleTableInheritance\Tests\Fixtures\Book;
use Nanigans\SingleTableInheritance\Tests\Fixtures\Publication;
use Nanigans\SingleTableInheritance\Tests\Fixtures\Publisher;

class SingleTableInheritanceMutatedPropertyTest extends TestCase
{

public function testMutatedPropertyDirect() {
$publisher_attr = ['name' => '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);

}

}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions tests/migrations/2021_09_03_210504_create_publication_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;

class CreatePublicationTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('publications', function ($table){
$table->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');
}

}
31 changes: 31 additions & 0 deletions tests/migrations/2021_09_03_210504_create_publishers_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;

class CreatePublishersTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('publishers', function ($table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('publishers');
}

}