-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.src
99 lines (66 loc) · 3.15 KB
/
README.src
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
INSERT-TOC-HERE
# Introduction
This package contains a "requester" the [Slim framework](https://www.slimframework.com/).
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](https://creativecommons.org/licenses/by/4.0/legalcode)
See the file [LICENSE.TXT](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:
```php
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](src/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](tests/www/index.php): this file is the application's entry point.
The three files below implement unit tests:
* [AppInit.php](tests/actions/AppInit.php): this file creates the application and initializes the requester.
* [GetTest.php](tests/actions/GetTest.php)
* [JsonRpcTest.php](tests/actions/JsonRpcTest.php)
* [PostTest.php](tests/actions/PostTest.php)