@@ -41,6 +41,7 @@ public function assertNotEquals($expected, $actual, $message = '')
4141 * @param $expected
4242 * @param $actual
4343 * @param string $message
44+ * @return mixed|void
4445 */
4546 public function assertSame ($ expected , $ actual , $ message = '' )
4647 {
@@ -261,6 +262,85 @@ public function assertFileNotExists($filename, $message = '')
261262 parent ::assertFileNotExists ($ filename , $ message );
262263 }
263264
265+ /**
266+ * @param $expected
267+ * @param $actual
268+ * @param $description
269+ */
270+ public function assertGreaterOrEquals ($ expected , $ actual , $ description = null )
271+ {
272+ parent ::assertGreaterThanOrEqual ($ expected , $ actual , $ description );
273+ }
274+
275+ /**
276+ * @param $expected
277+ * @param $actual
278+ * @param $description
279+ */
280+ public function assertLessOrEquals ($ expected , $ actual , $ description = null )
281+ {
282+ parent ::assertLessThanOrEqual ($ expected , $ actual , $ description );
283+ }
284+
285+ /**
286+ * @param $actual
287+ * @param $description
288+ */
289+ public function assertIsEmpty ($ actual , $ description = null )
290+ {
291+ parent ::assertEmpty ($ actual , $ description );
292+ }
293+
294+ /**
295+ * @param $key
296+ * @param $actual
297+ * @param $description
298+ */
299+ public function assertArrayHasKey ($ key , $ actual , $ description = null )
300+ {
301+ parent ::assertArrayHasKey ($ key , $ actual , $ description );
302+ }
303+
304+ /**
305+ * @param $key
306+ * @param $actual
307+ * @param $description
308+ */
309+ public function assertArrayNotHasKey ($ key , $ actual , $ description = null )
310+ {
311+ parent ::assertArrayNotHasKey ($ key , $ actual , $ description );
312+ }
313+
314+ /**
315+ * @param $class
316+ * @param $actual
317+ * @param $description
318+ */
319+ public function assertInstanceOf ($ class , $ actual , $ description = null )
320+ {
321+ parent ::assertInstanceOf ($ class , $ actual , $ description );
322+ }
323+
324+ /**
325+ * @param $class
326+ * @param $actual
327+ * @param $description
328+ */
329+ public function assertNotInstanceOf ($ class , $ actual , $ description = null )
330+ {
331+ parent ::assertNotInstanceOf ($ class , $ actual , $ description );
332+ }
333+
334+ /**
335+ * @param $type
336+ * @param $actual
337+ * @param $description
338+ */
339+ public function assertInternalType ($ type , $ actual , $ description = null )
340+ {
341+ parent ::assertInternalType ($ type , $ actual , $ description );
342+ }
343+
264344 /**
265345 * Fails the test with message.
266346 *
@@ -270,4 +350,60 @@ public function fail($message)
270350 {
271351 parent ::fail ($ message );
272352 }
353+
354+ /**
355+ * Handles and checks exception called inside callback function.
356+ * Either exception class name or exception instance should be provided.
357+ *
358+ * ```php
359+ * <?php
360+ * $I->expectException(MyException::class, function() {
361+ * $this->doSomethingBad();
362+ * });
363+ *
364+ * $I->expectException(new MyException(), function() {
365+ * $this->doSomethingBad();
366+ * });
367+ * ```
368+ * If you want to check message or exception code, you can pass them with exception instance:
369+ * ```php
370+ * <?php
371+ * // will check that exception MyException is thrown with "Don't do bad things" message
372+ * $I->expectException(new MyException("Don't do bad things"), function() {
373+ * $this->doSomethingBad();
374+ * });
375+ * ```
376+ *
377+ * @param $exception string or \Exception
378+ * @param $callback
379+ */
380+ public function expectException ($ exception , $ callback )
381+ {
382+ $ code = null ;
383+ $ msg = null ;
384+ if (is_object ($ exception )) {
385+ /** @var $exception \Exception **/
386+ $ class = get_class ($ exception );
387+ $ msg = $ exception ->getMessage ();
388+ $ code = $ exception ->getCode ();
389+ } else {
390+ $ class = $ exception ;
391+ }
392+ try {
393+ $ callback ();
394+ } catch (\Exception $ e ) {
395+ if (!$ e instanceof $ class ) {
396+ $ this ->fail (sprintf ("Exception of class $ class expected to be thrown, but %s caught " , get_class ($ e )));
397+ }
398+ if (null !== $ msg and $ e ->getMessage () !== $ msg ) {
399+ $ this ->fail (sprintf ("Exception of $ class expected to be ' $ msg', but actual message was '%s' " , $ e ->getMessage ()));
400+ }
401+ if (null !== $ code and $ e ->getCode () !== $ code ) {
402+ $ this ->fail (sprintf ("Exception of $ class expected to have code $ code, but actual code was %s " , $ e ->getCode ()));
403+ }
404+ $ this ->assertTrue (true ); // increment assertion counter
405+ return ;
406+ }
407+ $ this ->fail ("Expected exception to be thrown, but nothing was caught " );
408+ }
273409}
0 commit comments