/** * Test fetchAll() method returns array */ public function testFetchAllReturnsData() { $mockPostId = 1; $mockData = ['body' => 'somevalue']; $mockList = [['commentator_id' => 1]]; $mockReturnedRecords = [['commentator_id' => 1, 'body' => 'somevalue']]; $mockApp = m::mock(\Silex\Application::class)->makePartial(); $mockCommentatorObject = m::mock(\stdClass::class); $mockCommentatorObject->shouldReceive('fetchCommentatorBasicDataById')->with(1)->andReturn($mockData); $mockFactoryObject = m::mock(CommentatorFactory::class, [$mockApp]); $mockFactoryObject->shouldReceive('getNewCommentator')->andReturn($mockCommentatorObject); $mockDataObject = m::mock(CommentData::class, [$mockApp]); $mockDataObject->shouldReceive('fetchCommentsByPostId')->with($mockPostId)->andReturn($mockList); $object = new CommentApi($mockDataObject); $returned = $object->fetchAll($mockPostId, $mockFactoryObject); $this->assertSame($mockReturnedRecords, $returned); }
/** * @param Application $app * @param $post_id * * @return Response */ public function viewReadPost(Application $app, $post_id) { $requestResponseCode = Response::HTTP_OK; // Filter and validation $id = filter_var($post_id, FILTER_VALIDATE_INT); if ($id === false) { $message = 'Queried id must be a number.'; $app['monolog']->addError('Integer filtering returned false. ' . $message); $requestResponseCode = Response::HTTP_BAD_REQUEST; } // Fetch post data $apiObject = new PostApi(new PostData($app)); try { $post = $apiObject->fetch($id); if (!$post) { $requestResponseCode = Response::HTTP_BAD_REQUEST; } } catch (\InvalidArgumentException $e) { $app['monolog']->addError(sprintf(static::MESSAGE_CAUGHT_EXCEPTION, $e->getMessage(), $e->getCode())); $message = 'Invalid query.'; $requestResponseCode = Response::HTTP_BAD_REQUEST; } catch (\UnexpectedValueException $e) { $app['monolog']->addError(sprintf(static::MESSAGE_CAUGHT_EXCEPTION, $e->getMessage(), $e->getCode())); $message = 'Failed to retrieve data.'; $requestResponseCode = Response::HTTP_BAD_REQUEST; } // Fetch comment data $apiCommentObject = new CommentApi(new CommentData($app)); try { $comments = $apiCommentObject->fetchAll($post['post_id'], new CommentatorFactory($app)); //todo: need authorship for comments } catch (\InvalidArgumentException $e) { $message = $e->getMessage(); $requestResponseCode = Response::HTTP_BAD_REQUEST; } catch (\UnexpectedValueException $e) { $message = $e->getMessage(); $requestResponseCode = Response::HTTP_BAD_REQUEST; } // Content Display // Render page sections $pageArgs = ['loggedIn' => $this->isLoggedIn($app), 'admin' => $this->isAdmin($app), 'message' => false, 'post' => false, 'comments' => false, 'addComment' => false]; if (isset($message)) { $app['session']->getFlashBag()->add('message', $message); } //todo authorship display if (!is_null($app['session']->get('commentator'))) { $pageArgs['addComment'] = true; } if (isset($post) && is_array($post)) { $pageArgs['post'] = $post; $pageArgs['post_id'] = $post['post_id']; } if (isset($comments) && is_array($comments)) { $pageArgs['comments'] = $comments; } // Return page $content = $app['twig']->render('post.twig', $pageArgs); return new Response($content, $requestResponseCode); }