there4 / slim-test-helpers
Integration testing helpers for the Slim Framework
Installs: 253 094
Dependents: 4
Suggesters: 0
Security: 0
Stars: 60
Watchers: 13
Forks: 28
Open Issues: 6
Requires
- illuminate/database: >=4.0
- phpunit/dbunit: 2.*|3.*
- phpunit/phpunit: ^4.8|5.*|6.*
- slim/slim: ~3.1
Requires (Dev)
README
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.