there4/slim-test-helpers

Integration testing helpers for the Slim Framework
257,869 59
Install
composer require there4/slim-test-helpers
Latest Version:v2.1.3
Laravel:4 · 5
License:MIT
Last Updated:Mar 26, 2024
Links: GitHub  ·  Packagist
Maintainer: there4

Slim Test Helpers Build Status Code Climate Test Coverage

Integration testing helpers for the Slim Framework 3

For a full example, please see the companion repo at there4/slim-unit-testing-example.

Example

Here's a test for a very simple endpoint that returns the version from the application config. We're asserting that Slim responded with a 200 and that the version matches what we expect.

class VersionTest extends LocalWebTestCase
{
    public function testVersion()
    {
        $this->client->get('/version');
        $this->assertEquals(200, $this->client->response->getStatusCode());
        $this->assertEquals($this->app->config('version'), $this->client->response->getBody());
    }
}

Here is an example on how to pass data to a POST endpoint in a test case and retrieve it later in the endpoint. We are passing encoded JSON data in the body of the request. The data is retrieved in the endpoint using $app->request->getBody().

// test file
class UserTest extends LocalWebTestCase
{
    public function testVersion()
    {
        ......
        $data = array("user" => 1);
        $data = json_encode($data);
        $this->client->post('/user', $data);
        ......
    }
}

// endpoint file
.....
$app->post('/user', function() use ($app) {
    .....
    $data = $app->request->getBody();
    $data = json_decode($data, true);
    ......
});

Example with DbUnit

If you wish to use Database fixture, use class WebDbTestCase. Caution: Make sure the names you use for you fixture models won't conflict with your actual DB tables.

class LocalDbWebTestCase extends \There4\Slim\Test\WebDbTestCase
{
    /**
     * You must implement this method
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
     */
    public function getDataSet()
    {
        return $this->createFlatXMLDataSet(
            dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fixture.xml'
        );
    }
}

Setup

You'll need a bootstrap file for phpunit that can instantiate your Slim application. You can see [an example boostrap] in the sample app.

You'll implement your own getSlimInstance() method that returns an instance of your app by extending the WebTestCase helper.

Related Packages

itsgoingd/slim-facades

"Static" interface for various Slim features

57,243 74
itsgoingd/slim-services

Service manager for Slim compatible with Laravel packages

15,655 75
mileschou/slim-test

A simple test helper for Slim Framework 3

9,525 7
havenshen/slim-born

Slim Framework 3 skeleton application has authentication.

1,752 192
duxweb/dux-lite

The lightweight framework based on slim php

1,061 17

Version History

Version Released Laravel License
v2.1.3 >=4.0 MIT
v2.1.2 >=4.0 MIT
v2.1.1 >=4.0 MIT
v2.1 >=4.0 MIT
v0.9.7 >=4.0 MIT