Skip to content

Instantly share code, notes, and snippets.

@jdelisle
Last active August 29, 2015 14:04
Show Gist options
  • Save jdelisle/0fda5fee208ea35e9117 to your computer and use it in GitHub Desktop.
Save jdelisle/0fda5fee208ea35e9117 to your computer and use it in GitHub Desktop.
Testing a resource's fetchAll method returning a collection object to be serialized in JSON
<?php
namespace API\V1\Rest\Group;
class GroupCollection
{
public $data;
public $count;
public $total_items;
}
<?php
namespace API\V1\Rest\Group;
class GroupEntity
{
public $id;
}
<?php
namespace API\V1\Rest\Group;
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;
class GroupResource extends AbstractResourceListener
{
/**
* Fetch all or a subset of resources
*
* @param array $params
* @return ApiProblem|mixed
*/
public function fetchAll($params = array())
{
$entities = $this->fakeEntities(10);
$count = count($entities);
$totalCount = $count * 10;
$collection = new GroupCollection();
$collection->data = $entities;
$collection->count = $count;
$collection->total_items = $totalCount;
return $collection;
}
public function fakeEntities($count)
{
$entities = array();
for ($i = 1; $i <= $count; $i++) {
$entity = new GroupEntity();
$entity->id = $i;
$entities[] = $entity;
}
return $entities;
}
}
<?php
return array(
'zf-rest' => array(
'API\\V1\\Rest\\Group\\Controller' => array(
'listener' => 'API\\V1\\Rest\\Group\\GroupResource',
'route_name' => 'api.rest.group',
'route_identifier_name' => 'group_id',
'collection_name' => 'group',
'entity_http_methods' => array(),
'collection_http_methods' => array(
0 => 'GET',
),
'collection_query_whitelist' => array(),
'page_size' => 25,
'page_size_param' => null,
'entity_class' => 'API\\V1\\Rest\\Entity\\GroupEntity',
'collection_class' => 'API\\V1\\Rest\\Group\\GroupCollection',
'service_name' => 'Group',
),
),
'zf-hal' => array(
'metadata_map' => array(
'API\\V1\\Rest\\Entity\\GroupEntity' => array(
'entity_identifier_name' => 'id',
'route_name' => 'api.rest.group',
'route_identifier_name' => 'group_id',
'hydrator' => 'Zend\\Stdlib\\Hydrator\\ObjectProperty',
),
'API\\V1\\Rest\\Group\\GroupCollection' => array(
'entity_identifier_name' => 'id',
'route_name' => 'api.rest.group',
'route_identifier_name' => 'group_id',
'is_collection' => true,
),
),
),
);
{
"data" : [
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
],
"count" : 5,
"total_items" : 50
}
{
"trace" : [
{
"file" : "PATH\\apigility\\vendor\\zfcampus\\zf-hal\\src\\Plugin\\Hal.php",
"line" : 807,
"function" : "__construct",
"class" : "ZF\\Hal\\Collection",
"type" : "->",
"args" : [
{
"data" : [
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
],
"count" : 5,
"total_items" : 50
}
]
},
{
"file" : "PATH\\apigility\\vendor\\zfcampus\\zf-hal\\src\\Plugin\\Hal.php",
"line" : 696,
"function" : "createCollectionFromMetadata",
"class" : "ZF\\Hal\\Plugin\\Hal",
"type" : "->",
"args" : [
{
"data" : [
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
],
"count" : 5,
"total_items" : 50
}, {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zfcampus\\zf-hal\\src\\Plugin\\Hal.php",
"line" : 757,
"function" : "createEntityFromMetadata",
"class" : "ZF\\Hal\\Plugin\\Hal",
"type" : "->",
"args" : [
{
"data" : [
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
],
"count" : 5,
"total_items" : 50
}, {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zfcampus\\zf-rest\\src\\RestController.php",
"line" : 476,
"function" : "createEntity",
"class" : "ZF\\Hal\\Plugin\\Hal",
"type" : "->",
"args" : [
{
"data" : [
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
],
"count" : 5,
"total_items" : 50
}, "api.rest.group", "group_id"]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\Mvc\\Controller\\AbstractRestfulController.php",
"line" : 368,
"function" : "getList",
"class" : "ZF\\Rest\\RestController",
"type" : "->",
"args" : []
},
{
"file" : "PATH\\apigility\\vendor\\zfcampus\\zf-rest\\src\\RestController.php",
"line" : 275,
"function" : "onDispatch",
"class" : "Zend\\Mvc\\Controller\\AbstractRestfulController",
"type" : "->",
"args" : [{}
]
},
{
"function" : "onDispatch",
"class" : "ZF\\Rest\\RestController",
"type" : "->",
"args" : [{}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\EventManager\\EventManager.php",
"line" : 468,
"function" : "call_user_func",
"args" : [[{}, "onDispatch"], {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\EventManager\\EventManager.php",
"line" : 207,
"function" : "triggerListeners",
"class" : "Zend\\EventManager\\EventManager",
"type" : "->",
"args" : ["dispatch", {}, {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\Mvc\\Controller\\AbstractController.php",
"line" : 117,
"function" : "trigger",
"class" : "Zend\\EventManager\\EventManager",
"type" : "->",
"args" : ["dispatch", {}, {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\Mvc\\Controller\\AbstractRestfulController.php",
"line" : 300,
"function" : "dispatch",
"class" : "Zend\\Mvc\\Controller\\AbstractController",
"type" : "->",
"args" : [{}, {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\Mvc\\DispatchListener.php",
"line" : 114,
"function" : "dispatch",
"class" : "Zend\\Mvc\\Controller\\AbstractRestfulController",
"type" : "->",
"args" : [{}, {}
]
},
{
"function" : "onDispatch",
"class" : "Zend\\Mvc\\DispatchListener",
"type" : "->",
"args" : [{}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\EventManager\\EventManager.php",
"line" : 468,
"function" : "call_user_func",
"args" : [[{}, "onDispatch"], {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\EventManager\\EventManager.php",
"line" : 207,
"function" : "triggerListeners",
"class" : "Zend\\EventManager\\EventManager",
"type" : "->",
"args" : ["dispatch", {}, {}
]
},
{
"file" : "PATH\\apigility\\vendor\\zendframework\\zendframework\\library\\Zend\\Mvc\\Application.php",
"line" : 313,
"function" : "trigger",
"class" : "Zend\\EventManager\\EventManager",
"type" : "->",
"args" : ["dispatch", {}, {}
]
},
{
"file" : "PATH\\apigility\\public\\index.php",
"line" : 38,
"function" : "run",
"class" : "Zend\\Mvc\\Application",
"type" : "->",
"args" : []
}
],
"type" : "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title" : "Internal Server Error",
"status" : 500,
"detail" : "ZF\\Hal\\Collection::__construct expects an array or Traversable; received \"API\\V1\\Rest\\Group\\GroupCollection\""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment