80 lines
2.4 KiB
PHP
80 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Slovocast\Tests\Controller\User;
|
|
|
|
use Slovocast\Tests\TestCase;
|
|
use Slovocast\Domain\Repository\UserRepositoryInterface;
|
|
use Slovocast\Domain\Entity\User;
|
|
use Odan\Session\SessionInterface;
|
|
use Odan\Session\FlashInterface;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
class RegisterUserActionTest extends TestCase
|
|
{
|
|
protected function getUser(): User
|
|
{
|
|
return User::fromArray([
|
|
'email' => 'dave@slovocast.com',
|
|
'name' => 'Dave SH',
|
|
'password' => 'hashed_password'
|
|
]);
|
|
}
|
|
|
|
protected function createNewUserRequest(): Request
|
|
{
|
|
$user = $this->getUser();
|
|
return $this->createRequest('POST', '/users/register')
|
|
->withParsedBody([
|
|
'email' => $user->getEmail(),
|
|
'name' => $user->getName(),
|
|
'password' => $user->getPassword(),
|
|
'checked_password' => $user->getPassword()
|
|
]);
|
|
}
|
|
|
|
public function testSuccessfulRegistration(): void
|
|
{
|
|
$user = $this->getUser();
|
|
$app = $this->getAppInstance();
|
|
|
|
/** @var \DI\Container */
|
|
$container = $app->getContainer();
|
|
$userRepository = $this->prophesize(UserRepositoryInterface::class);
|
|
$userRepository->save($user)->willReturn(true);
|
|
$container->set(
|
|
UserRepositoryInterface::class,
|
|
$userRepository->reveal()
|
|
);
|
|
|
|
$request = $this->createNewUserRequest();
|
|
|
|
$response = $app->handle($request);
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
}
|
|
|
|
public function testFailingRegistration(): void
|
|
{
|
|
$user = $this->getUser();
|
|
|
|
$app = $this->getAppInstance();
|
|
|
|
/** @var $container \DI\Container */
|
|
$container = $app->getContainer();
|
|
|
|
$userRepository = $this->prophesize(UserRepositoryInterface::class);
|
|
$userRepository->save($user)->willReturn(false);
|
|
$container->set(
|
|
UserRepositoryInterface::class,
|
|
$userRepository->reveal()
|
|
);
|
|
|
|
$request = $this->createNewUserRequest();
|
|
|
|
$response = $app->handle($request);
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
$flash = $container->get(SessionInterface::class)->getFlash();
|
|
$this->assertEquals('Unable to register user.', $flash->get('error')[0]);
|
|
}
|
|
}
|