Skip to content

Commit c4688ed

Browse files
committed
Merge pull request #45 from cabello/patch-2
Create JsonResponse and add response status code assertions
2 parents a7a7246 + 43fc4e7 commit c4688ed

4 files changed

Lines changed: 161 additions & 4 deletions

File tree

Test/Functional/WebTestCase.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace IC\Bundle\Base\TestBundle\Test\Functional;
77

88
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
9-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
109
use IC\Bundle\Base\TestBundle\Test\Loader;
10+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
1111

1212
/**
1313
* Abstract class for Web test cases
@@ -181,4 +181,84 @@ protected static function getFixtureList()
181181
{
182182
return array();
183183
}
184+
185+
/**
186+
* Assert HTTP response status code is 200 OK.
187+
*
188+
* @param \Symfony\Component\HttpFoundation\Response $response
189+
* @param string $message
190+
*/
191+
protected function assertResponseStatusOk($response, $message = '')
192+
{
193+
$this->assertResponseStatusCode(200, $response, $message);
194+
}
195+
196+
/**
197+
* Assert HTTP response status code is Redirection 3xx.
198+
*
199+
* @param \Symfony\Component\HttpFoundation\Response $response
200+
* @param string $message
201+
*/
202+
protected function assertResponseStatusRedirection($response, $message = '')
203+
{
204+
$this->assertGreaterThanOrEqual(300, $response->getStatusCode(), $message);
205+
$this->assertLessThanOrEqual(399, $response->getStatusCode(), $message);
206+
}
207+
208+
/**
209+
* Assert HTTP response status code is 301 Moved.
210+
*
211+
* @param \Symfony\Component\HttpFoundation\Response $response
212+
* @param string $message
213+
*/
214+
protected function assertResponseStatusMoved($response, $message = '')
215+
{
216+
$this->assertResponseStatusCode(301, $response, $message);
217+
}
218+
219+
/**
220+
* Assert HTTP response status code is 302 Found.
221+
*
222+
* @param \Symfony\Component\HttpFoundation\Response $response
223+
* @param string $message
224+
*/
225+
protected function assertResponseStatusFound($response, $message = '')
226+
{
227+
$this->assertResponseStatusCode(302, $response, $message);
228+
}
229+
230+
/**
231+
* Assert HTTP response status code is 404 Not Found.
232+
*
233+
* @param \Symfony\Component\HttpFoundation\Response $response
234+
* @param string $message
235+
*/
236+
protected function assertResponseStatusNotFound($response, $message = '')
237+
{
238+
$this->assertResponseStatusCode(404, $response, $message);
239+
}
240+
241+
/**
242+
* Assert HTTP response status code matches the expected.
243+
*
244+
* @param integer $expectedStatusCode
245+
* @param \Symfony\Component\HttpFoundation\Response $response
246+
* @param string $message
247+
*/
248+
protected function assertResponseStatusCode($expectedStatusCode, $response, $message = '')
249+
{
250+
$this->assertEquals($expectedStatusCode, $response->getStatusCode(), $message);
251+
}
252+
253+
/**
254+
* Assert HTTP response header location matches the expected.
255+
*
256+
* @param string|null $expectedLocation
257+
* @param \Symfony\Component\HttpFoundation\Response $response
258+
* @param string $message
259+
*/
260+
protected function assertResponseRedirectionLocation($expectedLocation, $response, $message = '')
261+
{
262+
$this->assertEquals($expectedLocation, $response->headers->get('location'), $message);
263+
}
184264
}

Test/Helper/SessionHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function authenticate(UserInterface $user, $firewall)
8989

9090
$this->session->set('_security_' . $firewall, serialize($token));
9191
$this->session->save();
92-
92+
9393
$this->client->getCookieJar()->set(new Cookie($this->session->getName(), $this->session->getId()));
9494
}
9595

Test/Model/JsonResponse.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* @copyright 2013 Instaclick Inc.
4+
*/
5+
6+
namespace IC\Bundle\Base\TestBundle\Test\Model;
7+
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Symfony\Component\PropertyAccess\PropertyAccess;
10+
11+
/**
12+
* Provides accessor to properties of a JSON encoded response.
13+
*
14+
* @author Danilo Cabello <danilo.cabello@gmail.com>
15+
*/
16+
class JsonResponse
17+
{
18+
/**
19+
* Original response.
20+
*
21+
* @var \Symfony\Component\HttpFoundation\Response
22+
*/
23+
private $response;
24+
25+
/**
26+
* Decoded JSON response into StdClass object.
27+
*
28+
* @var StdClass|null
29+
*/
30+
private $responseObject = null;
31+
32+
/**
33+
* Constructor needs the original response and creates a property accessor.
34+
*
35+
* @param \Symfony\Component\HttpFoundation\Response $response
36+
*/
37+
public function __construct(Response $response)
38+
{
39+
$this->response = $response;
40+
$this->responseObject = json_decode($this->response->getContent());
41+
$this->accessor = PropertyAccess::getPropertyAccessor();
42+
}
43+
44+
/**
45+
* If method is not implemented here, try calling method from original response.
46+
*
47+
* @param string $methodName
48+
* @param array $argumentList
49+
*
50+
* @return mixed Return type will be the same from original method.
51+
*/
52+
public function __call($methodName, $argumentList)
53+
{
54+
return call_user_func_array(array($this->response, $methodName), $argumentList);
55+
}
56+
57+
/**
58+
* Retrieve decoded JSON response into StdClass object.
59+
*
60+
* @return StdClass
61+
*/
62+
public function getResponseObject()
63+
{
64+
return $this->responseObject;
65+
}
66+
67+
/**
68+
* Retrieve property from decoded object.
69+
*
70+
* @param string $propertyPath
71+
*
72+
* @return mixed
73+
*/
74+
public function getProperty($propertyPath)
75+
{
76+
return $this->accessor->getValue($this->getResponseObject(), $propertyPath);
77+
}
78+
}

Test/WebTestCase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,12 @@ public function getHelper($name)
8080

8181
/**
8282
* Add helper to helper list.
83-
*
83+
*
8484
* @param string $name
8585
* @param string $namespace
8686
*/
8787
public function addHelper($name, $namespace)
8888
{
8989
$this->helperList->set($name, $namespace);
9090
}
91-
9291
}

0 commit comments

Comments
 (0)