2424
2525namespace Tests \Assertions ;
2626
27+ use Illuminate \Http \JsonResponse ;
28+ use Illuminate \Http \Response ;
2729use Illuminate \View \View ;
2830use PHPUnit_Framework_Assert ;
2931use Illuminate \Http \RedirectResponse ;
3032
3133trait ControllerAssertionsTrait
3234{
35+ /**
36+ * Assert that a response is an instance of Illuminate View.
37+ *
38+ * @param mixed $response
39+ */
40+ public function assertIsViewResponse ($ response )
41+ {
42+ PHPUnit_Framework_Assert::assertInstanceOf (View::class, $ response );
43+ }
44+
45+ /**
46+ * Assert that a response is an instance of Illuminate Redirect Response.
47+ *
48+ * @param mixed $response
49+ */
50+ public function assertIsRedirectResponse ($ response )
51+ {
52+ PHPUnit_Framework_Assert::assertInstanceOf (RedirectResponse::class, $ response );
53+ }
54+
55+ /**
56+ * Assert that a response is an instance of Illuminate Json Response.
57+ *
58+ * @param mixed $response
59+ */
60+ public function assertIsJsonResponse ($ response )
61+ {
62+ PHPUnit_Framework_Assert::assertInstanceOf (JsonResponse::class, $ response );
63+ }
64+
65+ /**
66+ * Assert that a response is an instance of Illuminate Response.
67+ *
68+ * @param mixed $response
69+ */
70+ public function assertIsResponse ($ response )
71+ {
72+ PHPUnit_Framework_Assert::assertInstanceOf (Response::class, $ response );
73+ }
74+
3375 /**
3476 * Assert that a view name equals the passed name.
3577 *
36- * @param string $name
37- * @param \Illuminate\View\View $view
78+ * @param string $name
79+ * @param mixed $view
3880 */
3981 public function assertViewNameEquals ($ name , $ view )
4082 {
41- PHPUnit_Framework_Assert::assertInstanceOf (View::class, $ view );
4283 PHPUnit_Framework_Assert::assertEquals ($ name , $ view ->getName ());
4384 }
4485
4586 /**
4687 * Assert that a view name does not equal a provided name.
4788 *
48- * @param string $name
49- * @param \Illuminate\View\View $view
89+ * @param string $name
90+ * @param mixed $view
5091 */
5192 public function assertViewNameNotEquals ($ name , $ view )
5293 {
53- PHPUnit_Framework_Assert::assertInstanceOf (View::class, $ view );
5494 PHPUnit_Framework_Assert::assertNotEquals ($ name , $ view ->getName ());
5595 }
5696
5797 /**
5898 * Assert that a view has an attribute passed into it.
5999 *
60- * @param string $attribute
61- * @param \Illuminate\View\View $view
100+ * @param string $attribute
101+ * @param mixed $view
62102 */
63103 public function assertViewHasKey ($ attribute , $ view )
64104 {
65- PHPUnit_Framework_Assert::assertInstanceOf (View::class, $ view );
66-
67105 if (str_contains ($ attribute , '. ' )) {
68106 PHPUnit_Framework_Assert::assertNotEquals (
69107 '__TEST__FAIL ' ,
@@ -77,13 +115,11 @@ public function assertViewHasKey($attribute, $view)
77115 /**
78116 * Assert that a view does not have a specific attribute passed in.
79117 *
80- * @param string $attribute
81- * @param \Illuminate\View\View $view
118+ * @param string $attribute
119+ * @param mixed $view
82120 */
83121 public function assertViewNotHasKey ($ attribute , $ view )
84122 {
85- PHPUnit_Framework_Assert::assertInstanceOf (View::class, $ view );
86-
87123 if (str_contains ($ attribute , '. ' )) {
88124 PHPUnit_Framework_Assert::assertEquals (
89125 '__TEST__PASS ' ,
@@ -97,36 +133,78 @@ public function assertViewNotHasKey($attribute, $view)
97133 /**
98134 * Assert that a view attribute equals a given parameter.
99135 *
100- * @param string $attribute
101- * @param mixed $value
102- * @param \Illuminate\View\View $view
136+ * @param string $attribute
137+ * @param mixed $value
138+ * @param mixed $view
103139 */
104140 public function assertViewKeyEquals ($ attribute , $ value , $ view )
105141 {
106- PHPUnit_Framework_Assert::assertInstanceOf (View::class, $ view );
107142 PHPUnit_Framework_Assert::assertEquals ($ value , array_get ($ view ->getData (), $ attribute , '__TEST__FAIL ' ));
108143 }
109144
110145 /**
111146 * Assert that a view attribute does not equal a given parameter.
112147 *
113- * @param string $attribute
114- * @param mixed $value
115- * @param \Illuminate\View\View $view
148+ * @param string $attribute
149+ * @param mixed $value
150+ * @param mixed $view
116151 */
117152 public function assertViewKeyNotEquals ($ attribute , $ value , $ view )
118153 {
119- PHPUnit_Framework_Assert::assertInstanceOf (View::class, $ view );
120154 PHPUnit_Framework_Assert::assertNotEquals ($ value , array_get ($ view ->getData (), $ attribute , '__TEST__FAIL ' ));
121155 }
122156
123157 /**
124- * @param string $route
125- * @param \Illuminate\Http\RedirectResponse $response
158+ * Assert that a route redirect equals a given route name.
159+ *
160+ * @param string $route
161+ * @param mixed $response
126162 */
127163 public function assertRouteRedirectEquals ($ route , $ response )
128164 {
129- PHPUnit_Framework_Assert::assertInstanceOf (RedirectResponse::class, $ response );
130165 PHPUnit_Framework_Assert::assertEquals (route ($ route ), $ response ->getTargetUrl ());
131166 }
167+
168+ /**
169+ * Assert that a response code equals a given code.
170+ *
171+ * @param int $code
172+ * @param mixed $response
173+ */
174+ public function assertResponseCodeEquals ($ code , $ response )
175+ {
176+ PHPUnit_Framework_Assert::assertEquals ($ code , $ response ->getStatusCode ());
177+ }
178+
179+ /**
180+ * Assert that a response code does not equal a given code.
181+ *
182+ * @param int $code
183+ * @param mixed $response
184+ */
185+ public function assertResponseCodeNotEquals ($ code , $ response )
186+ {
187+ PHPUnit_Framework_Assert::assertNotEquals ($ code , $ response ->getStatusCode ());
188+ }
189+
190+ /**
191+ * Assert that a response is in a JSON format.
192+ *
193+ * @param mixed $response
194+ */
195+ public function assertResponseHasJsonHeaders ($ response )
196+ {
197+ PHPUnit_Framework_Assert::assertEquals ('application/json ' , $ response ->headers ->get ('content-type ' ));
198+ }
199+
200+ /**
201+ * Assert that response JSON matches a given JSON string.
202+ *
203+ * @param array|string $json
204+ * @param mixed $response
205+ */
206+ public function assertResponseJsonEquals ($ json , $ response )
207+ {
208+ PHPUnit_Framework_Assert::assertEquals (is_array ($ json ) ? json_encode ($ json ) : $ json , $ response ->getContent ());
209+ }
132210}
0 commit comments