Skip to content

Commit

Permalink
Test Model cache methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Feb 1, 2016
1 parent 19a22f9 commit 2da2390
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 67 deletions.
11 changes: 10 additions & 1 deletion src/Model/Cache.php
Expand Up @@ -61,13 +61,16 @@ protected static function initializeCache($type)
}

/**
* *NOTE* Will return a clone of the resource, so any changes to it wont affect the stored in cache resource.
* @param string $id
* @return Resource|null
*/
protected static function getCache($id)
{
$type = static::getType();

$id = (string)$id;

static::initializeCache($type);

$collection = self::$cache->{$type};
Expand All @@ -76,7 +79,9 @@ protected static function getCache($id)
return null;
}

return $collection->{$id};
$resource = $collection->{$id};

return (clone $resource);
}

/**
Expand All @@ -92,6 +97,8 @@ protected static function setCache($id, $resource)

$type = static::getType();

$id = (string)$id;

static::initializeCache($type);

$collection = self::$cache->{$type};
Expand All @@ -109,6 +116,8 @@ protected static function invalidateCache($id)
{
$type = static::getType();

$id = (string)$id;

static::initializeCache($type);

$collection = self::$cache->{$type};
Expand Down
1 change: 0 additions & 1 deletion src/Model/Get.php
Expand Up @@ -70,7 +70,6 @@ public static function get(
*
* // Will return a valid resource or null in case in's not found
* ```
* @todo make sure call to get method with multiple $additionalParameters will work
*/
public static function getById($id, Fields $fields = null, ...$additionalParameters)
{
Expand Down
35 changes: 35 additions & 0 deletions tests/APP/Bootstrap.php
Expand Up @@ -67,6 +67,41 @@ public static function prepare()
//Execute API
//$phramework->invoke();
}

/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
* @link https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/
*/
public static function invokeMethod(&$object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}

/**
* Call protected/private method of a class.
*
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
* @link https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/
*/
public static function invokeStaticMethod($class, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass($class);
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs(null, $parameters);
}
}

Bootstrap::prepare();
51 changes: 51 additions & 0 deletions tests/APP/Model.php
@@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: nohponex
* Date: 1/2/2016
* Time: 2:29 μμ
*/

namespace Phramework\JSONAPI\APP;

use Phramework\JSONAPI\Filter;
use Phramework\JSONAPI\Sort;
use Phramework\JSONAPI\Fields;
use Phramework\JSONAPI\Page;

class Model extends \Phramework\JSONAPI\Model
{
/**
* @param array[] $records
* @param Page $page
* @param Filter $filter
* @param Sort $sort
* @param Fields $fields
* @return array[]
*/
public static function handleGetWithArrayOfRecords(
$records,
Page $page = null,
Filter $filter = null,
Sort $sort = null,
Fields $fields = null,
...$additionalParameters
) {
if ($filter !== null) {
$records = array_filter(
$records,
function ($record) use ($filter) {
return in_array($record['id'], $filter->primary, false);
}
);
}

if ($page !== null) {
$records = array_values(
array_slice($records, $page->offset, $page->limit)
);
}

return $records;
}
}
27 changes: 9 additions & 18 deletions tests/APP/Models/Article.php
Expand Up @@ -35,7 +35,7 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class Article extends \Phramework\JSONAPI\Model
class Article extends \Phramework\JSONAPI\APP\Model
{
protected static $type = 'article';
protected static $endpoint = 'article';
Expand Down Expand Up @@ -113,7 +113,6 @@ public static function getFilterable()
* @param mixed ...$additionalParameters *[Optional]*
* @throws NotImplementedException
* @return Resource[]
* @todo apply Page, Filter and Sort rules to arrays as helper utility
*/
public static function get(
Page $page = null,
Expand Down Expand Up @@ -159,22 +158,14 @@ public static function get(
]
];

if ($filter !== null) {
$records = array_filter(
$records,
function ($record) use ($filter) {
return in_array($record['id'], $filter->primary, false);
}
);
}

if ($page !== null) {
$records = array_values(
array_slice($records, $page->offset, $page->limit)
);
}

return self::collection($records);
return self::collection(self::handleGetWithArrayOfRecords(
$records,
$page,
$filter,
$sort,
$fields,
...$additionalParameters
));
}

