Add the Prophecy PhpUnit library, fix the Register user action, test that the flash message also adds failure.

This commit is contained in:
Dave Smith-Hayes 2024-06-03 22:22:39 -04:00
parent d6bb157b73
commit ba5f68aa6f
5 changed files with 97 additions and 85 deletions

View File

@ -15,7 +15,8 @@
},
"require-dev": {
"phpunit/phpunit": "^11.1",
"phpspec/prophecy": "^1.19"
"phpspec/prophecy": "^1.19",
"phpspec/prophecy-phpunit": "^2.2"
},
"authors": [
{

107
app/composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "ef0dabb2ed1f62b79526b77f3db35cd4",
"content-hash": "e53cd4e029d379845d39165f4cc34653",
"packages": [
{
"name": "dflydev/dot-access-data",
@ -467,59 +467,6 @@
],
"time": "2024-01-28T23:22:08+00:00"
},
{
"name": "middlewares/php-session",
"version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/middlewares/php-session.git",
"reference": "089ea6929435d6f6f26e61a4f8ab682544294c31"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/middlewares/php-session/zipball/089ea6929435d6f6f26e61a4f8ab682544294c31",
"reference": "089ea6929435d6f6f26e61a4f8ab682544294c31",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.0",
"laminas/laminas-diactoros": "^2.3",
"middlewares/utils": "^3.0",
"oscarotero/php-cs-fixer-config": "^1.0",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8|^9",
"squizlabs/php_codesniffer": "^3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Middlewares\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Middleware to start php sessions using the request data",
"homepage": "https://github.com/middlewares/php-session",
"keywords": [
"http",
"middleware",
"psr-15",
"psr-7",
"server",
"session"
],
"support": {
"issues": "https://github.com/middlewares/php-session/issues",
"source": "https://github.com/middlewares/php-session/tree/v3.1.1"
},
"time": "2022-03-13T01:22:27+00:00"
},
{
"name": "monolog/monolog",
"version": "3.6.0",
@ -2742,6 +2689,58 @@
},
"time": "2024-02-29T11:52:51+00:00"
},
{
"name": "phpspec/prophecy-phpunit",
"version": "v2.2.0",
"source": {
"type": "git",
"url": "https://github.com/phpspec/prophecy-phpunit.git",
"reference": "16e1247e139434bce0bac09848bc5c8d882940fc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpspec/prophecy-phpunit/zipball/16e1247e139434bce0bac09848bc5c8d882940fc",
"reference": "16e1247e139434bce0bac09848bc5c8d882940fc",
"shasum": ""
},
"require": {
"php": "^7.3 || ^8",
"phpspec/prophecy": "^1.18",
"phpunit/phpunit": "^9.1 || ^10.1 || ^11.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
},
"autoload": {
"psr-4": {
"Prophecy\\PhpUnit\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Christophe Coevoet",
"email": "stof@notk.org"
}
],
"description": "Integrating the Prophecy mocking library in PHPUnit test cases",
"homepage": "http://phpspec.net",
"keywords": [
"phpunit",
"prophecy"
],
"support": {
"issues": "https://github.com/phpspec/prophecy-phpunit/issues",
"source": "https://github.com/phpspec/prophecy-phpunit/tree/v2.2.0"
},
"time": "2024-03-01T08:33:58+00:00"
},
{
"name": "phpstan/phpdoc-parser",
"version": "1.29.0",

View File

@ -6,11 +6,13 @@ use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Controller\Controller;
use Slovocast\Domain\Entity\User;
use Slovocast\Domain\Repository\UserRepositoryInterface;
use Odan\Session\SessionInterface;
class RegisterUserAction extends Controller
{
public function __construct(
protected UserRepositoryInterface $userRepository
protected UserRepositoryInterface $userRepository,
protected SessionInterface $session
) { }
public function respond(): Response
@ -34,6 +36,9 @@ class RegisterUserAction extends Controller
if ($result) {
return $this->render('user/success.twig');
} else {
$this->session
->getFlash()
->add('error', "Unable to register user.");
return $this->render('user/register.twig')->withStatus(400);
}

View File

@ -5,6 +5,9 @@ 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
{
@ -17,26 +20,33 @@ class RegisterUserActionTest extends TestCase
]);
}
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();
$prophecy = $this->getProphet()
->prophesize(UserRepositoryInterface::class);
$prophecy->save($user)->willReturn(true);
$app = $this->getAppInstance();
/** @var \DI\Container */
$container = $app->getContainer();
$container->set(UserRepositoryInterface::class, $prophecy->reveal());
$userRepository = $this->prophesize(UserRepositoryInterface::class);
$userRepository->save($user)->willReturn(true);
$container->set(
UserRepositoryInterface::class,
$userRepository->reveal()
);
$request = $this->createRequest('POST', '/users/register')->withParsedBody([
'email' => 'dave@slovocast.com',
'name' => 'Dave SH',
'password' => 'hashed_password',
'checked_password' => 'hashed_password'
]);
$request = $this->createNewUserRequest();
$response = $app->handle($request);
$this->assertEquals(200, $response->getStatusCode());
@ -45,25 +55,25 @@ class RegisterUserActionTest extends TestCase
public function testFailingRegistration(): void
{
$user = $this->getUser();
$prophecy = $this->getProphet()
->prophesize(UserRepositoryInterface::class);
$prophecy->save($user)->willReturn(false);
$app = $this->getAppInstance();
/** @var $container \DI\Container */
$container = $app->getContainer();
$container->set(UserRepositoryInterface::class, $prophecy->reveal());
$request = $this->createRequest('POST', '/users/register')
->withParsedBody([
'email' => 'dave@slovocast.com',
'name' => 'Dave SH',
'password' => 'hashed_password',
'checked_password' => 'hashed_password'
]);
$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]);
}
}

View File

@ -3,7 +3,7 @@
namespace Slovocast\Tests;
use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
use Prophecy\Prophet;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Psr7\Factory\StreamFactory;
@ -18,6 +18,8 @@ use Slovocast\Bootstrap;
*/
class TestCase extends PHPUnit_TestCase
{
use ProphecyTrait;
/**
* Generate an instance of the Application
*/
@ -58,9 +60,4 @@ class TestCase extends PHPUnit_TestCase
$stream
);
}
public function getProphet(): Prophet
{
return new Prophet();
}
}