Skip to content

Commit 60bce4b

Browse files
committed
Added support for full public PHPUnit API in Asserts.php
Removed unnecessary comments in tests, replaced expectException with expectThrowable
1 parent 06e8185 commit 60bce4b

File tree

2 files changed

+161
-92
lines changed

2 files changed

+161
-92
lines changed

src/Codeception/Module/Asserts.php

Lines changed: 134 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,157 @@
11
<?php
2+
23
namespace Codeception\Module;
34

45
use Codeception\Lib\Notification;
56
use Codeception\Module as CodeceptionModule;
67
use Codeception\Util\Shared\Asserts as SharedAsserts;
8+
use Exception;
9+
use Throwable;
710

811
/**
912
* Special module for using asserts in your tests.
1013
*/
1114
class Asserts extends CodeceptionModule
1215
{
1316
use SharedAsserts {
17+
assertArrayHasKey as public;
18+
assertArrayNotHasKey as public;
19+
assertClassHasAttribute as public;
20+
assertClassHasStaticAttribute as public;
21+
assertClassNotHasAttribute as public;
22+
assertClassNotHasStaticAttribute as public;
23+
assertContains as public;
24+
assertContainsEquals as public;
25+
assertContainsOnly as public;
26+
assertContainsOnlyInstancesOf as public;
27+
assertCount as public;
28+
assertDirectoryDoesNotExist as public;
29+
assertDirectoryExists as public;
30+
assertDirectoryIsNotReadable as public;
31+
assertDirectoryIsNotWritable as public;
32+
assertDirectoryIsReadable as public;
33+
assertDirectoryIsWritable as public;
34+
assertDoesNotMatchRegularExpression as public;
35+
assertEmpty as public;
1436
assertEquals as public;
15-
assertNotEquals as public;
1637
assertEqualsCanonicalizing as public;
17-
assertNotEqualsCanonicalizing as public;
1838
assertEqualsIgnoringCase as public;
19-
assertNotEqualsIgnoringCase as public;
2039
assertEqualsWithDelta as public;
21-
assertNotEqualsWithDelta as public;
22-
assertSame as public;
23-
assertNotSame as public;
40+
assertFalse as public;
41+
assertFileDoesNotExist as public;
42+
assertFileEquals as public;
43+
assertFileEqualsCanonicalizing as public;
44+
assertFileEqualsIgnoringCase as public;
45+
assertFileExists as public;
46+
assertFileIsNotReadable as public;
47+
assertFileIsNotWritable as public;
48+
assertFileIsReadable as public;
49+
assertFileIsWritable as public;
50+
assertFileNotEquals as public;
51+
assertFileNotEqualsCanonicalizing as public;
52+
assertFileNotEqualsIgnoringCase as public;
53+
assertFileNotExists as public;
54+
assertFinite as public;
55+
assertGreaterOrEquals as public;
2456
assertGreaterThan as public;
2557
assertGreaterThanOrEqual as public;
58+
assertInfinite as public;
59+
assertInstanceOf as public;
60+
assertIsArray as public;
61+
assertIsBool as public;
62+
assertIsCallable as public;
63+
assertIsClosedResource as public;
64+
assertIsEmpty as public;
65+
assertIsFloat as public;
66+
assertIsInt as public;
67+
assertIsIterable as public;
68+
assertIsNotArray as public;
69+
assertIsNotBool as public;
70+
assertIsNotCallable as public;
71+
assertIsNotClosedResource as public;
72+
assertIsNotFloat as public;
73+
assertIsNotInt as public;
74+
assertIsNotIterable as public;
75+
assertIsNotNumeric as public;
76+
assertIsNotObject as public;
77+
assertIsNotReadable as public;
78+
assertIsNotResource as public;
79+
assertIsNotScalar as public;
80+
assertIsNotString as public;
81+
assertIsNotWritable as public;
82+
assertIsNumeric as public;
83+
assertIsObject as public;
84+
assertIsReadable as public;
85+
assertIsResource as public;
86+
assertIsScalar as public;
87+
assertIsString as public;
88+
assertIsWritable as public;
89+
assertJson as public;
90+
assertJsonFileEqualsJsonFile as public;
91+
assertJsonFileNotEqualsJsonFile as public;
92+
assertJsonStringEqualsJsonFile as public;
93+
assertJsonStringEqualsJsonString as public;
94+
assertJsonStringNotEqualsJsonFile as public;
95+
assertJsonStringNotEqualsJsonString as public;
96+
assertLessOrEquals as public;
2697
assertLessThan as public;
2798
assertLessThanOrEqual as public;
28-
assertContains as public;
29-
assertNotContains as public;
30-
assertRegExp as public;
31-
assertNotRegExp as public;
3299
assertMatchesRegularExpression as public;
33-
assertDoesNotMatchRegularExpression as public;
34-
assertEmpty as public;
100+
assertNan as public;
101+
assertNotContains as public;
102+
assertNotContainsEquals as public;
103+
assertNotContainsOnly as public;
104+
assertNotCount as public;
35105
assertNotEmpty as public;
36-
assertNull as public;
106+
assertNotEquals as public;
107+
assertNotEqualsCanonicalizing as public;
108+
assertNotEqualsIgnoringCase as public;
109+
assertNotEqualsWithDelta as public;
110+
assertNotFalse as public;
111+
assertNotInstanceOf as public;
37112
assertNotNull as public;
113+
assertNotRegExp as public;
114+
assertNotSame as public;
115+
assertNotSameSize as public;
116+
assertNotTrue as public;
117+
assertNull as public;
118+
assertObjectHasAttribute as public;
119+
assertObjectNotHasAttribute as public;
120+
assertRegExp as public;
121+
assertSame as public;
122+
assertSameSize as public;
123+
assertStringContainsString as public;
124+
assertStringContainsStringIgnoringCase as public;
125+
assertStringEndsNotWith as public;
126+
assertStringEndsWith as public;
127+
assertStringEqualsFile as public;
128+
assertStringEqualsFileCanonicalizing as public;
129+
assertStringEqualsFileIgnoringCase as public;
130+
assertStringMatchesFormat as public;
131+
assertStringMatchesFormatFile as public;
132+
assertStringNotContainsString as public;
133+
assertStringNotContainsStringIgnoringCase as public;
134+
assertStringNotEqualsFile as public;
135+
assertStringNotEqualsFileCanonicalizing as public;
136+
assertStringNotEqualsFileIgnoringCase as public;
137+
assertStringNotMatchesFormat as public;
138+
assertStringNotMatchesFormatFile as public;
139+
assertStringStartsNotWith as public;
140+
assertStringStartsWith as public;
141+
assertThat as public;
142+
assertThatItsNot as public;
38143
assertTrue as public;
39-
assertFalse as public;
40-
assertFileExists as public;
41-
assertFileNotExists as public;
42-
assertFileDoesNotExist as public;
43-
assertGreaterOrEquals as public;
44-
assertLessOrEquals as public;
45-
assertIsEmpty as public;
46-
assertArrayHasKey as public;
47-
assertArrayNotHasKey as public;
48-
assertInstanceOf as public;
49-
assertNotInstanceOf as public;
50-
assertInternalType as public;
51-
assertCount as public;
52-
assertStringStartsWith as public;
53-
assertStringStartsNotWith as public;
54-
assertStringEndsWith as public;
55-
assertStringEndsNotWith as public;
56-
assertNotTrue as public;
57-
assertNotFalse as public;
58-
assertStringContainsString as public;
59-
assertStringContainsStringIgnoringCase as public;
60-
assertStringNotContainsString as public;
61-
assertStringNotContainsStringIgnoringCase as public;
62-
assertIsArray as public;
63-
assertIsBool as public;
64-
assertIsFloat as public;
65-
assertIsInt as public;
66-
assertIsNumeric as public;
67-
assertIsObject as public;
68-
assertIsResource as public;
69-
assertIsString as public;
70-
assertIsScalar as public;
71-
assertIsCallable as public;
72-
assertIsNotArray as public;
73-
assertIsNotBool as public;
74-
assertIsNotFloat as public;
75-
assertIsNotInt as public;
76-
assertIsNotNumeric as public;
77-
assertIsNotObject as public;
78-
assertIsNotResource as public;
79-
assertIsNotString as public;
80-
assertIsNotScalar as public;
81-
assertIsNotCallable as public;
144+
assertXmlFileEqualsXmlFile as public;
145+
assertXmlFileNotEqualsXmlFile as public;
146+
assertXmlStringEqualsXmlFile as public;
147+
assertXmlStringEqualsXmlString as public;
148+
assertXmlStringNotEqualsXmlFile as public;
149+
assertXmlStringNotEqualsXmlString as public;
82150
fail as public;
151+
markTestIncomplete as public;
152+
markTestSkipped as public;
83153
}
84154

85-
86-
87155
/**
88156
* Handles and checks exception called inside callback function.
89157
* Either exception class name or exception instance should be provided.
@@ -108,8 +176,8 @@ class Asserts extends CodeceptionModule
108176
* ```
109177
*
110178
* @deprecated Use expectThrowable() instead
111-
* @param $exception string or \Exception
112-
* @param $callback
179+
* @param Exception|string $exception
180+
* @param callable $callback
113181
*/
114182
public function expectException($exception, $callback)
115183
{
@@ -140,31 +208,28 @@ public function expectException($exception, $callback)
140208
* });
141209
* ```
142210
*
143-
* @param $throwable string or \Throwable
144-
* @param $callback
211+
* @param Throwable|string $throwable
212+
* @param callable $callback
145213
*/
146214
public function expectThrowable($throwable, $callback)
147215
{
148216
if (is_object($throwable)) {
149-
/** @var $throwable \Throwable */
150217
$class = get_class($throwable);
151218
$msg = $throwable->getMessage();
152219
$code = $throwable->getCode();
153220
} else {
154-
$class= $throwable;
221+
$class = $throwable;
155222
$msg = null;
156223
$code = null;
157224
}
158225

159226
try {
160227
$callback();
161-
} catch (\Exception $t) {
228+
} catch (Exception $t) {
162229
$this->checkThrowable($t, $class, $msg, $code);
163-
164230
return;
165-
} catch (\Throwable $t) {
231+
} catch (Throwable $t) {
166232
$this->checkThrowable($t, $class, $msg, $code);
167-
168233
return;
169234
}
170235

@@ -175,7 +240,7 @@ public function expectThrowable($throwable, $callback)
175240
* Check if the given throwable matches the expected data,
176241
* fail (throws an exception) if it does not.
177242
*
178-
* @param \Throwable $throwable
243+
* @param Throwable $throwable
179244
* @param string $expectedClass
180245
* @param string $expectedMsg
181246
* @param int $expectedCode

tests/unit/Codeception/Module/AssertsTest.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
<?php
22

3-
function make_container()
3+
namespace unit\Codeception\Module;
4+
5+
use Codeception\Lib\ModuleContainer;
6+
use Codeception\Module\Asserts;
7+
use Codeception\PHPUnit\TestCase;
8+
use Codeception\Util\Stub;
9+
use Exception;
10+
use PHPUnit\Framework\AssertionFailedError;
11+
use RuntimeException;
12+
use stdClass;
13+
14+
class AssertsTest extends TestCase
415
{
5-
return \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
6-
}
7-
8-
class AssertsTest extends \Codeception\PHPUnit\TestCase
9-
{
10-
/** @var \Codeception\Module\Asserts */
16+
/** @var Asserts */
1117
protected $module;
1218

1319
public function _setUp()
1420
{
15-
$this->module = new \Codeception\Module\Asserts(make_container());
21+
/** @var ModuleContainer $container */
22+
$container = Stub::make(ModuleContainer::class);
23+
$this->module = new Asserts($container);
1624
}
1725

1826
public function testAsserts()
@@ -47,11 +55,7 @@ public function testAsserts()
4755
$this->module->assertFileNotExists(__FILE__ . '.notExist');
4856
$this->module->assertFileDoesNotExist(__FILE__ . '.notExist');
4957
$this->module->assertInstanceOf('Exception', new Exception());
50-
//assertInternalType is deprecated and will be removed in PHPUnit 9
51-
//$this->module->assertInternalType('integer', 5);
5258
$this->module->assertArrayHasKey('one', ['one' => 1, 'two' => 2]);
53-
//assertArraySubset is deprecated and will be removed in PHPUnit 9
54-
//$this->module->assertArraySubset(['foo' => [1]], ['foo' => [1, 2]]);
5559
$this->module->assertCount(3, [1, 2, 3]);
5660

5761
$this->module->assertStringContainsString('bar', 'foobar');
@@ -91,32 +95,32 @@ public function testAsserts()
9195

9296
public function testExceptions()
9397
{
94-
$this->module->expectException('Exception', function () {
98+
$this->module->expectThrowable('Exception', function () {
9599
throw new Exception;
96100
});
97-
$this->module->expectException(new Exception('here'), function () {
101+
$this->module->expectThrowable(new Exception('here'), function () {
98102
throw new Exception('here');
99103
});
100-
$this->module->expectException(new Exception('here', 200), function () {
104+
$this->module->expectThrowable(new Exception('here', 200), function () {
101105
throw new Exception('here', 200);
102106
});
103107
}
104108

105109
public function testExceptionFails()
106110
{
107-
$this->expectException(PHPUnit\Framework\AssertionFailedError::class);
111+
$this->expectException(AssertionFailedError::class);
108112

109-
$this->module->expectException(new Exception('here', 200), function () {
113+
$this->module->expectThrowable(new Exception('here', 200), function () {
110114
throw new Exception('here', 2);
111115
});
112116
}
113117

114118
public function testOutputExceptionTimeWhenNothingCaught()
115119
{
116-
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
120+
$this->expectException(AssertionFailedError::class);
117121
$this->expectExceptionMessageRegExp('/RuntimeException/');
118122

119-
$this->module->expectException(RuntimeException::class, function () {
123+
$this->module->expectThrowable(RuntimeException::class, function () {
120124
});
121125
}
122126

@@ -135,7 +139,7 @@ public function testExpectThrowable()
135139

136140
public function testExpectThrowableFailOnDifferentClass()
137141
{
138-
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
142+
$this->expectException(AssertionFailedError::class);
139143

140144
$this->module->expectThrowable(new RuntimeException(), function () {
141145
throw new Exception();
@@ -144,7 +148,7 @@ public function testExpectThrowableFailOnDifferentClass()
144148

145149
public function testExpectThrowableFailOnDifferentMessage()
146150
{
147-
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
151+
$this->expectException(AssertionFailedError::class);
148152

149153
$this->module->expectThrowable(new Exception('foo', 200), function () {
150154
throw new Exception('bar', 200);
@@ -153,7 +157,7 @@ public function testExpectThrowableFailOnDifferentMessage()
153157

154158
public function testExpectThrowableFailOnDifferentCode()
155159
{
156-
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
160+
$this->expectException(AssertionFailedError::class);
157161

158162
$this->module->expectThrowable(new Exception('foobar', 200), function () {
159163
throw new Exception('foobar', 2);
@@ -162,7 +166,7 @@ public function testExpectThrowableFailOnDifferentCode()
162166

163167
public function testExpectThrowableFailOnNothingCaught()
164168
{
165-
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
169+
$this->expectException(AssertionFailedError::class);
166170
$this->expectExceptionMessageRegExp('/RuntimeException/');
167171

168172
$this->module->expectThrowable(RuntimeException::class, function () {

0 commit comments

Comments
 (0)