mileschou / slim-test
A simple test helper for Slim Framework 3
Installs: 7 318
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ^7.2 | ^8.0
- illuminate/database: ^6 | ^7 | ^8 | ^9
- slim/slim: ^3.4
- symfony/css-selector: ^5 | ^6
- symfony/dom-crawler: ^5 | ^6
Requires (Dev)
- ext-json: *
- behat/behat: ^3.10
- phpunit/phpunit: ^8.5 | ^9
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-10-24 21:43:29 UTC
README
A simple test helper for Slim Framework 3
The repository has some example in tests
folder. app.php is a definition use simple Slim router, SlimCaseTest.php is testing for SlimCase
class, and ClientTest.php is testing for Client
class. You can use Client
If you want to use PHPUnit style to write test, or use SlimCase
in Codeception style.
Installation with Composer
$ composer require --dev mileschou/slim-test
Using object in PHPUnit
First, prepare your Slim App in test code and pass to SlimCase
constructor.
use MilesChou\Slim\Test\SlimCase; class SlimAppTest extends PHPUnit_Framework_TestCase { public function setUp() { $app = require 'path/to/app.php'; $this->slimCase = new SlimCase($app); } }
Now, you can use Codeception Style to make assertion.
public function testSeeResponseOk() { // Arrange $url = '/will/return/ok'; // Act $this->slimCase->sendGET($url); // Assert $this->slimCase->seeResponseOk(); }
The visibility of Client
object in SlimCase
is public. That means you can use Client
like
$this->slimCase->client->get($url);
It's unsafe to access client object directly. The visibility will modify to
private
in the future.
Using trait in PHPUnit
You can use ClientTrait
or SlimCaseTrait
in PHPUnit, too. Here is an example:
use MilesChou\Slim\Test\SlimCaseTrait; class SlimAppTest extends PHPUnit_Framework_TestCase { use SlimCaseTrait; public function setUp() { $app = require 'path/to/app.php'; $this->setSlimApp($app); } public function tearDown() { $this->setSlimApp(null); } public function testSeeResponseOk() { // Arrange $url = '/will/return/ok'; // Act $this->sendGET($url); // Assert $this->seeResponseOk(); } }
Using in Behat
It's easy to using Slim Test in Behat. For example, I have a feature file
# features/app.feature Feature: an example testing use behat Scenario: Test assert response is okay Given a route named "/will/return/ok" When visit "/will/return/ok" Then I should see response okay
And implement context file easily
// features/bootstrap/FeatureContext.php use Behat\Behat\Context\Context; use Behat\Behat\Context\SnippetAcceptingContext; use MilesChou\Slim\Test\SlimCaseTrait; class FeatureContext implements Context, SnippetAcceptingContext { use SlimCaseTrait; public function __construct() { // bootstrap your slim app $app = require __DIR__ . '/../../app.php'; $this->setSlimApp($app); } /** @Given a route named :url */ public function aRouteNamed($url) { /** Do nothing */ } /** @When visit :url */ public function visit($url) { $this->sendGET($url); } /** @Then I should see response okay */ public function iShouldSeeResponseOkay() { $this->seeResponseOk(); } }
Tests
Execute all test suite use PHPUnit and Behat
$ php vendor/bin/phpunit
$ php vendor/bin/behat
You can use composer scripts, too
$ composer test
Run PHP built-in server if you want to check HTTP response via browser
$ php -S 0.0.0.0:8080 -t public
License
The Slim Test is licensed under the MIT license. See License File for more information.