Skip to content

Commit

Permalink
Write tests for helper classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Jan 30, 2016
1 parent 569a2c1 commit 5f1eaba
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 36 deletions.
26 changes: 16 additions & 10 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down Expand Up @@ -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
Expand All @@ -111,7 +117,7 @@ public function __construct(
* ]
* ];
* @throws RequestException
* @throws Exception
* @throws \Exception
* @throws IncorrectParametersException
* ```
*/
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/Viewers/JSONAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/src/FilterAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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;

use Phramework\Models\Operator;


/**
* @coversDefaultClass Phramework\JSONAPI\FilterAttribute
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class FilterAttributeTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers ::__construct
*/
public function testConstruct()
{
new FilterAttribute(
'id',
Operator::OPERATOR_EQUAL,
'5'
);
}
}
41 changes: 41 additions & 0 deletions tests/src/FilterJSONAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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;

use Phramework\Models\Operator;


/**
* @coversDefaultClass Phramework\JSONAPI\FilterJSONAttribute
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class FilterJSONAttributeTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers ::__construct
*/
public function testConstruct()
{
new FilterJSONAttribute(
'meta',
'keywords',
Operator::OPERATOR_LIKE,
'blog'
);
}
}
22 changes: 11 additions & 11 deletions tests/src/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testParseFromParametersFailurePrimaryNotString()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -216,7 +216,7 @@ public function testParseFromParametersFailurePrimaryToParse()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -234,7 +234,7 @@ public function testParseFromParametersFailureRelationshipNotString()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -252,7 +252,7 @@ public function testParseFromParametersFailureNotAllowedAttribute()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -270,7 +270,7 @@ public function testParseFromParametersFailureAttributeWithoutValidator()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -288,7 +288,7 @@ public function testParseFromParametersFailureAttributeIsArray()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -306,7 +306,7 @@ public function testParseFromParametersFailureAttributeToParse()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -324,7 +324,7 @@ public function testParseFromParametersFailureAttributeNotAllowedOperator()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -342,7 +342,7 @@ public function testParseFromParametersFailureAttributeNotAcceptingJSONOperator(
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -360,7 +360,7 @@ public function testParseFromParametersFailureAttributeUsingJSONPropertyValidato
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand All @@ -378,7 +378,7 @@ public function testParseFromParametersFailureAttributeJSONSecondLevel()
]
];

$filter = Filter::parseFromParameters(
Filter::parseFromParameters(
$parameters,
APP\Models\Article::class //Use article resource model's filters
);
Expand Down
26 changes: 26 additions & 0 deletions tests/src/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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;

/**
* @coversDefaultClass Phramework\JSONAPI\Model
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class ModelTest extends \PHPUnit_Framework_TestCase
{
}

0 comments on commit 5f1eaba

Please sign in to comment.