2017 © Pedro Peláez
 

library slim-test-helpers

Integration testing helpers for the Slim Framework

image

there4/slim-test-helpers

Integration testing helpers for the Slim Framework

  • Friday, October 20, 2017
  • by craig-davis
  • Repository
  • 12 Watchers
  • 54 Stars
  • 134,378 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 29 Forks
  • 5 Open issues
  • 13 Versions
  • 6 % Grown

The README.md

Slim Test Helpers Build Status Code Climate Test Coverage

Integration testing helpers for the Slim Framework 3, (*1)

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

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., (*3)

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()., (*4)

// 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., (*5)

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., (*6)

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

The Versions

04/08 2015

v0.9.4

0.9.4.0 https://github.com/there4/slim-test-helpers

Integration testing helpers for the Slim Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

slim

22/11 2014

v0.9.3

0.9.3.0 https://github.com/there4/slim-test-helpers

Integration testing helpers for the Slim Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

slim

11/11 2014

0.9.2

0.9.2.0 https://github.com/there4/slim-test-helpers

Integration testing helpers for the Slim Framework

  Sources   Download

MIT

The Requires

 

slim slim testing

01/11 2014

0.9.1

0.9.1.0 https://github.com/there4/slim-test-helpers

Integration testing helpers for the Slim Framework

  Sources   Download

MIT

The Requires

 

slim