|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace DataFlow\Tests\Unit\Stdlib\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Middleware\ResetIdMiddleware; |
|
12
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
13
|
|
|
|
|
14
|
|
|
final class ResetIdMiddlewareTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testResetData() |
|
17
|
|
|
{ |
|
18
|
|
|
$middleware = new ResetIdMiddleware(); |
|
19
|
|
|
|
|
20
|
|
|
$request = new \Zend\Diactoros\ServerRequest(); |
|
21
|
|
|
|
|
22
|
|
|
$pipeline = new \Zend\Stratigility\MiddlewarePipe(); |
|
23
|
|
|
$last = new class implements MiddlewareInterface |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public function process( |
|
29
|
|
|
ServerRequestInterface $request, |
|
30
|
|
|
RequestHandlerInterface $handler |
|
31
|
|
|
): ResponseInterface { |
|
32
|
|
|
return new JsonResponse($request->getParsedBody(), 200); |
|
33
|
|
|
} |
|
34
|
|
|
}; |
|
35
|
|
|
$pipeline->pipe($middleware); |
|
36
|
|
|
$pipeline->pipe($last); |
|
37
|
|
|
|
|
38
|
|
|
$response = $pipeline->handle($request->withParsedBody([ |
|
39
|
|
|
'name' => 'bar', |
|
40
|
|
|
'id' => 'baz', |
|
41
|
|
|
])); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode(['name' => 'bar']), $response->getBody()->__toString()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testResetDataNonDefault() |
|
47
|
|
|
{ |
|
48
|
|
|
$middleware = new ResetIdMiddleware('name'); |
|
49
|
|
|
|
|
50
|
|
|
$request = new \Zend\Diactoros\ServerRequest(); |
|
51
|
|
|
|
|
52
|
|
|
$pipeline = new \Zend\Stratigility\MiddlewarePipe(); |
|
53
|
|
|
$last = new class implements MiddlewareInterface |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function process( |
|
59
|
|
|
ServerRequestInterface $request, |
|
60
|
|
|
RequestHandlerInterface $handler |
|
61
|
|
|
): ResponseInterface { |
|
62
|
|
|
return new JsonResponse($request->getParsedBody(), 200); |
|
63
|
|
|
} |
|
64
|
|
|
}; |
|
65
|
|
|
$pipeline->pipe($middleware); |
|
66
|
|
|
$pipeline->pipe($last); |
|
67
|
|
|
|
|
68
|
|
|
$response = $pipeline->handle($request->withParsedBody([ |
|
69
|
|
|
'name' => 'bar', |
|
70
|
|
|
'id' => 'baz', |
|
71
|
|
|
])); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode(['id' => 'baz']), $response->getBody()->__toString()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.