2017 © Pedro Peláez
 

library slimtestclient

Test client for Slim application.

image

slimtestclient/slimtestclient

Test client for Slim application.

  • Tuesday, July 25, 2017
  • by ushiboy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Slim Test Client

Build Status, (*1)

Unofficial test client for Slim Framework., (*2)

Usage

Basic usage

To get started, you first instantiate and configure the Slim application and it pass to SlimTest client. Then you run the client's request method and get the response object., (*3)

The response object has implements \Psr\Http\Message\ResponseInterface, and also it has getRawBody and getParsedBody methods as an extention., (*4)

The getRawBody method returns a content of the response body. If the Content-Type of the response is application/json, getParsedBody method returns the decoded JSON data., (*5)

<?php
class SampleTest extends \PHPUnit\Framework\TestCase
{
    public function testRequest()
    {
        $app = new \Slim\App();
        $app->get('/test', function($req, $res) {
            $res->getBody()->write('Hello, world!');
            return $res;
        });

        $client = new \SlimTest\Client($app);
        $response = $client->request('GET', '/test');
        $this->assertEquals('Hello, world!', $response->getRawBody());
    }
}

Request Body

When passing the request body, set it as an associative array in the 3rd argument and use it., (*6)

$client = new \SlimTest\Client($app);
$response = $client->request('POST', '/test', ['title' => 'hoge']);

Query String

When using the query string, add it to the URL of the 2nd argument and use it., (*7)

$client = new \SlimTest\Client($app);
$response = $client->request('GET', '/test?a=1&b=2');

If you want to add cookies or custom headers, use an associative array as the 4th argument and use it., (*8)

$client = new \SlimTest\Client($app);
// add cookie
$response = $client->request('GET', '/test', null, [
    'Cookie' => 'test=1;'
]);

// add custom header
$response = $client->request('GET', '/test', null, [
    'X-My-Custom' => 'test'
]);

Upload File

When uploading a file, set it as associative array in the 5th argument. The structure of an associative array is the same as $_FILES., (*9)

$client = new \SlimTest\Client($app);
$response = $client->request('POST', '/test', null, [
    'Content-Type' => 'multipart/form-data'
], [
    'uploadfile' => [
        'name' => 'test.txt',
        'tmp_name' => '/path/to/file',
        'size' => $yourFileSize,
        'error' => UPLOAD_ERR_OK,
        'type' => 'text/plain'
    ]
]);

// or use \SlimTest\Client::generateUploadFile method
$response = $client->request('POST', '/test', null, [
    'Content-Type' => 'multipart/form-data'
], [
    'uploadfile' => Client::generateUploadFile('/path/to/file')
]);

JSON

If you use the requestJson method, you can omit the Content-Type. In this case, the request body is treated as JSON data., (*10)

$client = new \SlimTest\Client($app);
$response = $client->requestJson('POST', '/test', ['title'=>'hoge']);

The Versions

25/07 2017

dev-master

9999999-dev https://github.com/ushiboy/slim-test-client

Test client for Slim application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

test slim

25/07 2017

0.3.0

0.3.0.0 https://github.com/ushiboy/slim-test-client

Test client for Slim application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

test slim

05/07 2017

0.2.0

0.2.0.0 https://github.com/ushiboy/slim-test-client

Test client for Slim application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

test slim

29/06 2017

0.1.0

0.1.0.0 https://github.com/ushiboy/slim-test-client

Test client for Slim application.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

test slim