1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMG\UserBundle\Tests; |
4
|
|
|
|
5
|
|
|
trait TestHelpersTrait |
6
|
|
|
{ |
7
|
|
|
private $client; |
8
|
|
|
private $em; |
9
|
|
|
private $currentUser; |
10
|
|
|
private $fixtures; |
11
|
|
|
private $response; |
12
|
|
|
private $authAsUser; |
13
|
|
|
|
14
|
|
|
protected function performClientRequest( |
15
|
|
|
$method, |
16
|
|
|
$urlPath, |
17
|
|
|
$headers = ['HTTP_ACCEPT' => 'application/json'], |
18
|
|
|
$rawRequestBody = null |
19
|
|
|
) { |
20
|
|
|
$this->client->request( |
21
|
|
|
$method, |
22
|
|
|
$urlPath, |
23
|
|
|
[], |
24
|
|
|
[], |
25
|
|
|
$headers, |
26
|
|
|
$rawRequestBody |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
return $this->client->getResponse(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function performAuthenticatedClientRequest($method, $urlPath, $username = null) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$username = $username ?: $this->authAsUser; |
35
|
|
|
$this->client = static::createClient( |
36
|
|
|
array(), |
37
|
|
|
array('HTTP_Authorization' => "Bearer {$username}") |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
return $this->performClientRequest($method, $urlPath); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function assertJsonResponse( |
44
|
|
|
$response, |
45
|
|
|
$statusCode = 200, |
46
|
|
|
$checkValidJson = true, |
47
|
|
|
$contentType = 'application/json' |
48
|
|
|
) { |
49
|
|
|
$this->assertEquals( |
50
|
|
|
$statusCode, $response->getStatusCode(), |
51
|
|
|
$response->getContent() |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
if ($checkValidJson) { |
55
|
|
|
$this->assertTrue( |
56
|
|
|
$response->headers->contains('Content-Type', $contentType), |
57
|
|
|
$response->headers |
58
|
|
|
); |
59
|
|
|
$decode = json_decode($response->getContent()); |
60
|
|
|
$this->assertTrue( |
61
|
|
|
($decode !== null && $decode !== false), |
62
|
|
|
'is response valid json: ['.$response->getContent().']' |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function givenLoggedInAs($username) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->currentUser = $username; |
70
|
|
|
$this->client = static::createClient( |
71
|
|
|
[], |
72
|
|
|
[ |
73
|
|
|
'HTTP_Authorization' => "Bearer {$username}", |
74
|
|
|
'HTTP_ACCEPT' => 'application/json', |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function assertPermissionError() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$this->assertJsonResponse($this->response, 401, false); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function assertPermissionDenied() |
85
|
|
|
{ |
86
|
|
|
$this->assertJsonResponse($this->response, 403, false); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function assertNotFoundError() |
90
|
|
|
{ |
91
|
|
|
$this->assertJsonResponse($this->response, 404, false); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function assertNoContentResponse() |
95
|
|
|
{ |
96
|
|
|
$this->assertJsonResponse($this->response, 204, false); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function assertBadRequestError() |
100
|
|
|
{ |
101
|
|
|
$this->assertJsonResponse($this->response, 400, false); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function assertCreatedSuccess() |
105
|
|
|
{ |
106
|
|
|
$this->assertJsonResponse($this->response, 201, false); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function assertOkSuccess() |
110
|
|
|
{ |
111
|
|
|
$this->assertJsonResponse($this->response, 200, false); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function assertAcceptedSuccess() |
115
|
|
|
{ |
116
|
|
|
$this->assertJsonResponse($this->response, 202, false); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
abstract public function assertTrue($condition, $message = ''); |
120
|
|
|
abstract public function assertEquals( |
121
|
|
|
$expected, |
122
|
|
|
$actual, |
123
|
|
|
$message = '', |
124
|
|
|
$delta = 0.0, |
125
|
|
|
$maxDepth = 10, |
126
|
|
|
$canonicalize = false, |
127
|
|
|
$ignoreCase = false |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|