/** * @param Application $app * * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function newPost(Application $app) { $apiObject = new PostApi(new PostData($app)); try { $result = $apiObject->create($_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 post.'); return $this->index($app); } return $this->index($app); }
/** * Test create() method returns true if query succeeds */ public function testCreateReturnsPostIdIfQuerySucceeds() { $mockPostId = 1; $mockInsertData = ['title' => 'somevalue', 'body' => 'somevalue']; $mockSessionData = ['author_id' => 1]; $mockInsertPostData = ['author_id' => 1, 'title' => 'somevalue']; $mockInsertContentData = ['post_id' => $mockPostId, 'body' => 'somevalue']; $mockSessionObject = m::mock(\stdClass::class); $mockSessionObject->shouldReceive('get')->with('author')->andReturn($mockSessionData); $mockApp = m::mock(\Silex\Application::class)->makePartial(); $mockDataObject = m::mock(PostData::class, [$mockApp]); $mockDataObject->shouldReceive('getSession')->andReturn($mockSessionObject); $mockDataObject->shouldReceive('create')->with($mockInsertPostData)->andReturn($mockPostId); $mockDataObject->shouldReceive('createContent')->with($mockInsertContentData)->andReturn(true); $object = new PostApi($mockDataObject); $returned = $object->create($mockInsertData); $this->assertSame($mockPostId, $returned); }