|
1 | 1 | <?php |
2 | 2 | class AssertsTest extends \PHPUnit\Framework\TestCase |
3 | 3 | { |
| 4 | + /** @var \Codeception\Module\Asserts */ |
| 5 | + protected $module; |
| 6 | + |
| 7 | + public function setUp() |
| 8 | + { |
| 9 | + $this->module = new \Codeception\Module\Asserts(make_container()); |
| 10 | + } |
| 11 | + |
4 | 12 | public function testAsserts() |
5 | 13 | { |
6 | | - $module = new \Codeception\Module\Asserts(make_container()); |
7 | | - $module->assertEquals(1, 1); |
8 | | - $module->assertContains(1, [1, 2]); |
9 | | - $module->assertSame(1, 1); |
10 | | - $module->assertNotSame(1, '1'); |
11 | | - $module->assertRegExp('/^[\d]$/', '1'); |
12 | | - $module->assertNotRegExp('/^[a-z]$/', '1'); |
13 | | - $module->assertStringStartsWith('fo', 'foo'); |
14 | | - $module->assertStringStartsNotWith('ba', 'foo'); |
15 | | - $module->assertEmpty([]); |
16 | | - $module->assertNotEmpty([1]); |
17 | | - $module->assertNull(null); |
18 | | - $module->assertNotNull(''); |
19 | | - $module->assertNotNull(false); |
20 | | - $module->assertNotNull(0); |
21 | | - $module->assertTrue(true); |
22 | | - $module->assertNotTrue(false); |
23 | | - $module->assertNotTrue(null); |
24 | | - $module->assertNotTrue('foo'); |
25 | | - $module->assertFalse(false); |
26 | | - $module->assertNotFalse(true); |
27 | | - $module->assertNotFalse(null); |
28 | | - $module->assertNotFalse('foo'); |
29 | | - $module->assertFileExists(__FILE__); |
30 | | - $module->assertFileNotExists(__FILE__ . '.notExist'); |
31 | | - $module->assertInstanceOf('Exception', new Exception()); |
32 | | - $module->assertInternalType('integer', 5); |
33 | | - $module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]); |
34 | | - $module->assertArraySubset(['foo' => [1]], ['foo' => [1, 2]]); |
35 | | - $module->assertCount(3, [1, 2, 3]); |
| 14 | + $this->module->assertEquals(1, 1); |
| 15 | + $this->module->assertContains(1, [1, 2]); |
| 16 | + $this->module->assertSame(1, 1); |
| 17 | + $this->module->assertNotSame(1, '1'); |
| 18 | + $this->module->assertRegExp('/^[\d]$/', '1'); |
| 19 | + $this->module->assertNotRegExp('/^[a-z]$/', '1'); |
| 20 | + $this->module->assertStringStartsWith('fo', 'foo'); |
| 21 | + $this->module->assertStringStartsNotWith('ba', 'foo'); |
| 22 | + $this->module->assertEmpty([]); |
| 23 | + $this->module->assertNotEmpty([1]); |
| 24 | + $this->module->assertNull(null); |
| 25 | + $this->module->assertNotNull(''); |
| 26 | + $this->module->assertNotNull(false); |
| 27 | + $this->module->assertNotNull(0); |
| 28 | + $this->module->assertTrue(true); |
| 29 | + $this->module->assertNotTrue(false); |
| 30 | + $this->module->assertNotTrue(null); |
| 31 | + $this->module->assertNotTrue('foo'); |
| 32 | + $this->module->assertFalse(false); |
| 33 | + $this->module->assertNotFalse(true); |
| 34 | + $this->module->assertNotFalse(null); |
| 35 | + $this->module->assertNotFalse('foo'); |
| 36 | + $this->module->assertFileExists(__FILE__); |
| 37 | + $this->module->assertFileNotExists(__FILE__ . '.notExist'); |
| 38 | + $this->module->assertInstanceOf('Exception', new Exception()); |
| 39 | + $this->module->assertInternalType('integer', 5); |
| 40 | + $this->module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]); |
| 41 | + $this->module->assertArraySubset(['foo' => [1]], ['foo' => [1, 2]]); |
| 42 | + $this->module->assertCount(3, [1, 2, 3]); |
36 | 43 | } |
37 | 44 |
|
38 | 45 | public function testExceptions() |
39 | 46 | { |
40 | | - $module = new \Codeception\Module\Asserts(make_container()); |
41 | | - $module->expectException('Exception', function () { |
| 47 | + $this->module->expectException('Exception', function () { |
42 | 48 | throw new Exception; |
43 | 49 | }); |
44 | | - $module->expectException(new Exception('here'), function () { |
| 50 | + $this->module->expectException(new Exception('here'), function () { |
45 | 51 | throw new Exception('here'); |
46 | 52 | }); |
47 | | - $module->expectException(new Exception('here', 200), function () { |
| 53 | + $this->module->expectException(new Exception('here', 200), function () { |
48 | 54 | throw new Exception('here', 200); |
49 | 55 | }); |
50 | 56 | } |
51 | 57 |
|
52 | | - /** |
53 | | - * @expectedException PHPUnit\Framework\AssertionFailedError |
54 | | - */ |
55 | 58 | public function testExceptionFails() |
56 | 59 | { |
57 | | - $module = new \Codeception\Module\Asserts(make_container()); |
58 | | - $module->expectException(new Exception('here', 200), function () { |
| 60 | + $this->expectException(PHPUnit\Framework\AssertionFailedError::class); |
| 61 | + |
| 62 | + $this->module->expectException(new Exception('here', 200), function () { |
59 | 63 | throw new Exception('here', 2); |
60 | 64 | }); |
61 | 65 | } |
62 | 66 |
|
63 | | - /** |
64 | | - * @expectedException PHPUnit\Framework\AssertionFailedError |
65 | | - * @expectedExceptionMessageRegExp /RuntimeException/ |
66 | | - */ |
67 | 67 | public function testOutputExceptionTimeWhenNothingCaught() |
68 | 68 | { |
69 | | - $module = new \Codeception\Module\Asserts(make_container()); |
70 | | - $module->expectException(RuntimeException::class, function () { |
| 69 | + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
| 70 | + $this->expectExceptionMessageRegExp('/RuntimeException/'); |
| 71 | + |
| 72 | + $this->module->expectException(RuntimeException::class, function () { |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + public function testExpectThrowable() |
| 77 | + { |
| 78 | + $this->module->expectThrowable('Exception', function () { |
| 79 | + throw new Exception(); |
| 80 | + }); |
| 81 | + $this->module->expectThrowable(new Exception('here'), function () { |
| 82 | + throw new Exception('here'); |
| 83 | + }); |
| 84 | + $this->module->expectThrowable(new Exception('here', 200), function () { |
| 85 | + throw new Exception('here', 200); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + public function testExpectThrowableFailOnDifferentClass() |
| 90 | + { |
| 91 | + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
| 92 | + |
| 93 | + $this->module->expectThrowable(new RuntimeException(), function () { |
| 94 | + throw new Exception(); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + public function testExpectThrowableFailOnDifferentMessage() |
| 99 | + { |
| 100 | + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
| 101 | + |
| 102 | + $this->module->expectThrowable(new Exception('foo', 200), function () { |
| 103 | + throw new Exception('bar', 200); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + public function testExpectThrowableFailOnDifferentCode() |
| 108 | + { |
| 109 | + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
| 110 | + |
| 111 | + $this->module->expectThrowable(new Exception('foobar', 200), function () { |
| 112 | + throw new Exception('foobar', 2); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + public function testExpectThrowableFailOnNothingCaught() |
| 117 | + { |
| 118 | + $this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
| 119 | + $this->expectExceptionMessageRegExp('/RuntimeException/'); |
| 120 | + |
| 121 | + $this->module->expectThrowable(RuntimeException::class, function () { |
71 | 122 | }); |
72 | 123 | } |
73 | 124 | } |
0 commit comments