Skip to content

Commit 7441889

Browse files
committed
added more assert* methods to Asserts module. Fix #2973
1 parent 02e0711 commit 7441889

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

src/Codeception/Module/Asserts.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/unit/Codeception/Module/AssertsTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public function testAsserts()
77
$module->assertEquals(1,1);
88
$module->assertContains(1,[1,2]);
99
$module->assertSame(1,1);
10-
$module->assertNotSame(1,true);
10+
$module->assertNotSame(1, '1');
1111
$module->assertRegExp('/^[\d]$/','1');
1212
$module->assertNotRegExp('/^[a-z]$/','1');
1313
$module->assertEmpty([]);
@@ -20,6 +20,24 @@ public function testAsserts()
2020
$module->assertFalse(false);
2121
$module->assertFileExists(__FILE__);
2222
$module->assertFileNotExists(__FILE__ . '.notExist');
23+
$module->assertInstanceOf('Exception', new Exception());
24+
$module->assertInternalType('integer', 5);
25+
$module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]);
26+
}
27+
28+
public function testExceptions()
29+
{
30+
$module = new \Codeception\Module\Asserts(make_container());
31+
$module->expectException('Exception', function() { throw new Exception; });
32+
$module->expectException(new Exception('here'), function() { throw new Exception('here'); });
33+
$module->expectException(new Exception('here', 200), function() { throw new Exception('here', 200); });
34+
}
35+
36+
public function testExceptionFails()
37+
{
38+
$module = new \Codeception\Module\Asserts(make_container());
39+
$this->expectException('PHPUnit_Framework_AssertionFailedError');
40+
$module->expectException(new Exception('here', 200), function() { throw new Exception('here', 2); });
2341
}
2442

2543
}

0 commit comments

Comments
 (0)