10
10
use Codeception \Stub ;
11
11
use Exception ;
12
12
use PHPUnit \Framework \AssertionFailedError ;
13
+ use PHPUnit \Framework \Constraint \IsEqual ;
13
14
use RuntimeException ;
14
15
use stdClass ;
15
16
@@ -19,6 +20,8 @@ final class AssertsTest extends TestCase
19
20
20
21
public function _setUp ()
21
22
{
23
+ require_once codecept_data_dir ().'/DummyClass.php ' ;
24
+
22
25
/** @var ModuleContainer $container */
23
26
$ container = Stub::make (ModuleContainer::class);
24
27
$ this ->module = new Asserts ($ container );
@@ -27,148 +30,154 @@ public function _setUp()
27
30
public function testCodeceptionAsserts ()
28
31
{
29
32
$ 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 );
33
38
$ this ->module ->assertNotRegExp ('/^[a-z]$/ ' , '1 ' );
34
39
$ this ->module ->assertRegExp ('/^[\d]$/ ' , '1 ' );
35
- // assertThatItsNot
40
+ $ this -> module -> assertThatItsNot ( 3 , new IsEqual ( 4 ));
36
41
}
37
42
38
43
public function testPHPUnitAsserts ()
39
44
{
40
45
$ 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);
46
51
$ 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 ()]);
50
55
$ this ->module ->assertCount (3 , [1 , 2 , 3 ]);
51
- // assertDirectoryDoesNotExist
52
- // assertDirectoryExists
56
+ $ this -> module -> assertDirectoryDoesNotExist ( __DIR__ . ' notExist ' );
57
+ $ this -> module -> assertDirectoryExists ( __DIR__ );
53
58
// assertDirectoryIsNotReadable
54
59
// assertDirectoryIsNotWritable
55
- // assertDirectoryIsReadable
56
- // assertDirectoryIsWritable
60
+ $ this -> module -> assertDirectoryIsReadable ( __DIR__ );
61
+ $ this -> module -> assertDirectoryIsWritable ( __DIR__ );
57
62
$ this ->module ->assertDoesNotMatchRegularExpression ('/^[a-z]$/ ' , '1 ' );
58
63
$ this ->module ->assertEmpty ([]);
64
+ $ this ->module ->assertEmpty (0 );
59
65
$ this ->module ->assertEquals (1 , 1 );
60
66
$ this ->module ->assertEqualsCanonicalizing ([3 , 2 , 1 ], [1 , 2 , 3 ]);
61
67
$ this ->module ->assertEqualsIgnoringCase ('foo ' , 'FOO ' );
62
68
$ this ->module ->assertEqualsWithDelta (1.0 , 1.01 , 0.1 );
63
69
$ this ->module ->assertFalse (false );
64
70
$ 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 ' );
68
74
$ this ->module ->assertFileExists (__FILE__ );
69
75
// assertFileIsNotReadable
70
76
// 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 );
80
86
$ this ->module ->assertInstanceOf ('Exception ' , new Exception ());
81
87
$ this ->module ->assertIsArray ([1 , 2 , 3 ]);
82
88
$ this ->module ->assertIsBool (true );
83
89
$ this ->module ->assertIsCallable (function () {});
84
- // assertIsClosedResource
90
+ $ closedResource = fopen (__FILE__ , 'r ' );
91
+ fclose ($ closedResource );
92
+ $ this ->module ->assertIsClosedResource ($ closedResource );
85
93
$ this ->module ->assertIsFloat (1.2 );
86
94
$ this ->module ->assertIsInt (2 );
87
- // assertIsIterable
95
+ $ this -> module -> assertIsIterable ([]);
88
96
$ this ->module ->assertIsNotArray (false );
89
97
$ this ->module ->assertIsNotBool ([1 , 2 , 3 ]);
90
98
$ this ->module ->assertIsNotCallable ('test ' );
91
- // assertIsNotClosedResource
99
+ $ openendResource = fopen (__FILE__ , 'r ' );
100
+ $ this ->module ->assertIsNotClosedResource ($ openendResource );
92
101
$ this ->module ->assertIsNotFloat (false );
93
102
$ this ->module ->assertIsNotInt (false );
94
- // assertIsNotIterable
103
+ $ this -> module -> assertIsNotIterable ( ' test ' );
95
104
$ this ->module ->assertIsNotNumeric (false );
96
105
$ this ->module ->assertIsNotObject (false );
97
- // assertIsNotReadable
106
+ $ this -> module -> assertIsNotReadable ( __FILE__ . ' .notExist ' );
98
107
$ this ->module ->assertIsNotResource (false );
99
108
$ this ->module ->assertIsNotScalar (function () {});
100
109
$ this ->module ->assertIsNotString (false );
101
- // assertIsNotWritable
110
+ $ this -> module -> assertIsNotWritable ( __FILE__ . ' .notExist ' );
102
111
$ this ->module ->assertIsNumeric ('12.34 ' );
103
112
$ this ->module ->assertIsObject (new stdClass ());
104
- // assertIsReadable
113
+ $ this -> module -> assertIsReadable ( __FILE__ );
105
114
$ this ->module ->assertIsResource (fopen (__FILE__ , 'r ' ));
106
115
$ this ->module ->assertIsScalar ('test ' );
107
116
$ 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 );
118
127
$ 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 ' ]);
124
133
$ this ->module ->assertNotEmpty ([1 ]);
125
- // assertNotEquals
134
+ $ this -> module -> assertNotEquals ( true , false );
126
135
$ this ->module ->assertNotEqualsCanonicalizing ([3 , 2 , 1 ], [2 , 3 , 0 , 1 ]);
127
136
$ this ->module ->assertNotEqualsIgnoringCase ('foo ' , 'BAR ' );
128
137
$ this ->module ->assertNotEqualsWithDelta (1.0 , 1.5 , 0.1 );
129
138
$ this ->module ->assertNotFalse (true );
130
139
$ this ->module ->assertNotFalse (null );
131
140
$ this ->module ->assertNotFalse ('foo ' );
132
- // assertNotInstanceOf
141
+ $ this -> module -> assertNotInstanceOf (RuntimeException::class, new Exception ());
133
142
$ this ->module ->assertNotNull ('' );
134
143
$ this ->module ->assertNotNull (false );
135
144
$ this ->module ->assertNotNull (0 );
136
145
$ this ->module ->assertNotSame (1 , '1 ' );
137
- // assertNotSameSize
146
+ $ this -> module -> assertNotSameSize ([ 1 , 2 , 3 ], [ 1 , 1 , 2 , 3 ]);
138
147
$ this ->module ->assertNotTrue (false );
139
148
$ this ->module ->assertNotTrue (null );
140
149
$ this ->module ->assertNotTrue ('foo ' );
141
150
$ this ->module ->assertNull (null );
142
- // assertObjectHasAttribute
143
- // assertObjectNotHasAttribute
151
+ $ this -> module -> assertObjectHasAttribute ( ' foo ' , new \ DummyClass ());
152
+ $ this -> module -> assertObjectNotHasAttribute ( ' bar ' , new \ DummyClass ());
144
153
$ this ->module ->assertSame (1 , 1 );
145
- // assertSameSize
154
+ $ this -> module -> assertSameSize ([ 1 , 2 , 3 ], [ 1 , 2 , 3 ]);
146
155
$ this ->module ->assertStringContainsString ('bar ' , 'foobar ' );
147
156
$ this ->module ->assertStringContainsStringIgnoringCase ('bar ' , 'FooBar ' );
148
157
$ this ->module ->assertStringEndsNotWith ('fo ' , 'foo ' );
149
158
$ 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" );
155
164
$ this ->module ->assertStringNotContainsString ('baz ' , 'foobar ' );
156
165
$ 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 " );
162
171
$ this ->module ->assertStringStartsNotWith ('ba ' , 'foo ' );
163
172
$ this ->module ->assertStringStartsWith ('fo ' , 'foo ' );
164
- // assertThat
173
+ $ this -> module -> assertThat ( 4 , new IsEqual ( 4 ));
165
174
$ 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> ' );
172
181
// fail
173
182
// markTestIncomplete
174
183
// markTestSkipped
0 commit comments