/** * Test create() returns true if query succeeds */ public function testCreateReturnsIdIfSuccessful() { $mockId = 1; $mockUsername = '******'; $mockPassword = '******'; $mockHash = 'gibberish'; $mockProvidedData = ['username' => $mockUsername, 'password' => $mockPassword, 'password_confirm' => $mockPassword]; $mockInsertData = ['username' => $mockUsername, 'password_hash' => $mockHash]; $mockPasswordObject = m::mock(Password::class); $mockPasswordObject->shouldReceive('createHashedPassword')->with($mockPassword)->andReturn($mockPasswordObject); $mockPasswordObject->shouldReceive('getHash')->andReturn($mockHash); $mockApp = m::mock(\Silex\Application::class)->makePartial(); $mockDataObject = m::mock(CommentatorData::class, [$mockApp]); $mockDataObject->shouldReceive('doesUsernameExist')->with($mockUsername)->andReturn(false); $mockDataObject->shouldReceive('create')->with($mockInsertData)->andReturn($mockId); $object = new CommentatorApi($mockDataObject); $object->setPasswordObject($mockPasswordObject); $returned = $object->create($mockProvidedData); $this->assertSame($mockId, $returned); }
/** * @param Application $app * @param $user string * * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function newUser(Application $app, $user) { switch ($user) { case 'author': $apiObject = new AuthorApi(new AuthorData($app)); $resultFalseMessage = 'Found an existing author, unable to register another.'; $resultTrueMessage = 'Added author.'; break; case 'commentator': $apiObject = new CommentatorApi(new CommentatorData($app)); $resultFalseMessage = 'Failed registering commentary user. Reason: unknown.'; $resultTrueMessage = 'Successfully registered to comment.'; break; default: $app['session']->getFlashBag()->add('message', 'Unknown user creation attempt.'); return $this->index($app); } $apiObject->setPasswordObject(new Password()); try { $result = $apiObject->create($_POST); } catch (\InvalidArgumentException $e) { $app['session']->getFlashBag()->add('message', $e->getMessage()); return $this->viewRegister($app, $user); } catch (\UnexpectedValueException $e) { $app['session']->getFlashBag()->add('message', $e->getMessage()); return $this->viewRegister($app, $user); } if (!isset($result) || !$result) { $app['session']->getFlashBag()->add('message', $resultFalseMessage); return $this->index($app); } $app['session']->getFlashBag()->add('message', $resultTrueMessage); return $this->index($app); }