/** * Test create() method returns results if query succeeds */ public function testCreateReturnsResult() { $mockPostId = 1; $mockCommentId = 1; $mockCommentatorId = 1; $mockInsertData = ['post_id' => $mockPostId, 'body' => 'somevalue', 'commentator_id' => $mockCommentatorId]; $mockSessionData = ['commentator_id' => $mockCommentatorId]; $mockSessionObject = m::mock(\stdClass::class); $mockSessionObject->shouldReceive('get')->with('commentator')->andReturn($mockSessionData); $mockApp = m::mock(\Silex\Application::class)->makePartial(); $mockDataObject = m::mock(CommentData::class, [$mockApp]); $mockDataObject->shouldReceive('getSession')->andReturn($mockSessionObject); $mockDataObject->shouldReceive('create')->with($mockInsertData)->andReturn($mockPostId); $object = new CommentApi($mockDataObject); $returned = $object->create($mockPostId, $mockInsertData); $this->assertSame($mockCommentId, $returned); }
/** * @param Application $app * @param $post_id int * * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function newComment(Application $app, $post_id) { $apiObject = new CommentApi(new CommentData($app)); try { $result = $apiObject->create($post_id, $_POST); } catch (\InvalidArgumentException $e) { $message = $e->getMessage(); } catch (\UnexpectedValueException $e) { $message = $e->getMessage(); } if (isset($message)) { $app['session']->getFlashBag()->add('message', $message); } if (!isset($result) || !$result) { $app['session']->getFlashBag()->add('message', 'Failed to add comment.'); return $this->viewReadPost($app, $post_id); } $app['session']->getFlashBag()->add('message', 'Comment added.'); return $this->viewReadPost($app, $post_id); }