1010use Codeception \Stub ;
1111use Exception ;
1212use PHPUnit \Framework \AssertionFailedError ;
13+ use PHPUnit \Framework \Constraint \IsEqual ;
1314use RuntimeException ;
1415use stdClass ;
1516
@@ -19,6 +20,8 @@ final class AssertsTest extends TestCase
1920
2021 public function _setUp ()
2122 {
23+ require_once codecept_data_dir ().'/DummyClass.php ' ;
24+
2225 /** @var ModuleContainer $container */
2326 $ container = Stub::make (ModuleContainer::class);
2427 $ this ->module = new Asserts ($ container );
@@ -27,148 +30,154 @@ public function _setUp()
2730 public function testCodeceptionAsserts ()
2831 {
2932 $ this ->module ->assertFileNotExists (__FILE__ . '.notExist ' );
30- // assertGreaterOrEquals
31- // assertIsEmpty
32- // assertLessOrEquals
33+ $ this ->module ->assertGreaterOrEquals (2 , 2 );
34+ $ this ->module ->assertGreaterOrEquals (2 , 3 );
35+ $ this ->module ->assertIsEmpty ([]);
36+ $ this ->module ->assertLessOrEquals (2 , 1 );
37+ $ this ->module ->assertLessOrEquals (2 , 2 );
3338 $ this ->module ->assertNotRegExp ('/^[a-z]$/ ' , '1 ' );
3439 $ this ->module ->assertRegExp ('/^[\d]$/ ' , '1 ' );
35- // assertThatItsNot
40+ $ this -> module -> assertThatItsNot ( 3 , new IsEqual ( 4 ));
3641 }
3742
3843 public function testPHPUnitAsserts ()
3944 {
4045 $ this ->module ->assertArrayHasKey ('one ' , ['one ' => 1 , 'two ' => 2 ]);
41- // assertArrayNotHasKey
42- // assertClassHasAttribute
43- // assertClassHasStaticAttribute
44- // assertClassNotHasAttribute
45- // assertClassNotHasStaticAttribute
46+ $ this -> module -> assertArrayNotHasKey ( ' three ' , [ ' one ' => 1 , ' two ' => 2 ]);
47+ $ this -> module -> assertClassHasAttribute ( ' foo ' , \DummyClass::class);
48+ $ this -> module -> assertClassHasStaticAttribute ( ' staticFoo ' , \DummyClass::class);
49+ $ this -> module -> assertClassNotHasAttribute ( ' bar ' , \DummyClass::class);
50+ $ this -> module -> assertClassNotHasStaticAttribute ( ' staticBar ' , \DummyClass::class);
4651 $ this ->module ->assertContains (1 , [1 , 2 ]);
47- // assertContainsEquals
48- // assertContainsOnly
49- // assertContainsOnlyInstancesOf
52+ $ this -> module -> assertContainsEquals ( 2 , [ 1 , 2 ]);
53+ $ this -> module -> assertContainsOnly (\DummyClass::class, [ new \ DummyClass (), new \ DummyClass ()]);
54+ $ this -> module -> assertContainsOnlyInstancesOf (\DummyClass::class, [ new \ DummyClass (), new \ DummyClass ()]);
5055 $ this ->module ->assertCount (3 , [1 , 2 , 3 ]);
51- // assertDirectoryDoesNotExist
52- // assertDirectoryExists
56+ $ this -> module -> assertDirectoryDoesNotExist ( __DIR__ . ' notExist ' );
57+ $ this -> module -> assertDirectoryExists ( __DIR__ );
5358 // assertDirectoryIsNotReadable
5459 // assertDirectoryIsNotWritable
55- // assertDirectoryIsReadable
56- // assertDirectoryIsWritable
60+ $ this -> module -> assertDirectoryIsReadable ( __DIR__ );
61+ $ this -> module -> assertDirectoryIsWritable ( __DIR__ );
5762 $ this ->module ->assertDoesNotMatchRegularExpression ('/^[a-z]$/ ' , '1 ' );
5863 $ this ->module ->assertEmpty ([]);
64+ $ this ->module ->assertEmpty (0 );
5965 $ this ->module ->assertEquals (1 , 1 );
6066 $ this ->module ->assertEqualsCanonicalizing ([3 , 2 , 1 ], [1 , 2 , 3 ]);
6167 $ this ->module ->assertEqualsIgnoringCase ('foo ' , 'FOO ' );
6268 $ this ->module ->assertEqualsWithDelta (1.0 , 1.01 , 0.1 );
6369 $ this ->module ->assertFalse (false );
6470 $ this ->module ->assertFileDoesNotExist (__FILE__ . '.notExist ' );
65- // assertFileEquals
66- // assertFileEqualsCanonicalizing
67- // assertFileEqualsIgnoringCase
71+ $ this -> module -> assertFileEquals ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data1.txt ' );
72+ $ this -> module -> assertFileEqualsCanonicalizing ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data1.txt ' );
73+ $ this -> module -> assertFileEqualsIgnoringCase ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data2.txt ' );
6874 $ this ->module ->assertFileExists (__FILE__ );
6975 // assertFileIsNotReadable
7076 // assertFileIsNotWritable
71- // assertFileIsReadable
72- // assertFileIsWritable
73- // assertFileNotEquals
74- // assertFileNotEqualsCanonicalizing
75- // assertFileNotEqualsIgnoringCase
76- // assertFinite
77- // assertGreaterThan
78- // assertGreaterThanOrEqual
79- // assertInfinite
77+ $ this -> module -> assertFileIsReadable ( __FILE__ );
78+ $ this -> module -> assertFileIsWritable ( __FILE__ );
79+ $ this -> module -> assertFileNotEquals ( codecept_data_dir (). ' /data1.json ' , codecept_data_dir (). ' /data1.txt ' );
80+ $ this -> module -> assertFileNotEqualsCanonicalizing ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data3.txt ' );
81+ $ this -> module -> assertFileNotEqualsIgnoringCase ( codecept_data_dir (). ' /data1.txt ' , codecept_data_dir (). ' /data3.txt ' );
82+ $ this -> module -> assertFinite ( 2 );
83+ $ this -> module -> assertGreaterThan ( 5 , 6 );
84+ $ this -> module -> assertGreaterThanOrEqual ( 5 , 5 );
85+ $ this -> module -> assertInfinite ( 1e308 * 2 );
8086 $ this ->module ->assertInstanceOf ('Exception ' , new Exception ());
8187 $ this ->module ->assertIsArray ([1 , 2 , 3 ]);
8288 $ this ->module ->assertIsBool (true );
8389 $ this ->module ->assertIsCallable (function () {});
84- // assertIsClosedResource
90+ $ closedResource = fopen (__FILE__ , 'r ' );
91+ fclose ($ closedResource );
92+ $ this ->module ->assertIsClosedResource ($ closedResource );
8593 $ this ->module ->assertIsFloat (1.2 );
8694 $ this ->module ->assertIsInt (2 );
87- // assertIsIterable
95+ $ this -> module -> assertIsIterable ([]);
8896 $ this ->module ->assertIsNotArray (false );
8997 $ this ->module ->assertIsNotBool ([1 , 2 , 3 ]);
9098 $ this ->module ->assertIsNotCallable ('test ' );
91- // assertIsNotClosedResource
99+ $ openendResource = fopen (__FILE__ , 'r ' );
100+ $ this ->module ->assertIsNotClosedResource ($ openendResource );
92101 $ this ->module ->assertIsNotFloat (false );
93102 $ this ->module ->assertIsNotInt (false );
94- // assertIsNotIterable
103+ $ this -> module -> assertIsNotIterable ( ' test ' );
95104 $ this ->module ->assertIsNotNumeric (false );
96105 $ this ->module ->assertIsNotObject (false );
97- // assertIsNotReadable
106+ $ this -> module -> assertIsNotReadable ( __FILE__ . ' .notExist ' );
98107 $ this ->module ->assertIsNotResource (false );
99108 $ this ->module ->assertIsNotScalar (function () {});
100109 $ this ->module ->assertIsNotString (false );
101- // assertIsNotWritable
110+ $ this -> module -> assertIsNotWritable ( __FILE__ . ' .notExist ' );
102111 $ this ->module ->assertIsNumeric ('12.34 ' );
103112 $ this ->module ->assertIsObject (new stdClass ());
104- // assertIsReadable
113+ $ this -> module -> assertIsReadable ( __FILE__ );
105114 $ this ->module ->assertIsResource (fopen (__FILE__ , 'r ' ));
106115 $ this ->module ->assertIsScalar ('test ' );
107116 $ this ->module ->assertIsString ('test ' );
108- // assertIsWritable
109- // assertJson
110- // assertJsonFileEqualsJsonFile
111- // assertJsonFileNotEqualsJsonFile
112- // assertJsonStringEqualsJsonFile
113- // assertJsonStringEqualsJsonString
114- // assertJsonStringNotEqualsJsonFile
115- // assertJsonStringNotEqualsJsonString
116- // assertLessThan
117- // assertLessThanOrEqual
117+ $ this -> module -> assertIsWritable ( __FILE__ );
118+ $ this -> module -> assertJson ( ' [] ' );
119+ $ this -> module -> assertJsonFileEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , codecept_data_dir (). ' /data1.json ' );
120+ $ this -> module -> assertJsonFileNotEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , codecept_data_dir (). ' /data2.json ' );
121+ $ this -> module -> assertJsonStringEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , ' ["foo", "bar"] ' );
122+ $ this -> module -> assertJsonStringEqualsJsonString ( ' ["foo", "bar"] ' , ' [ "foo" , "bar" ] ' );
123+ $ this -> module -> assertJsonStringNotEqualsJsonFile ( codecept_data_dir (). ' /data1.json ' , ' ["bar", "foo"] ' );
124+ $ this -> module -> assertJsonStringNotEqualsJsonString ( ' ["foo", "bar"] ' , ' ["bar", "foo"] ' );
125+ $ this -> module -> assertLessThan ( 4 , 3 );
126+ $ this -> module -> assertLessThanOrEqual ( 3 , 3 );
118127 $ this ->module ->assertMatchesRegularExpression ('/^[\d]$/ ' , '1 ' );
119- // assertNan
120- // assertNotContains
121- // assertNotContainsEquals
122- // assertNotContainsOnly
123- // assertNotCount
128+ $ this -> module -> assertNan ( sqrt (- 1 ));
129+ $ this -> module -> assertNotContains ( ' three ' , [ ' one ' , ' two ' ]);
130+ $ this -> module -> assertNotContainsEquals ( 3 , [ 1 , 2 ]);
131+ $ this -> module -> assertNotContainsOnly (\DummyClass::class, [ new \ DummyClass (), new Exception ()]);
132+ $ this -> module -> assertNotCount ( 1 , [ ' one ' , ' two ' ]);
124133 $ this ->module ->assertNotEmpty ([1 ]);
125- // assertNotEquals
134+ $ this -> module -> assertNotEquals ( true , false );
126135 $ this ->module ->assertNotEqualsCanonicalizing ([3 , 2 , 1 ], [2 , 3 , 0 , 1 ]);
127136 $ this ->module ->assertNotEqualsIgnoringCase ('foo ' , 'BAR ' );
128137 $ this ->module ->assertNotEqualsWithDelta (1.0 , 1.5 , 0.1 );
129138 $ this ->module ->assertNotFalse (true );
130139 $ this ->module ->assertNotFalse (null );
131140 $ this ->module ->assertNotFalse ('foo ' );
132- // assertNotInstanceOf
141+ $ this -> module -> assertNotInstanceOf (RuntimeException::class, new Exception ());
133142 $ this ->module ->assertNotNull ('' );
134143 $ this ->module ->assertNotNull (false );
135144 $ this ->module ->assertNotNull (0 );
136145 $ this ->module ->assertNotSame (1 , '1 ' );
137- // assertNotSameSize
146+ $ this -> module -> assertNotSameSize ([ 1 , 2 , 3 ], [ 1 , 1 , 2 , 3 ]);
138147 $ this ->module ->assertNotTrue (false );
139148 $ this ->module ->assertNotTrue (null );
140149 $ this ->module ->assertNotTrue ('foo ' );
141150 $ this ->module ->assertNull (null );
142- // assertObjectHasAttribute
143- // assertObjectNotHasAttribute
151+ $ this -> module -> assertObjectHasAttribute ( ' foo ' , new \ DummyClass ());
152+ $ this -> module -> assertObjectNotHasAttribute ( ' bar ' , new \ DummyClass ());
144153 $ this ->module ->assertSame (1 , 1 );
145- // assertSameSize
154+ $ this -> module -> assertSameSize ([ 1 , 2 , 3 ], [ 1 , 2 , 3 ]);
146155 $ this ->module ->assertStringContainsString ('bar ' , 'foobar ' );
147156 $ this ->module ->assertStringContainsStringIgnoringCase ('bar ' , 'FooBar ' );
148157 $ this ->module ->assertStringEndsNotWith ('fo ' , 'foo ' );
149158 $ this ->module ->assertStringEndsWith ('oo ' , 'foo ' );
150- // assertStringEqualsFile
151- // assertStringEqualsFileCanonicalizing
152- // assertStringEqualsFileIgnoringCase
153- // assertStringMatchesFormat
154- // assertStringMatchesFormatFile
159+ $ this -> module -> assertStringEqualsFile ( codecept_data_dir (). ' /data1.txt ' , ' foo bar foo ' );
160+ $ this -> module -> assertStringEqualsFileCanonicalizing ( codecept_data_dir (). ' /data1.txt ' , ' foo bar foo ' );
161+ $ this -> module -> assertStringEqualsFileIgnoringCase ( codecept_data_dir (). ' /data1.txt ' , ' foo bAr foo ' );
162+ $ this -> module -> assertStringMatchesFormat ( ' *%s* ' , ' *** ' );
163+ $ this -> module -> assertStringMatchesFormatFile ( codecept_data_dir (). ' /expectedFileFormat.txt ' , " FOO \n" );
155164 $ this ->module ->assertStringNotContainsString ('baz ' , 'foobar ' );
156165 $ this ->module ->assertStringNotContainsStringIgnoringCase ('baz ' , 'FooBar ' );
157- // assertStringNotEqualsFile
158- // assertStringNotEqualsFileCanonicalizing
159- // assertStringNotEqualsFileIgnoringCase
160- // assertStringNotMatchesFormat
161- // assertStringNotMatchesFormatFile
166+ $ this -> module -> assertStringNotEqualsFile ( codecept_data_dir (). ' /data2.txt ' , ' foo bar foo ' );
167+ $ this -> module -> assertStringNotEqualsFileCanonicalizing ( codecept_data_dir (). ' /data3.txt ' , ' foo bar foo ' );
168+ $ this -> module -> assertStringNotEqualsFileIgnoringCase ( codecept_data_dir (). ' /data3.txt ' , ' foo bar foo ' );
169+ $ this -> module -> assertStringNotMatchesFormat ( ' *%s* ' , ' ** ' );
170+ $ this -> module -> assertStringNotMatchesFormatFile ( codecept_data_dir (). ' /expectedFileFormat.txt ' , " FO " );
162171 $ this ->module ->assertStringStartsNotWith ('ba ' , 'foo ' );
163172 $ this ->module ->assertStringStartsWith ('fo ' , 'foo ' );
164- // assertThat
173+ $ this -> module -> assertThat ( 4 , new IsEqual ( 4 ));
165174 $ this ->module ->assertTrue (true );
166- // assertXmlFileEqualsXmlFile
167- // assertXmlFileNotEqualsXmlFile
168- // assertXmlStringEqualsXmlFile
169- // assertXmlStringEqualsXmlString
170- // assertXmlStringNotEqualsXmlFile
171- // assertXmlStringNotEqualsXmlString
175+ $ this -> module -> assertXmlFileEqualsXmlFile ( codecept_data_dir (). ' /data1.xml ' , codecept_data_dir (). ' /data1.xml ' );
176+ $ this -> module -> assertXmlFileNotEqualsXmlFile ( codecept_data_dir (). ' /data1.xml ' , codecept_data_dir (). ' /data2.xml ' );
177+ $ this -> module -> assertXmlStringEqualsXmlFile ( codecept_data_dir (). ' /data1.xml ' , ' <foo>foo</foo> ' );
178+ $ this -> module -> assertXmlStringEqualsXmlString ( ' <foo>foo</foo> ' , ' <foo>foo</foo> ' );
179+ $ this -> module -> assertXmlStringNotEqualsXmlFile ( codecept_data_dir (). ' /data1.xml ' , ' <foo>bar</foo> ' );
180+ $ this -> module -> assertXmlStringNotEqualsXmlString ( ' <foo>foo</foo> ' , ' <foo>bar</foo> ' );
172181 // fail
173182 // markTestIncomplete
174183 // markTestSkipped
0 commit comments