Skip to content

Commit

Permalink
Complete IncludeResources unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Jun 18, 2016
1 parent b9d5e5d commit 00ed8d4
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 7 deletions.
27 changes: 23 additions & 4 deletions src/Directive/IncludeResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,37 @@ public function getInclude()
* @param string[] $include
* @return $this
*/
public function setInclude($include)
public function setInclude(string ...$include)
{
$this->include = $include;

return $this;
}

/**
* @param ResourceModel $model
* @return bool
* @throws \DomainException If include relationship is not defined
* at resource model relationships
*/
public function validate(ResourceModel $model) : bool
{
// TODO: Implement validate() method.
foreach ($this->include as $include) {
if (!$model->issetRelationship($include)) {
throw new \DomainException(sprintf(
'Relationship "%s" is not defined for resource model "%s"',
$include,
$model->getResourceType()
));
}
}

return true;
}

/**
* @inheritDoc
* todo use include by default class defined in mode's relationships
* @todo use include by default class defined in mode's relationships
*/
public static function parseFromRequest(
ServerRequestInterface $request,
Expand All @@ -87,8 +102,12 @@ public static function parseFromRequest(
$include[] = trim($i);
}

return new IncludeResources(
$include = new IncludeResources(
...array_unique($include)
);

$include->validate($model);

return $include;
}
}
8 changes: 5 additions & 3 deletions tests/APP/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ protected static function defineModel() : ResourceModel
Company::getResourceModel(),
Relationship::TYPE_TO_MANY,
null,
function (array $ids) {
//todo
}
(object) [
/*'GET' => function (array $ids) {
//todo
}*/
]
)
]);

Expand Down
172 changes: 172 additions & 0 deletions tests/src/Directive/IncludeResourcesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
/**
* Copyright 2015-2016 Xenofon Spafaridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Phramework\JSONAPI\Directive;

use Phramework\JSONAPI\APP\Models\User;
use Zend\Diactoros\ServerRequest;

/**
* @coversDefaultClass Phramework\JSONAPI\Directive\IncludeResources
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class IncludeResourcesTest extends \PHPUnit_Framework_TestCase
{
/**
* @var IncludeResources
*/
protected $includeResources;

public function setUp()
{
$this->includeResources = new IncludeResources('group', 'company');
}

/**
* @covers ::__construct
*/
public function testConstruct()
{
new IncludeResources();
new IncludeResources('group', 'company');
}

/**
* @covers ::getInclude
*/
public function testGetInclude()
{
$this->assertSame(
['group', 'company'],
$this->includeResources->getInclude()
);
}

/**
* @covers ::setInclude
*/
public function testSetInclude()
{
$includeResources = new IncludeResources();
$includeResources->setInclude('tag', 'group');

$this->assertSame(
['tag', 'group'],
$includeResources->getInclude()
);
}

/**
* @covers ::validate
*/
public function testValidate()
{
$this->assertTrue(
$this->includeResources->validate(
User::getResourceModel()
)
);
}

/**
* @covers ::validate
* @expectedException \DomainException
*/
public function testValidateFailure()
{
$includeResources = new IncludeResources('tag', 'whatever');

$this->assertTrue(
$includeResources->validate(
User::getResourceModel()
)
);
}

public function parseProvider()
{
return [
['tag, group', ['tag', 'group']],
['tag', ['tag']],
['', null]
];
}

/**
* @covers ::parseFromRequest
* @dataProvider parseProvider
*/
public function testParseFromRequest(
string $queryParameter,
$expected = null
) {
$request = new ServerRequest(
[],
[],
null,
null,
'php://input',
[],
[],
[
'include' => $queryParameter
]
);

$includeResources = IncludeResources::parseFromRequest(
$request,
User::getResourceModel()
);

if ($includeResources === null) {
return;
}

$this->assertInstanceOf(
IncludeResources::class,
$includeResources
);

$this->assertEquals(
$expected,
$includeResources->getInclude()
);
}

/**
* @covers ::parseFromRequest
*/
public function testParseFromRequestUnsetParameter()
{
$request = new ServerRequest(
[],
[],
null,
null,
'php://input',
[],
[]
);

$includeResources = IncludeResources::parseFromRequest(
$request,
User::getResourceModel()
);

$this->assertNull($includeResources);
}
}

0 comments on commit 00ed8d4

Please sign in to comment.