/** * @param Application $app * * @return Response */ public function index(Application $app) { // preset $requestResponseCode = Response::HTTP_OK; // Fetch Post list $apiObject = new PostApi(new PostData($app)); try { $result = $apiObject->fetchAll($app); } catch (\InvalidArgumentException $e) { $message = $e->getMessage(); $requestResponseCode = Response::HTTP_BAD_REQUEST; } catch (\UnexpectedValueException $e) { $message = $e->getMessage(); $requestResponseCode = Response::HTTP_BAD_REQUEST; } // Render page sections $pageArgs = ['loggedIn' => $this->isLoggedIn($app), 'admin' => $this->isAdmin($app), 'message' => false, 'addPost' => false, 'posts' => false]; if (isset($message)) { $app['session']->getFlashBag()->add('message', $message); } //todo authorship display if ($this->isAdmin($app)) { $pageArgs['addPost'] = true; } if (isset($result) && is_array($result)) { $pageArgs['posts'] = $result; } // Return page $content = $app['twig']->render('index.twig', $pageArgs); return new Response($content, $requestResponseCode); //todo: collection of objects with individual authorship rather than just array loop }
/** * Test fetchAll() method returns array */ public function testFetchAllReturnsData() { $mockData = ['title' => 'somevalue', 'body' => 'somevalue']; $mockApp = m::mock(\Silex\Application::class)->makePartial(); $mockDataObject = m::mock(PostData::class, [$mockApp]); $mockDataObject->shouldReceive('fetchPosts')->andReturn($mockData); $object = new PostApi($mockDataObject); $returned = $object->fetchAll(); $this->assertSame($mockData, $returned); }