1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\TestBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use Overwatch\TestBundle\DataFixtures\ORM\TestFixtures; |
6
|
|
|
use Overwatch\TestBundle\DataFixtures\ORM\TestGroupFixtures; |
7
|
|
|
use Overwatch\TestBundle\Entity\Test; |
8
|
|
|
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* TestApiControllerTest |
13
|
|
|
* Functional test of API methods provided by the APIController |
14
|
|
|
*/ |
15
|
|
|
class TestApiControllerTest extends DatabaseAwareTestCase |
16
|
|
|
{ |
17
|
|
|
public function testCreateTest() |
18
|
|
|
{ |
19
|
|
|
$newTest = new Test; |
20
|
|
|
$newTest |
21
|
|
|
->setName('Ping 1234') |
22
|
|
|
->setActual('1.2.3.4') |
23
|
|
|
->setExpectation('toPing') |
24
|
|
|
; |
25
|
|
|
|
26
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
27
|
|
|
$this->makeJsonRequest( |
28
|
|
|
'POST', |
29
|
|
|
'/api/tests/group/' . TestGroupFixtures::$groups['group-2']->getId(), |
30
|
|
|
[ |
31
|
|
|
'name' => $newTest->getName(), |
32
|
|
|
'actual' => $newTest->getActual(), |
33
|
|
|
'expectation' => $newTest->getExpectation() |
34
|
|
|
] |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$test = $this->em->getRepository("Overwatch\TestBundle\Entity\Test")->findOneBy([ |
|
|
|
|
38
|
|
|
'name' => $newTest->getName() |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$this->assertNotNull($test); |
42
|
|
|
$this->assertEquals($newTest->getActual(), $test->getActual()); |
43
|
|
|
$this->assertEquals($newTest->getExpectation(), $test->getExpectation()); |
44
|
|
|
|
45
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
46
|
|
|
$this->assertJsonStringEqualsJsonString( |
47
|
|
|
json_encode($test), |
48
|
|
|
$this->getResponseContent(true) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testCreateTestInsufficentPerms() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$this->logIn('ROLE_USER'); |
55
|
|
|
$this->client->request('POST', '/api/tests/group/' . TestGroupFixtures::$groups['group-1']->getId()); |
56
|
|
|
|
57
|
|
|
$this->assertForbidden($this->client->getResponse()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testCreateTestInvalidExpectation() |
61
|
|
|
{ |
62
|
|
|
$newTest = new Test; |
63
|
|
|
$newTest |
64
|
|
|
->setName('Ping 1234') |
65
|
|
|
->setActual('1.2.3.4') |
66
|
|
|
->setExpectation('IfThisExpectationExistsIBrokeIt') |
67
|
|
|
; |
68
|
|
|
|
69
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
70
|
|
|
$this->makeJsonRequest( |
71
|
|
|
'POST', |
72
|
|
|
'/api/tests/group/' . TestGroupFixtures::$groups['group-2']->getId(), |
73
|
|
|
[ |
74
|
|
|
'name' => $newTest->getName(), |
75
|
|
|
'actual' => $newTest->getActual(), |
76
|
|
|
'expectation' => $newTest->getExpectation() |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->assertEquals(Response::HTTP_UNPROCESSABLE_ENTITY, $this->client->getResponse()->getStatusCode()); |
81
|
|
|
$this->assertContains($newTest->getExpectation(), $this->getResponseContent(true)); |
82
|
|
|
$this->assertContains('could not be found', $this->getResponseContent(true)); |
83
|
|
|
|
84
|
|
|
$this->assertNull($this->em->getRepository("Overwatch\TestBundle\Entity\Test")->findOneBy([ |
|
|
|
|
85
|
|
|
'name' => $newTest->getName() |
86
|
|
|
])); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testCreateTestInvalidActual() |
90
|
|
|
{ |
91
|
|
|
$newTest = new Test; |
92
|
|
|
$newTest |
93
|
|
|
->setName('Ping 1234') |
94
|
|
|
->setExpectation('toPing') |
95
|
|
|
; |
96
|
|
|
|
97
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
98
|
|
|
$this->makeJsonRequest( |
99
|
|
|
'POST', |
100
|
|
|
'/api/tests/group/' . TestGroupFixtures::$groups['group-2']->getId(), |
101
|
|
|
[ |
102
|
|
|
'name' => $newTest->getName(), |
103
|
|
|
'expectation' => $newTest->getExpectation() |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->assertEquals(Response::HTTP_UNPROCESSABLE_ENTITY, $this->client->getResponse()->getStatusCode()); |
108
|
|
|
$this->assertContains('An actual value to test against must be provided.', $this->getResponseContent(true)); |
109
|
|
|
|
110
|
|
|
$this->assertNull($this->em->getRepository("Overwatch\TestBundle\Entity\Test")->findOneBy([ |
|
|
|
|
111
|
|
|
'name' => $newTest->getName() |
112
|
|
|
])); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetTestsInGroup() |
116
|
|
|
{ |
117
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
118
|
|
|
$this->client->request('GET', '/api/tests/group/' . TestGroupFixtures::$groups['group-1']->getId()); |
119
|
|
|
|
120
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
121
|
|
|
$this->assertCount(2, $this->getResponseContent()); |
122
|
|
|
$this->assertCollectionContainsObject( |
123
|
|
|
$this->em->find("Overwatch\TestBundle\Entity\Test", TestFixtures::$tests['test-1']->getId()), |
|
|
|
|
124
|
|
|
$this->getResponseContent() |
125
|
|
|
); |
126
|
|
|
$this->assertCollectionContainsObject( |
127
|
|
|
$this->em->find("Overwatch\TestBundle\Entity\Test", TestFixtures::$tests['test-2']->getId()), |
|
|
|
|
128
|
|
|
$this->getResponseContent() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
public function testGetTestsInGroupInsufficentPerms() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->logIn('ROLE_ADMIN'); |
135
|
|
|
$this->client->request('GET', '/api/tests/group/' . TestGroupFixtures::$groups['group-1']->getId()); |
136
|
|
|
|
137
|
|
|
$this->assertForbidden($this->client->getResponse()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function testGetTestsInGroupInvalidGroup() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
143
|
|
|
$this->client->request('GET', '/api/tests/group/1000'); |
144
|
|
|
|
145
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function testGetTest() |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
151
|
|
|
$this->client->request('GET', '/api/tests/' . TestFixtures::$tests['test-1']->getId()); |
152
|
|
|
|
153
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
154
|
|
|
$this->assertJsonStringEqualsJsonString( |
155
|
|
|
json_encode( |
156
|
|
|
$this->em->find("Overwatch\TestBundle\Entity\Test", TestFixtures::$tests['test-1']->getId()) |
|
|
|
|
157
|
|
|
), |
158
|
|
|
$this->getResponseContent(true) |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
View Code Duplication |
public function testGetTestInsufficentPerms() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$this->logIn('ROLE_ADMIN'); |
165
|
|
|
$this->client->request('GET', '/api/tests/' . TestFixtures::$tests['test-1']->getId()); |
166
|
|
|
|
167
|
|
|
$this->assertForbidden($this->client->getResponse()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
View Code Duplication |
public function testGetTestInvalidTest() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
173
|
|
|
$this->client->request('GET', '/api/tests/1000'); |
174
|
|
|
|
175
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
public function testUpdateTest() |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$newName = 'Renamed Test'; |
181
|
|
|
|
182
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
183
|
|
|
$this->makeJsonRequest( |
184
|
|
|
'PUT', |
185
|
|
|
'/api/tests/' . TestFixtures::$tests['test-1']->getId(), |
186
|
|
|
[ |
187
|
|
|
'name' => $newName |
188
|
|
|
] |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$test = $this->em->find("Overwatch\TestBundle\Entity\Test", TestFixtures::$tests['test-1']->getId()); |
|
|
|
|
192
|
|
|
|
193
|
|
|
$this->assertEquals($newName, $test->getName()); |
194
|
|
|
|
195
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
196
|
|
|
$this->assertJsonStringEqualsJsonString( |
197
|
|
|
json_encode($test), |
198
|
|
|
$this->getResponseContent(true) |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
View Code Duplication |
public function testUpdateTestInsufficentPerms() |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$this->logIn('ROLE_USER'); |
205
|
|
|
$this->client->request('PUT', '/api/tests/' . TestFixtures::$tests['test-1']->getId()); |
206
|
|
|
|
207
|
|
|
$this->assertForbidden($this->client->getResponse()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
View Code Duplication |
public function testUpdateTestInvalidTest() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
213
|
|
|
$this->client->request('PUT', '/api/tests/1000'); |
214
|
|
|
|
215
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testDeleteTest() |
219
|
|
|
{ |
220
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
221
|
|
|
$this->client->request('DELETE', '/api/tests/' . TestFixtures::$tests['test-1']->getId()); |
222
|
|
|
|
223
|
|
|
$this->assertTrue($this->client->getResponse()->isSuccessful()); |
224
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $this->client->getResponse()->getStatusCode()); |
225
|
|
|
$this->assertNull($this->em->find("Overwatch\TestBundle\Entity\Test", TestFixtures::$tests['test-1']->getId())); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
View Code Duplication |
public function testDeleteTestInsufficentPerms() |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$this->logIn('ROLE_USER'); |
231
|
|
|
$this->client->request('DELETE', '/api/tests/' . TestFixtures::$tests['test-1']->getId()); |
232
|
|
|
|
233
|
|
|
$this->assertForbidden($this->client->getResponse()); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function testDeleteTestInvalidTest() |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
239
|
|
|
$this->client->request('DELETE', '/api/tests/1000'); |
240
|
|
|
|
241
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
View Code Duplication |
public function testRunTest() |
|
|
|
|
245
|
|
|
{ |
246
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
247
|
|
|
$this->client->request('POST', '/api/tests/' . TestFixtures::$tests['test-1']->getId()); |
248
|
|
|
|
249
|
|
|
$test = $this->em->find("Overwatch\TestBundle\Entity\Test", TestFixtures::$tests['test-1']->getId()); |
|
|
|
|
250
|
|
|
|
251
|
|
|
$this->assertJsonResponse($this->client->getResponse()); |
252
|
|
|
$this->assertJsonStringEqualsJsonString( |
253
|
|
|
json_encode($test->getResults()->last()), |
254
|
|
|
$this->getResponseContent(true) |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
View Code Duplication |
public function testRunTestInsufficentPerms() |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
$this->logIn('ROLE_USER'); |
261
|
|
|
$this->client->request('POST', '/api/tests/' . TestFixtures::$tests['test-1']->getId()); |
262
|
|
|
|
263
|
|
|
$this->assertForbidden($this->client->getResponse()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
View Code Duplication |
public function testRunTestInvalidTest() |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
$this->logIn('ROLE_SUPER_ADMIN'); |
269
|
|
|
$this->client->request('POST', '/api/tests/1000'); |
270
|
|
|
|
271
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $this->client->getResponse()->getStatusCode()); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.