From 5f1eaba64aad5172b851b4ef95aaa07711f3dd9e Mon Sep 17 00:00:00 2001 From: Xenofon Spafaridis Date: Sun, 31 Jan 2016 01:08:10 +0200 Subject: [PATCH] Write tests for helper classes --- src/Filter.php | 26 ++++++---- src/Model/Model.php | 11 +++-- src/Resource.php | 2 +- src/Viewers/JSONAPI.php | 4 +- tests/src/FilterAttributeTest.php | 40 ++++++++++++++++ tests/src/FilterJSONAttributeTest.php | 41 ++++++++++++++++ tests/src/FilterTest.php | 22 ++++----- tests/src/ModelTest.php | 26 ++++++++++ tests/src/RelationshipTest.php | 69 +++++++++++++++++++++++---- tests/src/ResourceTest.php | 41 ++++++++++++++++ tests/src/ValidationModelTest.php | 45 +++++++++++++++++ tests/src/Viewers/JSONAPITest.php | 60 +++++++++++++++++++++++ 12 files changed, 351 insertions(+), 36 deletions(-) create mode 100644 tests/src/FilterAttributeTest.php create mode 100644 tests/src/FilterJSONAttributeTest.php create mode 100644 tests/src/ModelTest.php create mode 100644 tests/src/ResourceTest.php create mode 100644 tests/src/ValidationModelTest.php create mode 100644 tests/src/Viewers/JSONAPITest.php diff --git a/src/Filter.php b/src/Filter.php index e37b0d3..c404c9d 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -58,7 +58,13 @@ class Filter */ public $attributes = []; - + /** + * Filter constructor. + * @param array $primary + * @param array $relationships + * @param array $attributes + * @throws \Exception + */ public function __construct( $primary = [], $relationships = [], @@ -88,9 +94,9 @@ public function __construct( /** * @param object $parameters Request parameters + * @param string $modelClass + * @param bool $filterableJSON *[Optional]* * @return Filter|null - * @todo rewrite code - * @todo define $filterableJSON * @todo allow strings and integers as id * @todo Todo use filterValidation model for relationships * @todo allowed operator for JSON properties @@ -111,7 +117,7 @@ public function __construct( * ] * ]; * @throws RequestException - * @throws Exception + * @throws \Exception * @throws IncorrectParametersException * ``` */ @@ -195,11 +201,11 @@ public static function parseFromParameters($parameters, $modelClass, $filterable ); } - $filterSubkey = $filterKeyParts[1]; + $filterPropertyKey = $filterKeyParts[1]; - //Hack check $filterSubkey if valid using regular expression + //Hack check $filterPropertyKey if valid using regular expression (new StringValidator(0, null, self::JSON_ATTRIBUTE_FILTER_PROPERTY_EXPRESSION)) - ->parse($filterSubkey); + ->parse($filterPropertyKey); $filterKey = $filterKeyParts[0]; @@ -288,11 +294,11 @@ public static function parseFromParameters($parameters, $modelClass, $filterable //If filter validator is set for dereference JSON object property if ($filterValidationModel && isset($filterValidationModel->properties->{$filterKey}) - && isset($filterValidationModel->properties->{$filterKey}->properties->{$filterSubkey}) + && isset($filterValidationModel->properties->{$filterKey}->properties->{$filterPropertyKey}) ) { $attributePropertyValidator = $filterValidationModel->properties - ->{$filterKey}->properties->{$filterSubkey}; + ->{$filterKey}->properties->{$filterPropertyKey}; $operand = $attributePropertyValidator->parse($operand); } else { @@ -320,7 +326,7 @@ public static function parseFromParameters($parameters, $modelClass, $filterable if ($isJSONFilter) { //Push to attribute filters - $filter->attributes[] = new FilterJSONAttribute($filterKey, $filterSubkey, $operator, $operand); + $filter->attributes[] = new FilterJSONAttribute($filterKey, $filterPropertyKey, $operator, $operand); } else { //Push to attribute filters $filter->attributes[] = new FilterAttribute($filterKey, $operator, $operand); diff --git a/src/Model/Model.php b/src/Model/Model.php index 92126c4..6aa5284 100644 --- a/src/Model/Model.php +++ b/src/Model/Model.php @@ -29,10 +29,15 @@ abstract class Model { /** - * Model's method prefix, following by ucfirst(Class) + * Model's method prefix, following by ucfirst(type of resource) + * for example `getRelationshipByInput_template` if is was `input_template` */ const GET_RELATIONSHIP_BY_PREFIX = 'getRelationshipBy'; + /** + * Model's method prefix, following by ucfirst(type of resource) + * for example `postRelationshipByInput_template` if is was `input_template` + */ const POST_RELATIONSHIP_BY_PREFIX = 'postRelationshipBy'; /** @@ -65,13 +70,13 @@ abstract class Model /** * Resource's identification attribute (Primary key in database). - * **MAY** be overwritten, default is id + * **MAY** be overwritten, default is `"id"` * @var string */ protected static $idAttribute = 'id'; /** - * Resource's endpoint, usually it the same as type + * Resource's endpoint, used for access by external request, usually it the same as type * **MUST** be overwritten * @var string */ diff --git a/src/Resource.php b/src/Resource.php index d6a133b..19fd69c 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -68,7 +68,7 @@ class Resource public function __construct($type, $id) { $this->type = $type; - $this->id = $id; + $this->id = (string)$id; $this->links = new \stdClass(); $this->attributes = new \stdClass(); diff --git a/src/Viewers/JSONAPI.php b/src/Viewers/JSONAPI.php index 07a3047..20f8945 100644 --- a/src/Viewers/JSONAPI.php +++ b/src/Viewers/JSONAPI.php @@ -45,7 +45,9 @@ public function view($parameters) } //Include JSON API version object - $parameters->jsonapi = (object)['version' => '1.0']; + $parameters->jsonapi = (object)[ + 'version' => '1.0' + ]; echo json_encode($parameters); } diff --git a/tests/src/FilterAttributeTest.php b/tests/src/FilterAttributeTest.php new file mode 100644 index 0000000..f665d9d --- /dev/null +++ b/tests/src/FilterAttributeTest.php @@ -0,0 +1,40 @@ + + */ +class FilterAttributeTest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers ::__construct + */ + public function testConstruct() + { + new FilterAttribute( + 'id', + Operator::OPERATOR_EQUAL, + '5' + ); + } +} diff --git a/tests/src/FilterJSONAttributeTest.php b/tests/src/FilterJSONAttributeTest.php new file mode 100644 index 0000000..6617d6a --- /dev/null +++ b/tests/src/FilterJSONAttributeTest.php @@ -0,0 +1,41 @@ + + */ +class FilterJSONAttributeTest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers ::__construct + */ + public function testConstruct() + { + new FilterJSONAttribute( + 'meta', + 'keywords', + Operator::OPERATOR_LIKE, + 'blog' + ); + } +} diff --git a/tests/src/FilterTest.php b/tests/src/FilterTest.php index a37e9fb..8b8558d 100644 --- a/tests/src/FilterTest.php +++ b/tests/src/FilterTest.php @@ -197,7 +197,7 @@ public function testParseFromParametersFailurePrimaryNotString() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -216,7 +216,7 @@ public function testParseFromParametersFailurePrimaryToParse() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -234,7 +234,7 @@ public function testParseFromParametersFailureRelationshipNotString() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -252,7 +252,7 @@ public function testParseFromParametersFailureNotAllowedAttribute() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -270,7 +270,7 @@ public function testParseFromParametersFailureAttributeWithoutValidator() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -288,7 +288,7 @@ public function testParseFromParametersFailureAttributeIsArray() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -306,7 +306,7 @@ public function testParseFromParametersFailureAttributeToParse() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -324,7 +324,7 @@ public function testParseFromParametersFailureAttributeNotAllowedOperator() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -342,7 +342,7 @@ public function testParseFromParametersFailureAttributeNotAcceptingJSONOperator( ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -360,7 +360,7 @@ public function testParseFromParametersFailureAttributeUsingJSONPropertyValidato ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); @@ -378,7 +378,7 @@ public function testParseFromParametersFailureAttributeJSONSecondLevel() ] ]; - $filter = Filter::parseFromParameters( + Filter::parseFromParameters( $parameters, APP\Models\Article::class //Use article resource model's filters ); diff --git a/tests/src/ModelTest.php b/tests/src/ModelTest.php new file mode 100644 index 0000000..2d86b5a --- /dev/null +++ b/tests/src/ModelTest.php @@ -0,0 +1,26 @@ + + */ +class ModelTest extends \PHPUnit_Framework_TestCase +{ +} diff --git a/tests/src/RelationshipTest.php b/tests/src/RelationshipTest.php index 783f70c..9a274a2 100644 --- a/tests/src/RelationshipTest.php +++ b/tests/src/RelationshipTest.php @@ -16,35 +16,84 @@ */ namespace Phramework\JSONAPI; -use \Phramework\Phramework; +use Phramework\JSONAPI\APP\Models\Tag; +use Phramework\JSONAPI\Relationship; /** + * @coversDefaultClass \Phramework\JSONAPI\Relationship * @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0 * @author Xenofon Spafaridis */ class RelationshipTest extends \PHPUnit_Framework_TestCase { + /** + * @var Relationship + */ + protected $relationship; + + public function setUp() + { + $this->relationship = new Relationship( + 'tag-id', + Tag::getType(), + Relationship::TYPE_TO_ONE, + Tag::class, + Tag::getIdAttribute() + ); + } - private $object; + /** + * @covers ::__construct + */ + public function testConstruct() + { + new Relationship( + 'tag-id', + Tag::getType(), + Relationship::TYPE_TO_ONE, + Tag::class, + Tag::getIdAttribute() + ); + } + + /** + * @covers ::getRelationshipType + */ + public function testGetRelationshipType() + { + $this->relationship->getRelationshipType(); + } /** - * Sets up the fixture, for example, opens a network connection. - * This method is called before a test is executed. + * @covers ::getAttribute */ - protected function setUp() + public function testGetAttribute() { + $this->relationship->getAttribute(); } + /** - * Tears down the fixture, for example, closes a network connection. - * This method is called after a test is executed. + * @covers ::getResourceType */ - protected function tearDown() + public function testGetResourceType() { + $this->relationship->getResourceType(); + } + /** + * @covers ::getRelationshipClass + */ + public function testGetRelationshipClass() + { + $this->relationship->getRelationshipClass(); } - public function testExtends() + + /** + * @covers ::getRelationshipIdAttribute + */ + public function testGetRelationshipIdAttribute() { - $this->assertTrue(true); + $this->relationship->getRelationshipIdAttribute(); } } diff --git a/tests/src/ResourceTest.php b/tests/src/ResourceTest.php new file mode 100644 index 0000000..572cc65 --- /dev/null +++ b/tests/src/ResourceTest.php @@ -0,0 +1,41 @@ + + */ +class ResourceTest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers ::__construct + */ + public function testConstruct() + { + new Resource( + Article::getType(), + 'id' + ); + } +} diff --git a/tests/src/ValidationModelTest.php b/tests/src/ValidationModelTest.php new file mode 100644 index 0000000..ed99bc0 --- /dev/null +++ b/tests/src/ValidationModelTest.php @@ -0,0 +1,45 @@ + + */ +class ValidationModelTest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers ::__construct + */ + public function testConstruct() + { + new ValidationModel( + new ObjectValidator(), + null + ); + + new ValidationModel( + new ObjectValidator(), + new ObjectValidator() + ); + } +} diff --git a/tests/src/Viewers/JSONAPITest.php b/tests/src/Viewers/JSONAPITest.php new file mode 100644 index 0000000..592e6f5 --- /dev/null +++ b/tests/src/Viewers/JSONAPITest.php @@ -0,0 +1,60 @@ + + */ +class JSONAPITest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers ::view + */ + public function testView() + { + ob_start(); + (new JSONAPI())->view((object) [ + 'data' => (object) [ + 'type' => Article::getType(), + 'id' => '1' + ] + ]); + + (new JSONAPI())->view([ + 'data' => (object) [ + 'type' => Article::getType(), + 'id' => '10' + ] + ]); + ob_end_clean(); + } + + /** + * @covers ::header + * @before testView + */ + public function testHeader() + { + JSONAPI::header(); + } +}