-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a test case for mutated attributes
- Loading branch information
1 parent
5fd1dd8
commit be26203
Showing
8 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?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 UsesUuid; | ||
use HasEvents; | ||
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) | ||
{ | ||
if (! ctype_xdigit($value) || strlen($value) % 2 !== 0) { | ||
throw new MutateException(__METHOD__.' expects the value to be serialized to be a hexadecimal string.'); | ||
} | ||
$this->attributes['publisher_id'] = hex2bin($value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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 | ||
{ | ||
use UsesUuid; | ||
use HasEvents; | ||
protected $table = "publishers"; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'name' | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Nanigans\SingleTableInheritance\Tests\Fixtures; | ||
use Illuminate\Support\Str; | ||
|
||
trait UsesUuid | ||
{ | ||
protected static function bootUsesUuid() | ||
{ | ||
static::creating(function ($model) { | ||
$uuid = sprintf( '%04x%04x%04x%04x%04x%04x%04x%04x', | ||
// 32 bits for "time_low" | ||
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), | ||
|
||
// 16 bits for "time_mid" | ||
mt_rand( 0, 0xffff ), | ||
|
||
// 16 bits for "time_hi_and_version", | ||
// four most significant bits :wholds version number 4 | ||
mt_rand( 0, 0x0fff ) | 0x4000, | ||
|
||
// 16 bits, 8 bits for "clk_seq_hi_res", | ||
// 8 bits for "clk_seq_low", | ||
// two most significant bits holds zero and one for variant DCE1.1 | ||
mt_rand( 0, 0x3fff ) | 0x8000, | ||
|
||
// 48 bits for "node" | ||
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) | ||
); | ||
if (! $model->getKey()) { | ||
$model->{$model->getKeyName()} = (string) $uuid; | ||
} | ||
}); | ||
} | ||
|
||
public function fireEvent(string $eventName) | ||
{ | ||
if (!\in_array($eventName, $this->getObservableEvents())) { | ||
throw new \InvalidArgumentException( | ||
"Only model events can be fired this way. '$eventName' is not a valid model event." | ||
); | ||
} | ||
|
||
return $this->fireModelEvent($eventName); | ||
} | ||
|
||
public function getIncrementing() | ||
{ | ||
return false; | ||
} | ||
|
||
public function getKeyType() | ||
{ | ||
return 'string'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?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->fireEvent('creating'); | ||
$publisher->save(); | ||
|
||
$publication_attr = ['name' => 'MyBook', 'publisher_id' => $publisher->id, 'type' => 'book']; | ||
$parent = new Publication; | ||
//$book = $parent->newFromBuilder($publication_attr); | ||
$book = new Book($publication_attr); | ||
$book->fireEvent('creating'); | ||
$book->save(); | ||
$publisher_id = $book->getAttributeValue('publisher_id'); | ||
|
||
$serialized_id = hex2bin($publisher->id); | ||
|
||
$this->assertEquals($serialized_id, $publisher_id); | ||
|
||
} | ||
|
||
public function testMutatedPropertyFromBuilder() { | ||
$publisher_attr = ['name' => 'MyPublishingHouse']; | ||
$publisher = new Publisher($publisher_attr); | ||
$publisher->fireEvent('creating'); | ||
$publisher->save(); | ||
|
||
$publication_attr = ['name' => 'MyBook', 'publisher_id' => $publisher->id, 'type' => 'book']; | ||
$parent = new Publication; | ||
$book = $parent->newFromBuilder($publication_attr); | ||
$book->fireEvent('creating'); | ||
$book->save(); | ||
$publisher_id = $book->getAttributeValue('publisher_id'); | ||
|
||
$serialized_id = hex2bin($publisher->id); | ||
|
||
$this->assertEquals($serialized_id, $publisher_id); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/migrations/2021_09_03_210504_create_publication_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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->uuid('id')->primary(); | ||
$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
31
tests/migrations/2021_09_03_210504_create_publishers_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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->uuid('id')->primary(); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop('publishers'); | ||
} | ||
|
||
} |