1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Zend\Diactoros\ServerRequest; |
4
|
|
|
use Zend\Diactoros\Response; |
5
|
|
|
use Zend\Diactoros\Uri; |
6
|
|
|
use Zend\Diactoros\UploadedFile; |
7
|
|
|
use Psr7Unitesting\Assert; |
8
|
|
|
|
9
|
|
|
class AppTest extends PHPUnit_Framework_TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public function testServerRequest() |
12
|
|
|
{ |
13
|
|
|
$request = (new ServerRequest(['DOCUMENT_ROOT' => '/www'])) |
14
|
|
|
->withMethod('GET') |
15
|
|
|
->withUri(new Uri('http://user:[email protected]/?ola#id')) |
16
|
|
|
->withQueryParams(['ola' => 'value']) |
17
|
|
|
->withAttribute('bar', 'foo') |
18
|
|
|
->withCookieParams(['cookie-name' => 'cookie-value']) |
19
|
|
|
->withParsedBody(['name' => 'value']) |
20
|
|
|
->withUploadedFiles([ |
21
|
|
|
'file' => new UploadedFile('file.jpg', 50, 0, 'file.jpg', 'image/jpg'), |
22
|
|
|
]) |
23
|
|
|
->withHeader('Content-Type', 'text/html'); |
24
|
|
|
|
25
|
|
|
Assert::create($request) |
|
|
|
|
26
|
|
|
->protocolVersion('1.1') |
27
|
|
|
->header('Content-Type', 'text/html') |
28
|
|
|
->hasHeader('Content-Type') |
29
|
|
|
->hasNotHeader('X-Content-Type') |
30
|
|
|
->requestTarget('/?ola') |
31
|
|
|
->uri('http://user:[email protected]/?ola#id') |
32
|
|
|
->method('GET') |
33
|
|
|
->attribute('bar', 'foo') |
34
|
|
|
->hasAttribute('bar') |
35
|
|
|
->hasParsedBody('name') |
36
|
|
|
->parsedBody('name', 'value') |
37
|
|
|
->hasUploadedFile('file') |
38
|
|
|
->hasQueryParam('ola') |
39
|
|
|
->queryParam('ola', 'value') |
40
|
|
|
->cookieParam('cookie-name', 'cookie-value') |
41
|
|
|
->hasCookieParam('cookie-name') |
42
|
|
|
->hasServerParam('DOCUMENT_ROOT') |
43
|
|
|
->serverParam('DOCUMENT_ROOT', '/www') |
44
|
|
|
->assertUri() |
45
|
|
|
->scheme('http') |
46
|
|
|
->authority('user:[email protected]') |
47
|
|
|
->host('dag.gal') |
48
|
|
|
->query('ola') |
49
|
|
|
->fragment('id') |
50
|
|
|
->path('/') |
51
|
|
|
->userInfo('user:pass') |
52
|
|
|
->port(null) |
53
|
|
|
->end() |
54
|
|
|
|
55
|
|
|
->assertBody() |
56
|
|
|
->isNotWritable() |
57
|
|
|
->isReadable() |
58
|
|
|
->isSeekable() |
59
|
|
|
->size(0); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testResponse() |
63
|
|
|
{ |
64
|
|
|
$response = (new Response()) |
65
|
|
|
->withHeader('Content-Type', 'text/html') |
66
|
|
|
->withStatus(200, 'Ok!'); |
67
|
|
|
|
68
|
|
|
$response->getBody()->write(<<<HTML |
69
|
|
|
<!DOCTYPE html> |
70
|
|
|
<html> |
71
|
|
|
<head> |
72
|
|
|
<title>Hello world</title> |
73
|
|
|
</head> |
74
|
|
|
<body> |
75
|
|
|
<h1>Ola mundo</h1> |
76
|
|
|
<div class="first">Ola mundo</div> |
77
|
|
|
<div class="second">Ola mundo</div> |
78
|
|
|
</body> |
79
|
|
|
</html> |
80
|
|
|
HTML |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
Assert::create($response) |
|
|
|
|
84
|
|
|
->protocolVersion('1.1') |
85
|
|
|
->header('Content-Type', 'text/html') |
86
|
|
|
->hasHeader('Content-Type') |
87
|
|
|
->hasNotHeader('X-Content-Type') |
88
|
|
|
->statusCode(200) |
89
|
|
|
->reasonPhrase('Ok!') |
90
|
|
|
->assertBody() |
91
|
|
|
->isWritable() |
92
|
|
|
->isReadable() |
93
|
|
|
->isSeekable() |
94
|
|
|
->size(225) |
95
|
|
|
->assertHtml() |
96
|
|
|
->count('div', 2) |
97
|
|
|
->has('h1') |
98
|
|
|
->hasNot('section') |
99
|
|
|
->contains('h1', 'mundo') |
100
|
|
|
->notContains('h1', 'mundor') |
101
|
|
|
->isValid(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.