10
10
use Codeception \Stub ;
11
11
use Exception ;
12
12
use PHPUnit \Framework \AssertionFailedError ;
13
+ use PHPUnit \Framework \Constraint \IsEqual ;
14
+ use PHPUnit \Framework \IncompleteTestError ;
15
+ use PHPUnit \Framework \SkippedWithMessageException ;
13
16
use RuntimeException ;
14
17
use stdClass ;
15
18
@@ -19,6 +22,8 @@ final class AssertsTest extends TestCase
19
22
20
23
public function _setUp ()
21
24
{
25
+ require_once codecept_data_dir ().'/DummyClass.php ' ;
26
+
22
27
/** @var ModuleContainer $container */
23
28
$ container = Stub::make (ModuleContainer::class);
24
29
$ this ->module = new Asserts ($ container );
@@ -27,151 +32,154 @@ public function _setUp()
27
32
public function testCodeceptionAsserts ()
28
33
{
29
34
$ this ->module ->assertFileNotExists (__FILE__ . '.notExist ' );
30
- // assertGreaterOrEquals
31
- // assertIsEmpty
32
- // assertLessOrEquals
35
+ $ this ->module ->assertGreaterOrEquals (2 , 2 );
36
+ $ this ->module ->assertGreaterOrEquals (2 , 3 );
37
+ $ this ->module ->assertIsEmpty ([]);
38
+ $ this ->module ->assertLessOrEquals (2 , 1 );
39
+ $ this ->module ->assertLessOrEquals (2 , 2 );
33
40
$ this ->module ->assertNotRegExp ('/^[a-z]$/ ' , '1 ' );
34
41
$ this ->module ->assertRegExp ('/^[\d]$/ ' , '1 ' );
35
- // assertThatItsNot
42
+ $ this -> module -> assertThatItsNot ( 3 , new IsEqual ( 4 ));
36
43
}
37
44
38
45
public function testPHPUnitAsserts ()
39
46
{
40
47
$ this ->module ->assertArrayHasKey ('one ' , ['one ' => 1 , 'two ' => 2 ]);
41
- // assertArrayNotHasKey
42
- // assertClassHasAttribute
43
- // assertClassHasStaticAttribute
44
- // assertClassNotHasAttribute
45
- // assertClassNotHasStaticAttribute
48
+ $ this -> module -> assertArrayNotHasKey ( ' three ' , [ ' one ' => 1 , ' two ' => 2 ]);
49
+ $ this -> module -> assertClassHasAttribute ( ' foo ' , \DummyClass::class);
50
+ $ this -> module -> assertClassHasStaticAttribute ( ' staticFoo ' , \DummyClass::class);
51
+ $ this -> module -> assertClassNotHasAttribute ( ' bar ' , \DummyClass::class);
52
+ $ this -> module -> assertClassNotHasStaticAttribute ( ' staticBar ' , \DummyClass::class);
46
53
$ this ->module ->assertContains (1 , [1 , 2 ]);
47
- // assertContainsEquals
48
- // assertContainsOnly
49
- // assertContainsOnlyInstancesOf
54
+ $ this -> module -> assertContainsEquals ( 2 , [ 1 , 2 ]);
55
+ $ this -> module -> assertContainsOnly (\DummyClass::class, [ new \ DummyClass (), new \ DummyClass ()]);
56
+ $ this -> module -> assertContainsOnlyInstancesOf (\DummyClass::class, [ new \ DummyClass (), new \ DummyClass ()]);
50
57
$ this ->module ->assertCount (3 , [1 , 2 , 3 ]);
51
- // assertDirectoryDoesNotExist
52
- // assertDirectoryExists
58
+ $ this -> module -> assertDirectoryDoesNotExist ( __DIR__ . ' notExist ' );
59
+ $ this -> module -> assertDirectoryExists ( __DIR__ );
53
60
// assertDirectoryIsNotReadable
54
61
// assertDirectoryIsNotWritable
55
- // assertDirectoryIsReadable
56
- // assertDirectoryIsWritable
62
+ $ this -> module -> assertDirectoryIsReadable ( __DIR__ );
63
+ $ this -> module -> assertDirectoryIsWritable ( __DIR__ );
57
64
$ this ->module ->assertDoesNotMatchRegularExpression ('/^[a-z]$/ ' , '1 ' );
58
65
$ this ->module ->assertEmpty ([]);
66
+ $ this ->module ->assertEmpty (0 );
59
67
$ this ->module ->assertEquals (1 , 1 );
60
68
$ this ->module ->assertEqualsCanonicalizing ([3 , 2 , 1 ], [1 , 2 , 3 ]);
61
69
$ this ->module ->assertEqualsIgnoringCase ('foo ' , 'FOO ' );
62
70
$ this ->module ->assertEqualsWithDelta (1.0 , 1.01 , 0.1 );
63
71
$ this ->module ->assertFalse (false );
64
72
$ this ->module ->assertFileDoesNotExist (__FILE__ . '.notExist ' );
65
- // assertFileEquals
66
- // assertFileEqualsCanonicalizing
67
- // assertFileEqualsIgnoringCase
73
+ $ this -> module -> assertFileEquals ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data1.txt ' );
74
+ $ this -> module -> assertFileEqualsCanonicalizing ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data1.txt ' );
75
+ $ this -> module -> assertFileEqualsIgnoringCase ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data2.txt ' );
68
76
$ this ->module ->assertFileExists (__FILE__ );
69
77
// assertFileIsNotReadable
70
78
// assertFileIsNotWritable
71
- // assertFileIsReadable
72
- // assertFileIsWritable
73
- // assertFileNotEquals
74
- // assertFileNotEqualsCanonicalizing
75
- // assertFileNotEqualsIgnoringCase
76
- // assertFinite
77
- // assertGreaterThan
78
- // assertGreaterThanOrEqual
79
- // assertInfinite
79
+ $ this -> module -> assertFileIsReadable ( __FILE__ );
80
+ $ this -> module -> assertFileIsWritable ( __FILE__ );
81
+ $ this -> module -> assertFileNotEquals ( codecept_data_dir (). ' /data1.json ' , codecept_data_dir (). ' /data1.txt ' );
82
+ $ this -> module -> assertFileNotEqualsCanonicalizing ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data3.txt ' );
83
+ $ this -> module -> assertFileNotEqualsIgnoringCase ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data3.txt ' );
84
+ $ this -> module -> assertFinite ( 2 );
85
+ $ this -> module -> assertGreaterThan ( 5 , 6 );
86
+ $ this -> module -> assertGreaterThanOrEqual ( 5 , 5 );
87
+ $ this -> module -> assertInfinite ( 1e308 * 2 );
80
88
$ this ->module ->assertInstanceOf ('Exception ' , new Exception ());
81
89
$ this ->module ->assertIsArray ([1 , 2 , 3 ]);
82
90
$ this ->module ->assertIsBool (true );
83
91
$ this ->module ->assertIsCallable (function () {});
84
- // assertIsClosedResource
92
+ $ closedResource = fopen (__FILE__ , 'r ' );
93
+ fclose ($ closedResource );
94
+ $ this ->module ->assertIsClosedResource ($ closedResource );
85
95
$ this ->module ->assertIsFloat (1.2 );
86
96
$ this ->module ->assertIsInt (2 );
87
- // assertIsIterable
97
+ $ this -> module -> assertIsIterable ([]);
88
98
$ this ->module ->assertIsNotArray (false );
89
99
$ this ->module ->assertIsNotBool ([1 , 2 , 3 ]);
90
100
$ this ->module ->assertIsNotCallable ('test ' );
91
- // assertIsNotClosedResource
101
+ $ openendResource = fopen (__FILE__ , 'r ' );
102
+ $ this ->module ->assertIsNotClosedResource ($ openendResource );
92
103
$ this ->module ->assertIsNotFloat (false );
93
104
$ this ->module ->assertIsNotInt (false );
94
- // assertIsNotIterable
105
+ $ this -> module -> assertIsNotIterable ( ' test ' );
95
106
$ this ->module ->assertIsNotNumeric (false );
96
107
$ this ->module ->assertIsNotObject (false );
97
- // assertIsNotReadable
108
+ $ this -> module -> assertIsNotReadable ( __FILE__ . ' .notExist ' );
98
109
$ this ->module ->assertIsNotResource (false );
99
110
$ this ->module ->assertIsNotScalar (function () {});
100
111
$ this ->module ->assertIsNotString (false );
101
- // assertIsNotWritable
112
+ $ this -> module -> assertIsNotWritable ( __FILE__ . ' .notExist ' );
102
113
$ this ->module ->assertIsNumeric ('12.34 ' );
103
114
$ this ->module ->assertIsObject (new stdClass ());
104
- // assertIsReadable
115
+ $ this -> module -> assertIsReadable ( __FILE__ );
105
116
$ this ->module ->assertIsResource (fopen (__FILE__ , 'r ' ));
106
117
$ this ->module ->assertIsScalar ('test ' );
107
118
$ this ->module ->assertIsString ('test ' );
108
- // assertIsWritable
109
- // assertJson
110
- // assertJsonFileEqualsJsonFile
111
- // assertJsonFileNotEqualsJsonFile
112
- // assertJsonStringEqualsJsonFile
113
- // assertJsonStringEqualsJsonString
114
- // assertJsonStringNotEqualsJsonFile
115
- // assertJsonStringNotEqualsJsonString
116
- // assertLessThan
117
- // assertLessThanOrEqual
119
+ $ this -> module -> assertIsWritable ( __FILE__ );
120
+ $ this -> module -> assertJson ( ' [] ' );
121
+ $ this -> module -> assertJsonFileEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , codecept_data_dir (). ' /data1.json ' );
122
+ $ this -> module -> assertJsonFileNotEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , codecept_data_dir (). ' /data2.json ' );
123
+ $ this -> module -> assertJsonStringEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , ' ["foo", "bar"] ' );
124
+ $ this -> module -> assertJsonStringEqualsJsonString ( ' ["foo", "bar"] ' , ' [ "foo" , "bar" ] ' );
125
+ $ this -> module -> assertJsonStringNotEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , ' ["bar", "foo"] ' );
126
+ $ this -> module -> assertJsonStringNotEqualsJsonString ( ' ["foo", "bar"] ' , ' ["bar", "foo"] ' );
127
+ $ this -> module -> assertLessThan ( 4 , 3 );
128
+ $ this -> module -> assertLessThanOrEqual ( 3 , 3 );
118
129
$ this ->module ->assertMatchesRegularExpression ('/^[\d]$/ ' , '1 ' );
119
- // assertNan
120
- // assertNotContains
121
- // assertNotContainsEquals
122
- // assertNotContainsOnly
123
- // assertNotCount
130
+ $ this -> module -> assertNan ( sqrt (- 1 ));
131
+ $ this -> module -> assertNotContains ( ' three ' , [ ' one ' , ' two ' ]);
132
+ $ this -> module -> assertNotContainsEquals ( 3 , [ 1 , 2 ]);
133
+ $ this -> module -> assertNotContainsOnly (\DummyClass::class, [ new \ DummyClass (), new Exception ()]);
134
+ $ this -> module -> assertNotCount ( 1 , [ ' one ' , ' two ' ]);
124
135
$ this ->module ->assertNotEmpty ([1 ]);
125
- // assertNotEquals
136
+ $ this -> module -> assertNotEquals ( true , false );
126
137
$ this ->module ->assertNotEqualsCanonicalizing ([3 , 2 , 1 ], [2 , 3 , 0 , 1 ]);
127
138
$ this ->module ->assertNotEqualsIgnoringCase ('foo ' , 'BAR ' );
128
139
$ this ->module ->assertNotEqualsWithDelta (1.0 , 1.5 , 0.1 );
129
140
$ this ->module ->assertNotFalse (true );
130
141
$ this ->module ->assertNotFalse (null );
131
142
$ this ->module ->assertNotFalse ('foo ' );
132
- // assertNotInstanceOf
143
+ $ this -> module -> assertNotInstanceOf (RuntimeException::class, new Exception ());
133
144
$ this ->module ->assertNotNull ('' );
134
145
$ this ->module ->assertNotNull (false );
135
146
$ this ->module ->assertNotNull (0 );
136
147
$ this ->module ->assertNotSame (1 , '1 ' );
137
- // assertNotSameSize
148
+ $ this -> module -> assertNotSameSize ([ 1 , 2 , 3 ], [ 1 , 1 , 2 , 3 ]);
138
149
$ this ->module ->assertNotTrue (false );
139
150
$ this ->module ->assertNotTrue (null );
140
151
$ this ->module ->assertNotTrue ('foo ' );
141
152
$ this ->module ->assertNull (null );
142
- // assertObjectHasAttribute
143
- // assertObjectNotHasAttribute
153
+ $ this -> module -> assertObjectHasAttribute ( ' foo ' , new \ DummyClass ());
154
+ $ this -> module -> assertObjectNotHasAttribute ( ' bar ' , new \ DummyClass ());
144
155
$ this ->module ->assertSame (1 , 1 );
145
- // assertSameSize
156
+ $ this -> module -> assertSameSize ([ 1 , 2 , 3 ], [ 1 , 2 , 3 ]);
146
157
$ this ->module ->assertStringContainsString ('bar ' , 'foobar ' );
147
158
$ this ->module ->assertStringContainsStringIgnoringCase ('bar ' , 'FooBar ' );
148
159
$ this ->module ->assertStringEndsNotWith ('fo ' , 'foo ' );
149
160
$ this ->module ->assertStringEndsWith ('oo ' , 'foo ' );
150
- // assertStringEqualsFile
151
- // assertStringEqualsFileCanonicalizing
152
- // assertStringEqualsFileIgnoringCase
153
- // assertStringMatchesFormat
154
- // assertStringMatchesFormatFile
161
+ $ this -> module -> assertStringEqualsFile ( codecept_data_dir (). ' /data1.txt ' , ' foo bar foo ' );
162
+ $ this -> module -> assertStringEqualsFileCanonicalizing ( codecept_data_dir (). ' /data1.txt ' , ' foo bar foo ' );
163
+ $ this -> module -> assertStringEqualsFileIgnoringCase ( codecept_data_dir (). ' /data1.txt ' , ' foo bAr foo ' );
164
+ $ this -> module -> assertStringMatchesFormat ( ' *%s* ' , ' *** ' );
165
+ $ this -> module -> assertStringMatchesFormatFile ( codecept_data_dir (). ' /expectedFileFormat.txt ' , " FOO \n" );
155
166
$ this ->module ->assertStringNotContainsString ('baz ' , 'foobar ' );
156
167
$ this ->module ->assertStringNotContainsStringIgnoringCase ('baz ' , 'FooBar ' );
157
- // assertStringNotEqualsFile
158
- // assertStringNotEqualsFileCanonicalizing
159
- // assertStringNotEqualsFileIgnoringCase
160
- // assertStringNotMatchesFormat
161
- // assertStringNotMatchesFormatFile
168
+ $ this -> module -> assertStringNotEqualsFile ( codecept_data_dir (). ' /data2.txt ' , ' foo bar foo ' );
169
+ $ this -> module -> assertStringNotEqualsFileCanonicalizing ( codecept_data_dir (). ' /data3.txt ' , ' foo bar foo ' );
170
+ $ this -> module -> assertStringNotEqualsFileIgnoringCase ( codecept_data_dir (). ' /data3.txt ' , ' foo bar foo ' );
171
+ $ this -> module -> assertStringNotMatchesFormat ( ' *%s* ' , ' ** ' );
172
+ $ this -> module -> assertStringNotMatchesFormatFile ( codecept_data_dir (). ' /expectedFileFormat.txt ' , " FO " );
162
173
$ this ->module ->assertStringStartsNotWith ('ba ' , 'foo ' );
163
174
$ this ->module ->assertStringStartsWith ('fo ' , 'foo ' );
164
- // assertThat
175
+ $ this -> module -> assertThat ( 4 , new IsEqual ( 4 ));
165
176
$ this ->module ->assertTrue (true );
166
- // assertXmlFileEqualsXmlFile
167
- // assertXmlFileNotEqualsXmlFile
168
- // assertXmlStringEqualsXmlFile
169
- // assertXmlStringEqualsXmlString
170
- // assertXmlStringNotEqualsXmlFile
171
- // assertXmlStringNotEqualsXmlString
172
- // fail
173
- // markTestIncomplete
174
- // markTestSkipped
177
+ $ this ->module ->assertXmlFileEqualsXmlFile (codecept_data_dir ().'/data1.xml ' , codecept_data_dir ().'/data1.xml ' );
178
+ $ this ->module ->assertXmlFileNotEqualsXmlFile (codecept_data_dir ().'/data1.xml ' , codecept_data_dir ().'/data2.xml ' );
179
+ $ this ->module ->assertXmlStringEqualsXmlFile (codecept_data_dir ().'/data1.xml ' , ' <foo>foo</foo> ' );
180
+ $ this ->module ->assertXmlStringEqualsXmlString ('<foo>foo</foo> ' , ' <foo>foo</foo> ' );
181
+ $ this ->module ->assertXmlStringNotEqualsXmlFile (codecept_data_dir ().'/data1.xml ' , '<foo>bar</foo> ' );
182
+ $ this ->module ->assertXmlStringNotEqualsXmlString ('<foo>foo</foo> ' , '<foo>bar</foo> ' );
175
183
}
176
184
177
185
public function testExceptions ()
@@ -253,4 +261,28 @@ public function testExpectThrowableFailOnNothingCaught()
253
261
$ this ->module ->expectThrowable (RuntimeException::class, function () {
254
262
});
255
263
}
264
+
265
+ public function testFail ()
266
+ {
267
+ $ this ->expectException (AssertionFailedError::class);
268
+ $ this ->expectExceptionMessage ('foobar ' );
269
+
270
+ $ this ->module ->fail ('foobar ' );
271
+ }
272
+
273
+ public function testMarkTestIncomplete ()
274
+ {
275
+ $ this ->expectException (IncompleteTestError::class);
276
+ $ this ->expectExceptionMessage ('foobar ' );
277
+
278
+ $ this ->module ->markTestIncomplete ('foobar ' );
279
+ }
280
+
281
+ public function testMarkTestSkipped ()
282
+ {
283
+ $ this ->expectException (SkippedWithMessageException::class);
284
+ $ this ->expectExceptionMessage ('foobar ' );
285
+
286
+ $ this ->module ->markTestSkipped ('foobar ' );
287
+ }
256
288
}
0 commit comments