Skip to content

Commit

Permalink
Test Model/Get
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Jan 31, 2016
1 parent 7aa4970 commit 19a22f9
Show file tree
Hide file tree
Showing 11 changed files with 387 additions and 33 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="true"
bootstrap="./vendor/autoload.php"
bootstrap="./tests/APP/Bootstrap.php"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
2 changes: 1 addition & 1 deletion src/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @since 1.0.0
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @property object-read $fields
* @property-read object $fields
*/
class Fields
{
Expand Down
6 changes: 3 additions & 3 deletions src/FilterAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
* @since 1.0.0
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @property-write string $attribute
* @property-write string $operator
* @property-write string $operand
* @property-read string $attribute
* @property-read string $operator
* @property-read string $operand
*/
class FilterAttribute
{
Expand Down
2 changes: 1 addition & 1 deletion src/FilterJSONAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @since 1.0.0
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
* @property-write string $key
* @property-read string $key
*/
class FilterJSONAttribute extends FilterAttribute
{
Expand Down
9 changes: 4 additions & 5 deletions src/Model/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
namespace Phramework\JSONAPI\Model;

use Phramework\Exceptions\NotImplementedException;
use Phramework\JSONAPI\Fields;
use Phramework\JSONAPI\Filter;
use Phramework\JSONAPI\Page;
Expand Down Expand Up @@ -100,20 +101,18 @@ public static function getById($id, Fields $fields = null, ...$additionalParamet
}

//Prepare filter
$filter = new Filter();

$filter->primary = (
$filter = new Filter((
is_array($id)
? $id
: [$id]
); //Force array for primary data
)); //Force array for primary data

$collection = static::get(
new Page(count($id)), //limit number of requested resources
$filter,
null, //sort
$fields, //fields
$additionalParameters
...$additionalParameters
);

if (!is_array($id)) {
Expand Down
14 changes: 8 additions & 6 deletions tests/APP/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
namespace Phramework\JSONAPI\APP;

define('NS', '\\Phramework\\JSONAPI\\APP\\Controllers\\');
include __DIR__ . '/../../vendor/autoload.php';

define('NS', 'Phramework\\JSONAPI\\APP\\Controllers\\');

use \Phramework\Phramework;

Expand Down Expand Up @@ -50,11 +52,9 @@ public static function prepare()
//Initialize API
$phramework = new Phramework($settings, $URIStrategy);

\Phramework\Database\Database::setAdapter(
new \Phramework\Database\MySQL($settings['database'])
);

var_dump($settings);
//\Phramework\Database\Database::setAdapter(
// new \Phramework\Database\MySQL($settings['database'])
//);

Phramework::setViewer(
\Phramework\JSONAPI\APP\Viewers\Viewer::class
Expand All @@ -68,3 +68,5 @@ public static function prepare()
//$phramework->invoke();
}
}

Bootstrap::prepare();
41 changes: 39 additions & 2 deletions tests/APP/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
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\JSONAPI\ValidationModel;
use Phramework\Models\Operator;
use \Phramework\Validate\ArrayValidator;
Expand Down Expand Up @@ -118,8 +122,9 @@ public static function get(
Fields $fields = null,
...$additionalParameters
) {
$articles = [
$records = [
[
'id' => '1',
'creator-user_id' => 1,
'status' => 1,
'title' => 'First post',
Expand All @@ -129,15 +134,47 @@ public static function get(
]
],
[
'id' => '2',
'creator-user_id' => 1,
'status' => 1,
'title' => 'Second post',
'updated' => time(),
'meta' => null
],
[
'id' => '3',
'creator-user_id' => 2,
'status' => 1,
'title' => 'Third post',
'updated' => time() + 100,
'meta' => null
],
[
'id' => '4',
'creator-user_id' => 1,
'status' => 0,
'title' => 'Fourth post',
'updated' => time() + 1000,
'meta' => null
]
];

return self::collection($articles);
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);
}

public static function getRelationships()
Expand Down
71 changes: 57 additions & 14 deletions tests/APP/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
*/
namespace Phramework\JSONAPI\APP\Models;

use \Phramework\Database\Database;
use \Phramework\JSONAPI\Relationship;
use \Phramework\Validate\ArrayValidator;
use \Phramework\Validate\ObjectValidator;
use \Phramework\Validate\StringValidator;
use \Phramework\Validate\UnsignedIntegerValidator;
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
Expand Down Expand Up @@ -50,12 +54,49 @@ public static function get(
Fields $fields = null,
...$additionalParameters
) {
return self::collection(
Database::executeAndFetchAll(
'SELECT * FROM "tag"
WHERE "status" = 1'
)
);
$records = [
[
'id' => 1,
'status' => 1,
'title' => 'Tag 1',
'created' => null
],
[
'id' => 2,
'status' => 1,
'title' => 'Tag 2',
'created' => time()
],
[
'id' => 3,
'status' => 1,
'title' => 'Tag 3',
'created' => time() + 100
],
[
'id' => 4,
'status' => 0,
'title' => 'Tag 4',
'created' => time() + 1000
]
];

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);
}

/**
Expand Down Expand Up @@ -86,7 +127,7 @@ public static function postRelationshipByArticle(

public static function getRelationshipByArticle($articleId)
{
return Database::executeAndFetchAllArray(
/*return Database::executeAndFetchAllArray(
'SELECT "t"."id"
FROM "article-tag" as "a-t"
JOIN "tag" AS "t"
Expand All @@ -95,7 +136,9 @@ public static function getRelationshipByArticle($articleId)
AND "a-t"."status" = 1
AND "t"."status" = 1',
[$articleId]
);
);*/

return [];
}

public static function getRelationships()
Expand Down
32 changes: 32 additions & 0 deletions tests/src/FilterJSONAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
*/
class FilterJSONAttributeTest extends \PHPUnit_Framework_TestCase
{
protected $filterAttribute;

public function setUp()
{
$this->filterAttribute = new FilterJSONAttribute(
'meta',
'keywords',
Operator::OPERATOR_LIKE,
'blog'
);
}

/**
* @covers ::__construct
*/
Expand All @@ -37,4 +49,24 @@ public function testConstruct()
'blog'
);
}

/**
* @covers ::__get
*/
public function test__get()
{
$this->assertSame(
'keywords',
$this->filterAttribute->key
);
}

/**
* @covers ::__get
* @expectedException PHPUnit_Framework_Error_Notice
*/
public function test__getFailure()
{
$this->assertNull($this->filterAttribute->{'not-found'});
}
}
62 changes: 62 additions & 0 deletions tests/src/Model/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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\Model;

use Gitonomy\Git\Reference\Tag;
use Phramework\JSONAPI\APP\Models\Article;
use Phramework\JSONAPI\Page;
use Phramework\JSONAPI\Resource;

/**
* @coversDefaultClass Phramework\JSONAPI\Model\Cache
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class CacheTest extends \PHPUnit_Framework_TestCase
{
/**
* Helper method
* @param int $limit
* @param int $offset
* @return Resource
*/
protected function get($limit = 1, $offset = 0)
{
//Get first trend
$collection = Article::get(
new Page($limit, $offset)
);

return $collection;
}

/**
* @covers ::getCache
*/
public function testGetCache()
{
}

/**
* @covers ::setCache
*/
public function testSetCache()
{
//Get with offset (another resource)
//$collection = self::get(1, 3);
}
}

0 comments on commit 19a22f9

Please sign in to comment.