Skip to content

Commit

Permalink
Create a test case for mutated attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahmarshy committed Sep 3, 2021
1 parent 5fd1dd8 commit be26203
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 0 deletions.
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';
}
61 changes: 61 additions & 0 deletions tests/Fixtures/Publication.php
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);
}

}
23 changes: 23 additions & 0 deletions tests/Fixtures/Publisher.php
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'
];

}
56 changes: 56 additions & 0 deletions tests/Fixtures/UsesUuid.php
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';
}
}
52 changes: 52 additions & 0 deletions tests/SingleTableInheritanceMutatedPropertyTest.php
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);

}

}
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->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 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->uuid('id')->primary();
$table->string('name');
$table->timestamps();
});
}

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

}

0 comments on commit be26203

Please sign in to comment.