Skip to content

Commit

Permalink
Improve phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Feb 13, 2016
1 parent 5d8456a commit 1e12fc1
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function validate($modelClass)
* $fields = Fields::parseFromParameters(
* (object) [
* 'fields' => [
* Article::getType() => ['title, updated'],
* Article::getType() => ['title', updated'],
* Tag::getType() => ['title']
* ]
* ], //Request parameters object
Expand Down
24 changes: 21 additions & 3 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function __construct(
* @throws \Exception
* @throws IncorrectParametersException When filter for primary id attribute is incorrect
* @throws IncorrectParametersException When filter for relationship is incorrect
* @todo add relationship validator
* @example
* ```php
* $filter = new Filter([1, 2, 3]);
Expand Down Expand Up @@ -181,8 +180,27 @@ public function validate($modelClass)
continue;
}

//@TODO add relationship validator
$relationshipValidator = [UnsignedIntegerValidator::class, 'parseStatic'];
$relationshipObject = $modelClass::getRelationship($relationshipKey);
$relationshipObjectModelClass = $relationshipObject->modelClass;
$relationshipValidationModel = $relationshipObjectModelClass::getValidationModel();
$relationshipFilterValidationModel = $relationshipObjectModelClass::getValidationModel();

if ($relationshipFilterValidationModel !== null
&& isset($relationshipFilterValidationModel->properties->{$relationshipObjectModelClass::getIdAttribute()})) {
$relationshipValidator = [
$relationshipFilterValidationModel->properties->{$relationshipObjectModelClass::getIdAttribute()},
'parse'
];
} elseif ($relationshipValidationModel !== null
&& isset($relationshipValidationModel->properties->{$relationshipObjectModelClass::getIdAttribute()})
) {
$relationshipValidator = [
$relationshipValidationModel->properties->{$relationshipObjectModelClass::getIdAttribute()},
'parse'
];
} else {
$relationshipValidator = [UnsignedIntegerValidator::class, 'parseStatic'];
}

//Run validator, if any value is incorrect IncorrectParametersException will be thrown
foreach ($relationshipValue as $id) {
Expand Down
34 changes: 34 additions & 0 deletions tests/src/Model/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Phramework\JSONAPI\APP\Models\Tag;
use Phramework\JSONAPI\Page;
use Phramework\JSONAPI\Resource;
use Phramework\JSONAPI\Sort;

/**
* @coversDefaultClass Phramework\JSONAPI\Model\Get
Expand Down Expand Up @@ -85,6 +86,7 @@ public function testGetById()

//Fetch multiple resources
$resources = Article::getById($ids);

//Request again to access cached
$resources = Article::getById($ids);

Expand Down Expand Up @@ -161,4 +163,36 @@ public function testGetById()

return $id;
}

/**
* @covers ::parseSort
*/
public function testParseSort()
{
$this->assertInstanceOf(Sort::class, Article::parseSort((object) []));
}

/**
* @covers ::parsePage
*/
public function testParsePage()
{
$this->assertNull(Article::parsePage((object) []));
}

/**
* @covers ::parseFields
*/
public function testParseFields()
{
$this->assertNull(Article::parseFields((object) []));
}

/**
* @covers ::parseFilter
*/
public function testParseFilter()
{
$this->assertNull(Article::parseFilter((object) []));
}
}
80 changes: 80 additions & 0 deletions tests/src/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Phramework\JSONAPI\APP\Models\Tag;
use Phramework\JSONAPI\Relationship;
use Phramework\Phramework;

/**
* @coversDefaultClass \Phramework\JSONAPI\Relationship
Expand Down Expand Up @@ -61,6 +62,34 @@ public function testConstruct()
);
}

/**
* @covers ::__construct
*/
public function testConstruct2()
{
new Relationship(
Tag::class,
Relationship::TYPE_TO_ONE,
'tag-id',
[Tag::class, 'getRelationshipByArticle']
);
}

/**
* @covers ::__construct
*/
public function testConstruct3()
{
new Relationship(
Tag::class,
Relationship::TYPE_TO_ONE,
'tag-id',
(object) [
Phramework::METHOD_GET => [Tag::class, 'getRelationshipByArticle']
]
);
}

/**
* @covers ::__construct
* @expectedException \Exception
Expand Down Expand Up @@ -88,6 +117,38 @@ public function testConstructFailure2()
);
}

/**
* @covers ::__construct
* @expectedException \Exception
*/
public function testConstructFailureInvalidCallbackMethod()
{
new Relationship(
Tag::class,
Relationship::TYPE_TO_ONE,
null,
(object) [
'HTTPMETODNOTALLOWED' => [Tag::class, 'getRelationshipByArticle']
]
);
}

/**
* @covers ::__construct
* @expectedException \Exception
*/
public function testConstructFailureInvalidMethodNotCallable()
{
new Relationship(
Tag::class,
Relationship::TYPE_TO_ONE,
null,
(object) [
Phramework::METHOD_GET => [Tag::class, 'getRelationshipByArticleNotCallable']
]
);
}

/**
* @covers ::__get
* @param string $property
Expand All @@ -107,4 +168,23 @@ public function testGetFailure()
{
$this->relationship->{'not-found'};
}

/**
* @covers ::__set
*/
public function testSet()
{
$this->relationship->{'flag'} = Relationship::FLAG_INCLUDE_BY_DEFAULT;

$this->assertSame(Relationship::FLAG_INCLUDE_BY_DEFAULT, $this->relationship->{'flag'});
}

/**
* @covers ::__set
* @expectedException \Exception
*/
public function testSetFailure()
{
$this->relationship->{'modelClass'} = Tag::class;
}
}
12 changes: 12 additions & 0 deletions tests/src/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ public function testSet($property)
$this->assertEquals($obj, $resource->{$property});
}

/**
* @covers ::__set
*/
public function testSetId()
{
$resource = new Resource(Tag::getType(), '1');

$resource->{'id'} = '2';

$this->assertEquals('2', $resource->{'id'});
}

/**
* @covers ::__set
* @param string $property
Expand Down

0 comments on commit 1e12fc1

Please sign in to comment.