Add the Prophecy PhpUnit library, fix the Register user action, test that the flash message also adds failure.
This commit is contained in:
parent
d6bb157b73
commit
ba5f68aa6f
@ -15,7 +15,8 @@
|
|||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^11.1",
|
"phpunit/phpunit": "^11.1",
|
||||||
"phpspec/prophecy": "^1.19"
|
"phpspec/prophecy": "^1.19",
|
||||||
|
"phpspec/prophecy-phpunit": "^2.2"
|
||||||
},
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
107
app/composer.lock
generated
107
app/composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "ef0dabb2ed1f62b79526b77f3db35cd4",
|
"content-hash": "e53cd4e029d379845d39165f4cc34653",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "dflydev/dot-access-data",
|
"name": "dflydev/dot-access-data",
|
||||||
@ -467,59 +467,6 @@
|
|||||||
],
|
],
|
||||||
"time": "2024-01-28T23:22:08+00:00"
|
"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",
|
"name": "monolog/monolog",
|
||||||
"version": "3.6.0",
|
"version": "3.6.0",
|
||||||
@ -2742,6 +2689,58 @@
|
|||||||
},
|
},
|
||||||
"time": "2024-02-29T11:52:51+00:00"
|
"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",
|
"name": "phpstan/phpdoc-parser",
|
||||||
"version": "1.29.0",
|
"version": "1.29.0",
|
||||||
|
@ -6,11 +6,13 @@ use Psr\Http\Message\ResponseInterface as Response;
|
|||||||
use Slovocast\Controller\Controller;
|
use Slovocast\Controller\Controller;
|
||||||
use Slovocast\Domain\Entity\User;
|
use Slovocast\Domain\Entity\User;
|
||||||
use Slovocast\Domain\Repository\UserRepositoryInterface;
|
use Slovocast\Domain\Repository\UserRepositoryInterface;
|
||||||
|
use Odan\Session\SessionInterface;
|
||||||
|
|
||||||
class RegisterUserAction extends Controller
|
class RegisterUserAction extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected UserRepositoryInterface $userRepository
|
protected UserRepositoryInterface $userRepository,
|
||||||
|
protected SessionInterface $session
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
public function respond(): Response
|
public function respond(): Response
|
||||||
@ -34,6 +36,9 @@ class RegisterUserAction extends Controller
|
|||||||
if ($result) {
|
if ($result) {
|
||||||
return $this->render('user/success.twig');
|
return $this->render('user/success.twig');
|
||||||
} else {
|
} else {
|
||||||
|
$this->session
|
||||||
|
->getFlash()
|
||||||
|
->add('error', "Unable to register user.");
|
||||||
return $this->render('user/register.twig')->withStatus(400);
|
return $this->render('user/register.twig')->withStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,9 @@ namespace Slovocast\Tests\Controller\User;
|
|||||||
use Slovocast\Tests\TestCase;
|
use Slovocast\Tests\TestCase;
|
||||||
use Slovocast\Domain\Repository\UserRepositoryInterface;
|
use Slovocast\Domain\Repository\UserRepositoryInterface;
|
||||||
use Slovocast\Domain\Entity\User;
|
use Slovocast\Domain\Entity\User;
|
||||||
|
use Odan\Session\SessionInterface;
|
||||||
|
use Odan\Session\FlashInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
|
||||||
class RegisterUserActionTest extends TestCase
|
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
|
public function testSuccessfulRegistration(): void
|
||||||
{
|
{
|
||||||
$user = $this->getUser();
|
$user = $this->getUser();
|
||||||
|
|
||||||
$prophecy = $this->getProphet()
|
|
||||||
->prophesize(UserRepositoryInterface::class);
|
|
||||||
$prophecy->save($user)->willReturn(true);
|
|
||||||
|
|
||||||
$app = $this->getAppInstance();
|
$app = $this->getAppInstance();
|
||||||
|
|
||||||
/** @var \DI\Container */
|
/** @var \DI\Container */
|
||||||
$container = $app->getContainer();
|
$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([
|
$request = $this->createNewUserRequest();
|
||||||
'email' => 'dave@slovocast.com',
|
|
||||||
'name' => 'Dave SH',
|
|
||||||
'password' => 'hashed_password',
|
|
||||||
'checked_password' => 'hashed_password'
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response = $app->handle($request);
|
$response = $app->handle($request);
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
@ -45,25 +55,25 @@ class RegisterUserActionTest extends TestCase
|
|||||||
public function testFailingRegistration(): void
|
public function testFailingRegistration(): void
|
||||||
{
|
{
|
||||||
$user = $this->getUser();
|
$user = $this->getUser();
|
||||||
$prophecy = $this->getProphet()
|
|
||||||
->prophesize(UserRepositoryInterface::class);
|
|
||||||
$prophecy->save($user)->willReturn(false);
|
|
||||||
|
|
||||||
$app = $this->getAppInstance();
|
$app = $this->getAppInstance();
|
||||||
|
|
||||||
/** @var $container \DI\Container */
|
/** @var $container \DI\Container */
|
||||||
$container = $app->getContainer();
|
$container = $app->getContainer();
|
||||||
$container->set(UserRepositoryInterface::class, $prophecy->reveal());
|
|
||||||
|
|
||||||
$request = $this->createRequest('POST', '/users/register')
|
$userRepository = $this->prophesize(UserRepositoryInterface::class);
|
||||||
->withParsedBody([
|
$userRepository->save($user)->willReturn(false);
|
||||||
'email' => 'dave@slovocast.com',
|
$container->set(
|
||||||
'name' => 'Dave SH',
|
UserRepositoryInterface::class,
|
||||||
'password' => 'hashed_password',
|
$userRepository->reveal()
|
||||||
'checked_password' => 'hashed_password'
|
);
|
||||||
]);
|
|
||||||
|
$request = $this->createNewUserRequest();
|
||||||
|
|
||||||
$response = $app->handle($request);
|
$response = $app->handle($request);
|
||||||
$this->assertEquals(400, $response->getStatusCode());
|
$this->assertEquals(400, $response->getStatusCode());
|
||||||
|
|
||||||
|
$flash = $container->get(SessionInterface::class)->getFlash();
|
||||||
|
$this->assertEquals('Unable to register user.', $flash->get('error')[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
namespace Slovocast\Tests;
|
namespace Slovocast\Tests;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
|
use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
|
||||||
use Prophecy\Prophet;
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Slim\App;
|
use Slim\App;
|
||||||
use Slim\Psr7\Factory\StreamFactory;
|
use Slim\Psr7\Factory\StreamFactory;
|
||||||
@ -18,6 +18,8 @@ use Slovocast\Bootstrap;
|
|||||||
*/
|
*/
|
||||||
class TestCase extends PHPUnit_TestCase
|
class TestCase extends PHPUnit_TestCase
|
||||||
{
|
{
|
||||||
|
use ProphecyTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate an instance of the Application
|
* Generate an instance of the Application
|
||||||
*/
|
*/
|
||||||
@ -58,9 +60,4 @@ class TestCase extends PHPUnit_TestCase
|
|||||||
$stream
|
$stream
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getProphet(): Prophet
|
|
||||||
{
|
|
||||||
return new Prophet();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user