public function testIndexItemExists() { // create mocks $apiMethods = array('getUserId', 'getSystemValue', 'add3rdPartyScript', 'addStyle', 'addScript'); $api = $this->getAPIMock($apiMethods); $api->expects($this->any())->method('getUserId')->will($this->returnValue('richard')); $item = new Item(); $item->setUser('user'); $item->setPath('/path'); $item->setId(3); $item->setName('name'); $itemMapperMock = $this->getMock('ItemMapper', array('findByUserId')); $itemMapperMock->expects($this->any())->method('findByUserId')->will($this->returnValue($item)); $controller = new ItemController($api, null, $itemMapperMock); $response = $controller->index(); $params = $response->getParams(); $this->assertEquals($item, $params['item']); }
/** * @CSRFExcemption * @IsAdminExcemption * @IsSubAdminExcemption * * @brief renders the index page * @param array $urlParams: an array with the values, which were matched in * the routes file * @return an instance of a Response implementation */ public function index($urlParams = array()) { // thirdparty stuff $this->api->add3rdPartyScript('angular/angular.min'); // your own stuff $this->api->addStyle('style'); $this->api->addStyle('animation'); $this->api->addScript('app'); // example database access // check if an entry with the current user is in the database, if not // create a new entry try { $item = $this->itemMapper->findByUserId($this->api->getUserId()); } catch (DoesNotExistException $e) { $item = new Item(); $item->setUser($this->api->getUserId()); $item->setPath('/home/path'); $item->setName('john'); $this->itemMapper->save($item); } $templateName = 'main'; $params = array('somesetting' => $this->api->getSystemValue('somesetting'), 'item' => $item); return $this->render($templateName, $params); }