jasny / codeception-module
Codeception Module for Jasny MVC
Installs: 7 796
Dependents: 0
Suggesters: 1
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/jasny/codeception-module
Requires
- php: >=7.2.0
- codeception/codeception: ^4.0
- codeception/lib-innerbrowser: ^1.2
- jasny/http-message: ^1.3
- jasny/router: ^1.1
- psr/container: ^1.0
- symfony/http-client: ^5.0
Requires (Dev)
- codeception/module-rest: ^1.0
- jasny/controller: ^1.0
- jasny/error-handler: ^0.2
- jasny/php-code-quality: ^2.1
- jasny/view: ^1.1.0
- mouf/picotainer: ^1.1
- twig/twig: ^1.29
This package is auto-updated.
Last update: 2023-06-09 12:06:00 UTC
README
This module allows you to run tests using Jasny MVC.
Install
Via commandline:
composer require --dev jasny/codeception-module
Via composer.json:
{
  "require-dev": {
    "jasny/codeception-module": "^1.0"
  }
}
Config
- container: Path to file that returns a
Interop\Container\ContainerInterface.
class_name: FunctionalTester modules: enabled: - \Helper\Functional - \Jasny\Codeception\Module: container: tests/_data/container.php - REST: depends: \Jasny\Codeception\Module
Container
The container an object that takes care or depency injection. It must be an object the implements Interop\Container\ContainerInterface.
If you're project doesn't use an dependency injection container, you can use Picotainer,
which is automatically installed with this codeception module.
The container must contain an item for Jasny\RouterInterface.
Example of container.php using Picotainer.
use Mouf\Picotainer\Picotainer; use Jasny\Router; use Jasny\Router\Routes\Glob as Routes; use Jasny\RouterInterface; return new Picotainer([ RouterInterface::class => function() { return new Router(new Routes([ '/' => ['controller' => 'foo'], // ... ])); } ]);
The cointain may have a Psr\Http\Message\ServerRequestInterface and Psr\Http\Message\ResponseInterface item.
use Mouf\Picotainer\Picotainer; use Jasny\Router; use Jasny\Router\Routes\Glob as Routes; use Jasny\RouterInterface; use Jasny\HttpMessage\ServerRequest; use Jasny\HttpMessage\Response; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; return new new Picotainer([ RouterInterface::class => function() { return new Router(new Routes([ '/' => ['controller' => 'foo'], // ... ])); }, ServerRequestInterface::class => function() { return new ServerRequest(); }, ResponseInterface::class => function() { return new Response(); } ]);
Legacy code
The Jasny PSR-7 http message implementation is capable of dealing with legacy code by binding to the global environment.
This allows testing of code that accesses superglobals like $_GET and $_POST and outputs using echo and
headers().
Use withGlobalEnvironment(true) for both request and response object. The Codeception module will make sure
output buffering starts and everything is restored after each test.
use Mouf\Picotainer\Picotainer; use Jasny\Router; use Jasny\Router\Routes\Glob as Routes; use Jasny\RouterInterface; use Jasny\HttpMessage\ServerRequest; use Jasny\HttpMessage\Response; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; return new Picotainer([ RouterInterface::class => function() { return new Router(new Routes([ '/' => ['controller' => 'foo'], // ... ])); }, ServerRequestInterface::class => function() { return (new ServerRequest())->withGlobalEnvironment(true); }, ResponseInterface::class => function() { return (new Response())->withGlobalEnvironment(true); } ]);
Error handler
The container may also contain a Jasny Error Handler. If a fatal error is caught by the error handler, the output is typically a nice message intended for the end user. It doesn't contain any information about the error itself.
If the container has a Jasny\ErrorHandlerInterface object, it will output the error as debug information on a failed
test. To see the error use the --debug flag when running composer run.
use Mouf\Picotainer\Picotainer; use Jasny\Router; use Jasny\Router\Routes\Glob as Routes; use Jasny\RouterInterface; use Jasny\ErrorHandler; use Jasny\ErrorHandlerInterface; return new Picotainer([ RouterInterface::class => function($container) { $router = new Router(new Routes([ '/' => ['controller' => 'foo'], // ... ])); $errorHandler = $container->get(ErrorHandlerInterface::class); $router->add($errorHandler->asMiddleware()); return $router; }, ErrorHandlerInterface::class => function() { $errorHandler = new ErrorHandler(); $errorHandler->logUncaught(E_PARSE | E_ERROR | E_WARNING | E_USER_WARNING); $errorHandler->logUncaught(Exception::class); $errorHandler->logUncaught(Error::class); // PHP7 only return $errorHandler; }); ]);
API
- container - The container
- client - BrowserKit client