dbeurive/slim-requester

This package contains a tool that allows you to perform requests to your Slim application by bypassing the WEB server.


License
CC-BY-4.0

Documentation

Introduction

This package contains a "requester" the Slim framework.

The requester allows you to perform requests over your Slim application without the need for a WEB server. This is particularly useful when you want to automate unit tests. Indeed, while you are unit-testing your application's logic, you don't want to test the WEB server's configuration.

Please note that this package is a work in progress, since new features will be added.

License

This code is published under the following license:

Creative Commons Attribution 4.0 International Public License

See the file LICENSE.TXT

Installation

From the command line:

composer require dbeurive/slim-requester

Or, from within the file composer.json:

"require": {
    "dbeurive/slim-requester": "*"
}

Synopsis

Create a the Slim application:

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use dbeurive\Slim\Requester;

// Create your Slim application

$configuration = array(/* your configuration */);
$application = new \Slim\App($configuration);

$application->get('/get/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");
    return $response;
});

$application->post('/post', function (Request $request, Response $response) {
    $data = $request->getParsedBody();
    $firstName = filter_var($data['firstname'], FILTER_SANITIZE_STRING);
    $lastName  = filter_var($data['lastname'], FILTER_SANITIZE_STRING);
    $response->getBody()->write("Hello, $firstName $lastName");
    return $response;
});

// Create the requester

$requester = new Requester($application);

// And then you can perform requests:

$text = $requester->get('/get/toto');
$parameters = ['firstname' => 'Mickey', 'lastname' => 'Mouse'];
$text = $requester->post('/post', $parameters);
$response = $requester->getResponse();

API

\dbeurive\Slim\Requester::__construct(App $inApplication)
\dbeurive\Slim\Requester::get($inRequestUri, $inQueryString='')
\dbeurive\Slim\Requester::post($inRequestUri, $inPostParameters=[])
\dbeurive\Slim\Requester::jsonRpc($inRequestUri, $inParameters=[])
\dbeurive\Slim\Requester::setServerCgiEnvironmentVariables(array $inServerCgiEnvironmentVariables, $inMerge=false)
\dbeurive\Slim\Requester::setHttpHeaders(array $inHttpHeaders, $inMerge=false)
\dbeurive\Slim\Requester::getResponse()
\dbeurive\Slim\Requester::getRequest()

Please see the file Requester.php for a detailed description of the API.

Examples

Please see the unit tests for examples.

The file below creates the application:

  • index.php: this file is the application's entry point.

The three files below implement unit tests: