public function setUpController()
 {
     $this->setUpRouter();
     $this->setUpListeners();
     $resource = new Resource();
     $events = $resource->getEventManager();
     $events->attach($this->listeners);
     $controller = $this->controller = new RestController('Api\\RestController');
     $controller->setResource($resource);
     $controller->setIdentifierName('id');
     $controller->setPageSize(3);
     $controller->setRoute('resource');
     $controller->setEvent($this->getEvent());
     $this->setUpContentNegotiation($controller);
 }
 public function testChildResourceObjectIdentifierMappingInCollectionsViaControllerReturn()
 {
     $this->setUpAlternateRouter();
     $resource = new Resource();
     $resource->getEventManager()->attach('fetchAll', function ($e) {
         return array((object) array('id' => 'luke', 'name' => 'Luke Skywalker'), (object) array('id' => 'leia', 'name' => 'Leia Organa'));
     });
     $controller = new RestController();
     $controller->setPluginManager($this->plugins);
     $controller->setResource($resource);
     $controller->setRoute('parent/child');
     $controller->setIdentifierName('child_id');
     $controller->setCollectionName('children');
     $r = new ReflectionObject($controller);
     $m = $r->getMethod('getIdentifier');
     $m->setAccessible(true);
     $uri = 'http://localhost.localdomain/api/parent/anakin/child';
     $request = new Request();
     $request->setUri($uri);
     $matches = $this->router->match($request);
     $this->assertInstanceOf('Zend\\Mvc\\Router\\RouteMatch', $matches);
     $this->assertEquals('anakin', $matches->getParam('id'));
     $this->assertNull($matches->getParam('child_id'));
     $this->assertEquals('parent/child', $matches->getMatchedRouteName());
     // Emulate url helper factory and inject route matches
     $this->helpers->get('url')->setRouteMatch($matches);
     $result = $controller->getList();
     $this->assertInstanceOf('ZF\\Hal\\Collection', $result);
     // Now, what happens if we render this?
     $model = new HalJsonModel();
     $model->setPayload($result);
     $json = $this->renderer->render($model);
     $test = json_decode($json);
     $this->assertObjectHasAttribute('_links', $test);
     $this->assertObjectHasAttribute('self', $test->_links);
     $this->assertObjectHasAttribute('href', $test->_links->self);
     $this->assertEquals('http://localhost.localdomain/api/parent/anakin/child', $test->_links->self->href);
     $this->assertObjectHasAttribute('_embedded', $test);
     $this->assertObjectHasAttribute('children', $test->_embedded);
     $this->assertInternalType('array', $test->_embedded->children);
     foreach ($test->_embedded->children as $child) {
         $this->assertObjectHasAttribute('_links', $child);
         $this->assertObjectHasAttribute('self', $child->_links);
         $this->assertObjectHasAttribute('href', $child->_links->self);
         $this->assertRegexp('#^http://localhost.localdomain/api/parent/anakin/child/[^/]+$#', $child->_links->self->href);
     }
 }
 /**
  * Create service with name
  *
  * @param ServiceLocatorInterface $controllers
  * @param string                  $name
  * @param string                  $requestedName
  * @return RestController
  * @throws ServiceNotCreatedException if listener specified is not a ListenerAggregate
  */
 public function createServiceWithName(ServiceLocatorInterface $controllers, $name, $requestedName)
 {
     $services = $controllers->getServiceLocator();
     $config = $services->get('Config');
     $config = $config['zf-rest'][$requestedName];
     if ($services->has($config['listener'])) {
         $listener = $services->get($config['listener']);
     } else {
         $listener = new $config['listener']();
     }
     if (!$listener instanceof ListenerAggregateInterface) {
         throw new ServiceNotCreatedException(sprintf('%s expects that the "listener" reference a service that implements Zend\\EventManager\\ListenerAggregateInterface; received %s', __METHOD__, is_object($listener) ? get_class($listener) : gettype($listener)));
     }
     $resourceIdentifiers = array(get_class($listener));
     if (isset($config['resource_identifiers'])) {
         if (!is_array($config['resource_identifiers'])) {
             $config['resource_identifiers'] = (array) $config['resource_identifiers'];
         }
         $resourceIdentifiers = array_merge($resourceIdentifiers, $config['resource_identifiers']);
     }
     $events = $services->get('EventManager');
     $events->attach($listener);
     $events->setIdentifiers($resourceIdentifiers);
     $resource = new Resource();
     $resource->setEventManager($events);
     $identifier = $requestedName;
     if (isset($config['identifier'])) {
         $identifier = $config['identifier'];
     }
     $controllerClass = isset($config['controller_class']) ? $config['controller_class'] : 'ZF\\Rest\\RestController';
     $controller = new $controllerClass($identifier);
     if (!$controller instanceof RestController) {
         throw new ServiceNotCreatedException(sprintf('"%s" must be an implementation of ZF\\Rest\\RestController', $controllerClass));
     }
     $controller->setEventManager($events);
     $controller->setResource($resource);
     $this->setControllerOptions($config, $controller);
     if (isset($config['entity_class'])) {
         $listener->setEntityClass($config['entity_class']);
     }
     if (isset($config['collection_class'])) {
         $listener->setCollectionClass($config['collection_class']);
     }
     return $controller;
 }