Skip to content

Commit

Permalink
Write phpunit tests for handlePost
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Oct 8, 2016
1 parent 748aff3 commit ae4cb8e
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 21 deletions.
7 changes: 6 additions & 1 deletion src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ function ($d) {
$model
);


if ($parsed !== null) {
//overwrite
if ($overwrite
Expand All @@ -238,6 +237,12 @@ function ($d) {
return $directives;
}

/**
* @param $object
* @param ISource|null $source
* @param string[] ...$properties
* @throws MissingParametersException
*/
public static function requireProperties(
$object,
ISource $source = null,
Expand Down
10 changes: 4 additions & 6 deletions src/Controller/Helper/RequestBodyQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
trait RequestBodyQueue
{
/**
* What is resource ?
* @todo
* @param \stdClass $resource Primary data resource
*/
Expand All @@ -47,8 +46,7 @@ public static function handleResource(
ResourceModel $model,
ValidationModel $validationModel,
array $validationCallbacks = []
)
{
) {
//Fetch request attributes
$requestAttributes = $resource->attributes ?? new \stdClass();
$requestRelationships = $resource->relationships ?? new \stdClass();
Expand Down Expand Up @@ -164,7 +162,7 @@ function (\stdClass $p) {

//Convert to array
$tempIds = (
is_array($rValue)
is_array($rValue)
? $rValue
: [$rValue]
);
Expand Down Expand Up @@ -204,7 +202,8 @@ function (\stdClass $p) {
$callback(
$resource,
$parsedAttributes, //parsed
$parsedRelationships //parsed
$parsedRelationships, //parsed
$source
);
}

Expand All @@ -214,4 +213,3 @@ function (\stdClass $p) {
);
}
}

46 changes: 36 additions & 10 deletions src/Controller/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Phramework\Exceptions\ForbiddenException;
use Phramework\Exceptions\IncorrectParameterException;
use Phramework\Exceptions\MissingParametersException;
use Phramework\Exceptions\RequestException;
use Phramework\Exceptions\ServerException;
use Phramework\Exceptions\Source\Pointer;
Expand All @@ -43,19 +44,29 @@ trait Post
{
use RequestBodyQueue;

//prototype
/**
* Handle HTTP POST request method to create new resources
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param ResourceModel $model
* @param array $validationCallbacks
* @param callable|null $viewCallback
* @param array $validationCallbacks function of
* - \stdClass $resource
* - \stdClass $parsedAttributes
* - \stdClass $parsedRelationships
* - ISource $source
* returning void
* @param callable|null $viewCallback function of
* - ServerRequestInterface $request,
* - ResponseInterface $response,
* - string[] $ids
* - returning ResponseInterface
* @param int|null $bulkLimit
* @param array $directives
* @return ResponseInterface
* @throws ForbiddenException
* @throws RequestException
* @throws MissingParametersException
* @throws ServerException
*/
public static function handlePost(
ServerRequestInterface $request,
Expand All @@ -67,10 +78,12 @@ public static function handlePost(
array $directives = []
) : ResponseInterface {
//todo figure out a permanent solution to have body as object instead of array, for every framework
$body = json_decode(json_encode($request->getParsedBody()));
$body = json_decode(json_encode($request->getParsedBody())) ?? new \stdClass();

Controller::requireProperties($body, new Pointer('/'), 'data');

//Access request body primary data
$data = $body->data ?? [new \stdClass()];
$data = $body->data;

/**
* @var bool
Expand All @@ -88,7 +101,7 @@ public static function handlePost(
//check bulk limit
if ($bulkLimit !== null && count($data) > $bulkLimit) {
throw new RequestException(sprintf(
'Number of batch requests is exceeding the maximum of %s',
'Number of bulk requests is exceeding the maximum of %s',
$bulkLimit
));
}
Expand Down Expand Up @@ -143,6 +156,7 @@ public static function handlePost(
$item = static::handleResource(
$resource,
$source,
$model,
$validationModel,
$validationCallbacks
);
Expand Down Expand Up @@ -233,13 +247,25 @@ public static function handlePost(
);
}

/*if (count($ids) === 1) {
return Post::defaultViewCallback(
$request,
$response,
$ids
);
}

public static function defaultViewCallback(
ServerRequestInterface $request,
ResponseInterface $response,
array $ids
) : ResponseInterface {
if (count($ids) === 1) {
//Prepare response with 201 Created status code
return Response::created(
$response = Response::created(
$response,
'link' . $ids[0] // location
'link/' . $ids[0] // location //todo
);
}*/ //see https://stackoverflow.com/questions/11309444/can-the-location-header-be-used-for-multiple-resource-locations-in-a-201-created
} //see https://stackoverflow.com/questions/11309444/can-the-location-header-be-used-for-multiple-resource-locations-in-a-201-created

//Return 204 No Content
return Response::noContent($response);
Expand Down
2 changes: 1 addition & 1 deletion src/Directive/FilterAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static function parse($filterKey, $filterValue)
}

//@todo is this required?
$singleFilterValue = urldecode($singleFilterValue);
$singleFilterValue = urldecode((string) $singleFilterValue);

list($operator, $operand) = Operator::parse($singleFilterValue);

Expand Down
2 changes: 1 addition & 1 deletion tests/APP/DataSource/MemoryDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function post(

if (!property_exists($attributes, $idAttribute)) {
//generate an id
$attributes->{$idAttribute} = md5(mt_rand());
$attributes->{$idAttribute} = md5((string) mt_rand());
}

$table = $this->resourceModel->getVariable('table');
Expand Down
17 changes: 16 additions & 1 deletion tests/APP/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use Phramework\JSONAPI\ResourceModel;
use Phramework\JSONAPI\Model;
use Phramework\JSONAPI\ModelTrait;
use Phramework\JSONAPI\ValidationModel;
use Phramework\Validate\ObjectValidator;
use Phramework\Validate\StringValidator;

/**
* @since 3.0.0
Expand All @@ -38,7 +41,19 @@ class Tag extends Model
protected static function defineModel() : ResourceModel
{
$model = (new ResourceModel('tag', new MemoryDataSource()))
->addVariable('table', 'tag');
->addVariable('table', 'tag')
->setValidationModel(
new ValidationModel(
new ObjectValidator(
(object) [
'name' => new StringValidator(2, 10)
],
['name'],
false
)
),
'POST'
);

return $model;
}
Expand Down

0 comments on commit ae4cb8e

Please sign in to comment.