1010use Codeception \Stub ;
1111use Exception ;
1212use PHPUnit \Framework \AssertionFailedError ;
13+ use PHPUnit \Framework \Constraint \IsEqual ;
14+ use PHPUnit \Framework \IncompleteTestError ;
15+ use PHPUnit \Framework \SkippedWithMessageException ;
1316use RuntimeException ;
1417use stdClass ;
1518
@@ -19,6 +22,8 @@ final class AssertsTest extends TestCase
1922
2023 public function _setUp ()
2124 {
25+ require_once codecept_data_dir ().'/DummyClass.php ' ;
26+
2227 /** @var ModuleContainer $container */
2328 $ container = Stub::make (ModuleContainer::class);
2429 $ this ->module = new Asserts ($ container );
@@ -27,151 +32,154 @@ public function _setUp()
2732 public function testCodeceptionAsserts ()
2833 {
2934 $ 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 );
3340 $ this ->module ->assertNotRegExp ('/^[a-z]$/ ' , '1 ' );
3441 $ this ->module ->assertRegExp ('/^[\d]$/ ' , '1 ' );
35- // assertThatItsNot
42+ $ this -> module -> assertThatItsNot ( 3 , new IsEqual ( 4 ));
3643 }
3744
3845 public function testPHPUnitAsserts ()
3946 {
4047 $ 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);
4653 $ 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 ()]);
5057 $ this ->module ->assertCount (3 , [1 , 2 , 3 ]);
51- // assertDirectoryDoesNotExist
52- // assertDirectoryExists
58+ $ this -> module -> assertDirectoryDoesNotExist ( __DIR__ . ' notExist ' );
59+ $ this -> module -> assertDirectoryExists ( __DIR__ );
5360 // assertDirectoryIsNotReadable
5461 // assertDirectoryIsNotWritable
55- // assertDirectoryIsReadable
56- // assertDirectoryIsWritable
62+ $ this -> module -> assertDirectoryIsReadable ( __DIR__ );
63+ $ this -> module -> assertDirectoryIsWritable ( __DIR__ );
5764 $ this ->module ->assertDoesNotMatchRegularExpression ('/^[a-z]$/ ' , '1 ' );
5865 $ this ->module ->assertEmpty ([]);
66+ $ this ->module ->assertEmpty (0 );
5967 $ this ->module ->assertEquals (1 , 1 );
6068 $ this ->module ->assertEqualsCanonicalizing ([3 , 2 , 1 ], [1 , 2 , 3 ]);
6169 $ this ->module ->assertEqualsIgnoringCase ('foo ' , 'FOO ' );
6270 $ this ->module ->assertEqualsWithDelta (1.0 , 1.01 , 0.1 );
6371 $ this ->module ->assertFalse (false );
6472 $ 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 ' );
6876 $ this ->module ->assertFileExists (__FILE__ );
6977 // assertFileIsNotReadable
7078 // 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 );
8088 $ this ->module ->assertInstanceOf ('Exception ' , new Exception ());
8189 $ this ->module ->assertIsArray ([1 , 2 , 3 ]);
8290 $ this ->module ->assertIsBool (true );
8391 $ this ->module ->assertIsCallable (function () {});
84- // assertIsClosedResource
92+ $ closedResource = fopen (__FILE__ , 'r ' );
93+ fclose ($ closedResource );
94+ $ this ->module ->assertIsClosedResource ($ closedResource );
8595 $ this ->module ->assertIsFloat (1.2 );
8696 $ this ->module ->assertIsInt (2 );
87- // assertIsIterable
97+ $ this -> module -> assertIsIterable ([]);
8898 $ this ->module ->assertIsNotArray (false );
8999 $ this ->module ->assertIsNotBool ([1 , 2 , 3 ]);
90100 $ this ->module ->assertIsNotCallable ('test ' );
91- // assertIsNotClosedResource
101+ $ openendResource = fopen (__FILE__ , 'r ' );
102+ $ this ->module ->assertIsNotClosedResource ($ openendResource );
92103 $ this ->module ->assertIsNotFloat (false );
93104 $ this ->module ->assertIsNotInt (false );
94- // assertIsNotIterable
105+ $ this -> module -> assertIsNotIterable ( ' test ' );
95106 $ this ->module ->assertIsNotNumeric (false );
96107 $ this ->module ->assertIsNotObject (false );
97- // assertIsNotReadable
108+ $ this -> module -> assertIsNotReadable ( __FILE__ . ' .notExist ' );
98109 $ this ->module ->assertIsNotResource (false );
99110 $ this ->module ->assertIsNotScalar (function () {});
100111 $ this ->module ->assertIsNotString (false );
101- // assertIsNotWritable
112+ $ this -> module -> assertIsNotWritable ( __FILE__ . ' .notExist ' );
102113 $ this ->module ->assertIsNumeric ('12.34 ' );
103114 $ this ->module ->assertIsObject (new stdClass ());
104- // assertIsReadable
115+ $ this -> module -> assertIsReadable ( __FILE__ );
105116 $ this ->module ->assertIsResource (fopen (__FILE__ , 'r ' ));
106117 $ this ->module ->assertIsScalar ('test ' );
107118 $ 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 );
118129 $ 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 ' ]);
124135 $ this ->module ->assertNotEmpty ([1 ]);
125- // assertNotEquals
136+ $ this -> module -> assertNotEquals ( true , false );
126137 $ this ->module ->assertNotEqualsCanonicalizing ([3 , 2 , 1 ], [2 , 3 , 0 , 1 ]);
127138 $ this ->module ->assertNotEqualsIgnoringCase ('foo ' , 'BAR ' );
128139 $ this ->module ->assertNotEqualsWithDelta (1.0 , 1.5 , 0.1 );
129140 $ this ->module ->assertNotFalse (true );
130141 $ this ->module ->assertNotFalse (null );
131142 $ this ->module ->assertNotFalse ('foo ' );
132- // assertNotInstanceOf
143+ $ this -> module -> assertNotInstanceOf (RuntimeException::class, new Exception ());
133144 $ this ->module ->assertNotNull ('' );
134145 $ this ->module ->assertNotNull (false );
135146 $ this ->module ->assertNotNull (0 );
136147 $ this ->module ->assertNotSame (1 , '1 ' );
137- // assertNotSameSize
148+ $ this -> module -> assertNotSameSize ([ 1 , 2 , 3 ], [ 1 , 1 , 2 , 3 ]);
138149 $ this ->module ->assertNotTrue (false );
139150 $ this ->module ->assertNotTrue (null );
140151 $ this ->module ->assertNotTrue ('foo ' );
141152 $ this ->module ->assertNull (null );
142- // assertObjectHasAttribute
143- // assertObjectNotHasAttribute
153+ $ this -> module -> assertObjectHasAttribute ( ' foo ' , new \ DummyClass ());
154+ $ this -> module -> assertObjectNotHasAttribute ( ' bar ' , new \ DummyClass ());
144155 $ this ->module ->assertSame (1 , 1 );
145- // assertSameSize
156+ $ this -> module -> assertSameSize ([ 1 , 2 , 3 ], [ 1 , 2 , 3 ]);
146157 $ this ->module ->assertStringContainsString ('bar ' , 'foobar ' );
147158 $ this ->module ->assertStringContainsStringIgnoringCase ('bar ' , 'FooBar ' );
148159 $ this ->module ->assertStringEndsNotWith ('fo ' , 'foo ' );
149160 $ 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" );
155166 $ this ->module ->assertStringNotContainsString ('baz ' , 'foobar ' );
156167 $ 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 " );
162173 $ this ->module ->assertStringStartsNotWith ('ba ' , 'foo ' );
163174 $ this ->module ->assertStringStartsWith ('fo ' , 'foo ' );
164- // assertThat
175+ $ this -> module -> assertThat ( 4 , new IsEqual ( 4 ));
165176 $ 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> ' );
175183 }
176184
177185 public function testExceptions ()
@@ -253,4 +261,28 @@ public function testExpectThrowableFailOnNothingCaught()
253261 $ this ->module ->expectThrowable (RuntimeException::class, function () {
254262 });
255263 }
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+ }
256288}
0 commit comments