Skip to content

Commit

Permalink
Merge pull request #20 from mauro-moreno/master
Browse files Browse the repository at this point in the history
Added missing tests for addVisible, addHidden and casts
  • Loading branch information
jenssegers committed Jul 21, 2018
2 parents be4f291 + 2aae156 commit eedaeed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,9 @@ protected function hasCast($key)
*/
protected function isJsonCastable($key)
{
$castables = ['array', 'json', 'object', 'collection'];
return $this->hasCast($key) &&
in_array($this->getCastType($key), ['array', 'json', 'object'], true);
in_array($this->getCastType($key), $castables, true);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public function testToArray()
$this->assertEquals('foo', $array['name']);
$this->assertFalse(isset($array['password']));
$this->assertEquals($array, $model->jsonSerialize());

$model->addHidden(['name']);
$model->addVisible('password');
$array = $model->toArray();
$this->assertTrue(is_array($array));
$this->assertFalse(isset($array['name']));
$this->assertTrue(isset($array['password']));
}

public function testToJson()
Expand Down Expand Up @@ -159,21 +166,37 @@ public function testCasts()
$model = new ModelStub;
$model->score = '0.34';
$model->data = ['foo' => 'bar'];
$model->count = 1;
$model->object_data = ['foo' => 'bar'];
$model->active = 'true';
$model->default = 'bar';
$model->collection_data = [['foo' => 'bar', 'baz' => 'bat']];

$this->assertTrue(is_float($model->score));
$this->assertTrue(is_array($model->data));
$this->assertTrue(is_bool($model->active));
$this->assertTrue(is_int($model->count));
$this->assertEquals('bar', $model->default);
$this->assertInstanceOf('\stdClass', $model->object_data);
$this->assertInstanceOf('\Illuminate\Support\Collection', $model->collection_data);

$attributes = $model->getAttributes();
$this->assertTrue(is_string($attributes['score']));
$this->assertTrue(is_string($attributes['data']));
$this->assertTrue(is_string($attributes['active']));
$this->assertTrue(is_int($attributes['count']));
$this->assertTrue(is_string($attributes['default']));
$this->assertTrue(is_string($attributes['object_data']));
$this->assertTrue(is_string($attributes['collection_data']));

$array = $model->toArray();
$this->assertTrue(is_float($array['score']));
$this->assertTrue(is_array($array['data']));
$this->assertTrue(is_bool($array['active']));
$this->assertTrue(is_int($array['count']));
$this->assertEquals('bar', $array['default']);
$this->assertInstanceOf('\stdClass', $array['object_data']);
$this->assertInstanceOf('\Illuminate\Support\Collection', $array['collection_data']);
}

public function testGuarded()
Expand All @@ -195,6 +218,20 @@ public function testGuarded()
ModelStub::reguard();
}

public function testGuardedCallback()
{
ModelStub::unguard();
$mock = $this->getMockBuilder('stdClass')
->setMethods(['callback'])
->getMock();
$mock->expects($this->once())
->method('callback')
->will($this->returnValue('foo'));
$string = ModelStub::unguarded([$mock, 'callback']);
$this->assertEquals('foo', $string);
ModelStub::reguard();
}

public function testTotallyGuarded()
{
$this->setExpectedException('Jenssegers\Model\MassAssignmentException');
Expand Down
8 changes: 8 additions & 0 deletions tests/stubs/ModelStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class ModelStub extends Model
'data' => 'array',
'active' => 'bool',
'secret' => 'string',
'count' => 'int',
'object_data' => 'object',
'collection_data' => 'collection',
'foo' => 'bar',
];

protected $guarded = [
Expand All @@ -25,6 +29,10 @@ class ModelStub extends Model
'score',
'data',
'active',
'count',
'object_data',
'default',
'collection_data',
];

public function getListItemsAttribute($value)
Expand Down

0 comments on commit eedaeed

Please sign in to comment.