public static function getRelationships()
Expand Down
86 changes: 86 additions & 0 deletions tests/APP/Models/NotCachedModel.php
@@ -0,0 +1,86 @@
<?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\APP\Models;

use Phramework\Database\Database;
use Phramework\JSONAPI\Fields;
use Phramework\JSONAPI\Filter;
use Phramework\JSONAPI\Page;
use Phramework\JSONAPI\Relationship;
use Phramework\JSONAPI\Sort;
use Phramework\Validate\ArrayValidator;
use Phramework\Validate\ObjectValidator;
use Phramework\Validate\StringValidator;
use Phramework\Validate\UnsignedIntegerValidator;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class NotCachedModel extends \Phramework\JSONAPI\APP\Model
{
protected static $type = 'not_cached';
protected static $endpoint = 'not_cached';
protected static $table = 'not-cached';

/**
* Disable caching
* @var bool
*/
static $caching = false;

/**
* @param Page|null $page *[Optional]*
* @param Filter|null $filter *[Optional]*
* @param Sort|null $sort *[Optional]*
* @param Fields|null $fields *[Optional]*
* @param mixed ...$additionalParameters *[Optional]*
* @throws NotImplementedException
* @return Resource[]
*/
public static function get(
Page $page = null,
Filter $filter = null,
Sort $sort = null,
Fields $fields = null,
...$additionalParameters
) {
$records = [
[
'id' => 1,
'status' => 1,
'title' => 'Tag 1',
'created' => null
],
[
'id' => 2,
'status' => 1,
'title' => 'Tag 2',
'created' => time()
]
];

return self::collection(self::handleGetWithArrayOfRecords(
$records,
$page,
$filter,
$sort,
$fields,
...$additionalParameters
));
}
}
27 changes: 9 additions & 18 deletions tests/APP/Models/Tag.php
Expand Up @@ -31,7 +31,7 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class Tag extends \Phramework\JSONAPI\Model
class Tag extends \Phramework\JSONAPI\APP\Model
{
protected static $type = 'tag';
protected static $endpoint = 'tag';
Expand All @@ -45,7 +45,6 @@ class Tag extends \Phramework\JSONAPI\Model
* @param mixed ...$additionalParameters *[Optional]*
* @throws NotImplementedException
* @return Resource[]
* @todo apply Page, Filter and Sort rules to arrays as helper utility
*/
public static function get(
Page $page = null,
Expand Down Expand Up @@ -81,22 +80,14 @@ public static function get(
]
];

if ($filter !== null) {
$records = array_filter(
$records,
function ($record) use ($filter) {
return in_array($record['id'], $filter->primary, false);
}
);
}

if ($page !== null) {
$records = array_values(
array_slice($records, $page->offset, $page->limit)
);
}

return self::collection($records);
return self::collection(self::handleGetWithArrayOfRecords(
$records,
$page,
$filter,
$sort,
$fields,
...$additionalParameters
));
}

/**
Expand Down
19 changes: 11 additions & 8 deletions tests/APP/Models/User.php
Expand Up @@ -27,7 +27,7 @@
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class User extends \Phramework\JSONAPI\Model
class User extends \Phramework\JSONAPI\APP\Model
{
protected static $type = 'user';
protected static $endpoint = 'user';
Expand All @@ -41,7 +41,6 @@ class User extends \Phramework\JSONAPI\Model
* @param mixed ...$additionalParameters *[Optional]*
* @throws NotImplementedException
* @return Resource[]
* @todo apply Page, Filter and Sort rules to arrays as helper utility
*/
public static function get(
Page $page = null,
Expand All @@ -50,11 +49,15 @@ public static function get(
Fields $fields = null,
...$additionalParameters
) {
return self::collection(
Database::executeAndFetchAll(
'SELECT * FROM "user"
WHERE "status" = 1'
)
);
$records = [];

return self::collection(self::handleGetWithArrayOfRecords(
$records,
$page,
$filter,
$sort,
$fields,
...$additionalParameters
));
}
}

0 comments on commit 2da2390

Please sign in to comment.