Skip to content

Commit

Permalink
Prepare POST tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Feb 3, 2016
1 parent 47d1c0e commit 1cc3c56
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 50 deletions.
47 changes: 39 additions & 8 deletions src/Controller/POST.php
Expand Up @@ -17,9 +17,11 @@
namespace Phramework\JSONAPI\Controller;

use Phramework\JSONAPI\Controller\POST\QueueItem;
use Phramework\JSONAPI\Util;
use \Phramework\Models\Request;
use \Phramework\Exceptions\RequestException;
use \Phramework\JSONAPI\Relationship;
use Phramework\Validate\ObjectValidator;

/**
* POST related methods
Expand All @@ -44,6 +46,7 @@ abstract class POST extends \Phramework\JSONAPI\Controller\GET
* so that any exceptions can be thrown, and finally invoke the execution of the queue.
* @uses $modelClass::post method to create resources
* @return int[]
* @todo validate type
*/
protected static function handlePOST(
$params,
Expand All @@ -59,7 +62,7 @@ protected static function handlePOST(
$data = static::getRequestData($params);

//Treat single requests as an array of resources
if (!is_array($data)) {
if (is_object($data) || is_array($data) && Util::isArrayAssoc($data)) {
$data = [$data];
}

Expand All @@ -68,7 +71,11 @@ protected static function handlePOST(
$resource = (object)$resource;
}

$requestAttributes = $resource->attributes;
$requestAttributes = (
isset($resource->attributes) && $resource->attributes
? $resource->attributes
: new \stdClass()
);

if (property_exists($resource, 'relationships')) {
$requestRelationships = $resource->relationships;
Expand Down Expand Up @@ -126,6 +133,8 @@ protected static function handlePOST(
$ids[] = $id;
}

return $ids;

if (count($ids) === 1) {
//Prepare response with 201 Created status code
\Phramework\Models\Response::created(
Expand Down Expand Up @@ -170,10 +179,17 @@ private static function handlePOSTResource(
$validationCallbacks,
$requestRelationships
) {
$validationModel = $modelClass::getValidationModel();
$validator = $modelClass::getValidationModel();

$attributesValidator = (
isset($validator->attributes) && $validator->attributes
? $validator->attributes
: new ObjectValidator()
);


//Parse request attributes using $validationModel to validate the data
$attributes = $validationModel->attributes->parse($requestAttributes);
$attributes = $attributesValidator->parse($requestAttributes);

$relationships = $modelClass::getRelationships();

Expand All @@ -193,6 +209,11 @@ private static function handlePOSTResource(
* - copy ids to $relationshipAttributes object
*/
foreach ($requestRelationships as $relationshipKey => $relationshipValue) {
//Work with objects
if (is_array($relationshipValue)) {
$relationshipValue = (object) $relationshipValue;
}

if (!isset($relationshipValue->data)) {
throw new RequestException(sprintf(
'Relationship "%s" must have a member data defined',
Expand All @@ -202,6 +223,10 @@ private static function handlePOSTResource(

$relationshipData = $relationshipValue->data;

if (is_array($relationshipData) && Util::isArrayAssoc($relationshipData)) {
$relationshipData = (object) $relationshipData;
}

//Check if relationship exists
static::exists(
$modelClass::relationshipExists($relationshipKey),
Expand Down Expand Up @@ -252,25 +277,32 @@ private static function handlePOSTResource(
}

foreach ($relationshipData as $value) {
if (is_array($value) && Util::isArrayAssoc($value)) {
$value = (object) $value;
}

if (!is_object($value)) {
throw new RequestException(sprintf(
'Expected data properties to be object for relationship "%s"',
$relationshipKey
));
}

if (!isset($value->id) || !isset($value->type)) {
throw new RequestException(sprintf(
'Attributes "id" and "type" required for relationship "%s"',
$relationshipKey
));
}

if ($value->type !== $relationshipResourceType) {
throw new RequestException(sprintf(
'Invalid resource type "%s" for relationship "%s"',
$relationshipResourceType,
$relationshipKey
));
}

$parsedValues[] = $value->id;
}

Expand All @@ -284,8 +316,8 @@ private static function handlePOSTResource(
//Parse attributes using relationship's validation model
$parsedRelationshipAttributes =
(
isset($validationModel->relationships)
? $validationModel->relationships->parse(
isset($validator->relationships)
? $validator->relationships->parse(
$relationshipAttributes
)
: new \stdClass()
Expand Down Expand Up @@ -409,9 +441,8 @@ private static function handlePOSTResource(
);
}


//Push to queueRelationships
$queueRelationships->{$relationshipKey} = (object)[
$queueRelationships->{$relationshipKey} = (object) [
'callback' => $relationshipCallMethod, //callable
'resources' => $parsedRelationshipValue //array
];
Expand Down
89 changes: 52 additions & 37 deletions tests/APP/Models/Article.php
Expand Up @@ -125,18 +125,63 @@ public static function get(
Fields $fields = null,
...$additionalParameters
) {
$records = [
return self::collection(
self::handleGetWithArrayOfRecords(
static::getRecords(),
$page,
$filter,
$sort,
$fields,
...$additionalParameters
),
$fields
);
}

public static function getRelationships()
{
return (object)[
'creator' => new Relationship(
'creator-user_id',
'user',
Relationship::TYPE_TO_ONE,
User::class,
'id'
),
'tag' => new Relationship(
'tag-id',
'tag',
Relationship::TYPE_TO_MANY,
Tag::class,
'id'
)
];
}

public static function post(
$attributes,
$return = \Phramework\Database\Operations\Create::RETURN_ID
) {
$records = static::getRecords();

return $records[0]['id'];
}


public static function getRecords()
{
return [
[
'id' => '1',
'creator-user_id' => '1',
'status' => 1,
'title' => 'First post',
'updated' => null,
'meta' => (object) [
'meta' => (object)[
'keywords' => 'blog'
],
Resource::META_MEMBER => (object) [
'view' => 1000,
Resource::META_MEMBER => (object)[
'view' => 1000,
'unique' => 100
]
],
Expand All @@ -149,7 +194,9 @@ public static function get(
'meta' => null,
Resource::META_MEMBER => [
'some_key' => [
1, 2, 3
1,
2,
3
]
]
],
Expand All @@ -170,37 +217,5 @@ public static function get(
'meta' => null
]
];

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

public static function getRelationships()
{
return (object)[
'creator' => new Relationship(
'creator-user_id',
'user',
Relationship::TYPE_TO_ONE,
User::class,
'id'
),
'tag' => new Relationship(
'tag-id',
'tag',
Relationship::TYPE_TO_MANY,
Tag::class,
'id'
)
];
}
}
5 changes: 3 additions & 2 deletions tests/APP/Models/Tag.php
Expand Up @@ -106,7 +106,7 @@ public static function postRelationshipByArticle(
$additionalAttributes = null,
$return = \Phramework\Database\Operations\Create::RETURN_NUMBER_OF_RECORDS
) {
return \Phramework\Database\Operations\Create::create(
/*return \Phramework\Database\Operations\Create::create(
[
'tag-id' => $tagId,
'article-id' => $articleId,
Expand All @@ -115,7 +115,8 @@ public static function postRelationshipByArticle(
'article-tag',
static::getSchema(),
$return
);
);*/
return 1;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion tests/src/Controllers/POSTTest.php
Expand Up @@ -52,6 +52,7 @@ protected function prepare()
$_SERVER['REQUEST_METHOD'] = Phramework::METHOD_POST;

$_POST['data'] = [
'type' => 'article',
'attributes' => [
'title' => 'omg'
],
Expand Down Expand Up @@ -90,6 +91,7 @@ function (
$that->params = $parameters;
}
);

// clean the output buffer
//ob_clean();
//$this->phramework->invoke();
Expand All @@ -108,6 +110,14 @@ protected function tearDown()
//}
}

/**
* @covers \Phramework\JSONAPI\Controller\POST::handlePOSTResource
*/
public function testHandlePOSTResource()
{
$this->testPOSTSuccess();
}

/**
* @covers \Phramework\JSONAPI\Controller\POST::handlePOST
*/
Expand All @@ -121,7 +131,7 @@ public function testPOSTSuccess()
//Access parameters written by invoked phramework's viewer
$params = $this->params;

var_dump($params);
print_r([$params]);

$this->assertInternalType('object', $params);

Expand Down
4 changes: 2 additions & 2 deletions tests/src/Model/RelationshipTest.php
Expand Up @@ -34,14 +34,14 @@ public function testGetRelationshipData()
{
$articles = Article::get(new Page(1, 1))[0];

print_r($articles);
//print_r($articles);

$relationship = Tag::getRelationshipByArticle(
'1',
'tag',
null
);

print_r($relationship);
//print_r($relationship);
}
}

0 comments on commit 1cc3c56

Please sign in to comment.