@@ -18,77 +18,175 @@ Write tests as usual, just use fluent assertions short aliases ``` check($x)->..
1818
1919``` php
2020// arrange
21- $user = UserFactory::createOne([
22- 'phone' => $phoneBefore = faker()->e164PhoneNumber;
23- ]);
21+ $user = UserFactory::createOne();
2422
2523// act
26- $user->setPhone(
27- $phoneAfter = faker()->e164PhoneNumber
28- );
29-
30- // assert
24+ $user->setPhone($e164PhoneNumber = faker()->e164PhoneNumber);
3125
3226// traditional PHPUnit assertions
33- self::assertSame(expected: $phoneAfter, actual: $user->getPhone());
34- self::assertNotSame(expected: $phoneBefore, actual: $user->getPhone());
27+ self::assertSame($e164PhoneNumber, $user->getPhone());
28+
29+ // fluent assertions
30+ fact($user->getPhone())
31+ ->is($e164PhoneNumber)
32+ ->isString()
33+ ->startsWith('+7')
34+ // etc.
35+ ;
36+ ```
3537
36- ### Comparison and Equality Methods
38+ ### Array assertions
3739``` php
38- fact(42)->is(42)->equals(42)->not(43);
40+ fact([1, 2, 3])->count(3); // Passes
41+ fact([1, 2])->count(3); // Fails
42+
43+ fact([1, 2])->notCount(3); // Passes
44+ fact([1, 2, 3])->notCount(3); // Fails
45+
46+ fact(['a' => ['b' => 'c']])->arrayContainsAssociativeArray(['a' => ['b' => 'c']]); // Passes
47+ fact(['a' => ['b' => 'd']])->arrayContainsAssociativeArray(['a' => ['b' => 'c']]); // Fails
48+
49+ fact(['a' => 1])->arrayHasKey('a'); // Passes
50+ fact(['a' => 1])->arrayHasKey('b'); // Fails
51+
52+ fact(['a' => 1])->arrayNotHasKey('b'); // Passes
53+ fact(['a' => 1])->arrayNotHasKey('a'); // Fails
54+
55+ fact([1, 2, 3])->contains(2); // Passes
56+ fact([1, 2])->contains(3); // Fails
57+
58+ fact([1, 2])->doesNotContain(3); // Passes
59+ fact([1, 2, 3])->doesNotContain(3); // Fails
60+
61+ fact([1, 2])->hasSize(2); // Passes
62+ fact([1, 2, 3])->hasSize(2); // Fails
63+
64+ fact([])->isEmptyArray(); // Passes
65+ fact([1, 2])->isEmptyArray(); // Fails
66+
67+ fact([1, 2])->isNotEmptyArray(); // Passes
68+ fact([])->isNotEmptyArray(); // Fails
69+
3970```
4071
41- ### Boolean Methods
72+ ### Boolean assertions
4273``` php
43- fact(true)->true()->notFalse();
74+ fact(true)->true(); // Passes
75+ fact(1)->true(); // Fails due to strict comparison
76+
77+ fact(false)->notTrue(); // Passes
78+ fact(true)->notTrue(); // Fails
79+
80+ fact(false)->false(); // Passes
81+ fact(0)->false(); // Fails due to strict comparison
82+
83+ fact(true)->notFalse(); // Passes
84+ fact(false)->notFalse(); // Fails
4485```
4586
46- ### Null Methods
87+
88+ ### Comparison and Equality assertions
4789``` php
48- fact(null)->null()->notNull();
90+ fact(42)->is(42); // Passes
91+ fact(42)->is('42'); // Fails due to type difference
92+
93+ fact(42)->equals(42); // Passes
94+ fact(42)->equals('42'); // Passes due to loose comparison
95+
96+ fact(42)->not(43); // Passes
97+ fact(42)->not(42); // Fails
4998```
5099
51- ### Numeric Methods
100+
101+ ### Null assertions
52102``` php
53- fact(5)->isLowerThan(10)->isGreaterThan(0)->isPositive()->isNegative()->isZero()->isBetween(1, 10);
103+ fact(null)->null(); // Passes
104+ fact('')->null(); // Fails
105+
106+ fact(42)->notNull(); // Passes
107+ fact(null)->notNull(); // Fails
54108```
55109
56- ### String Methods
110+ ### Numeric assertions
57111``` php
58- fact('hello world')->matchesRegularExpression('/\w+/')->startsWith('hello')->endsWith('world')->hasLength(11)->isEmptyString();
112+ fact(5)->isLowerThan(10); // Passes
113+ fact(10)->isLowerThan(5); // Fails
114+
115+ fact(10)->isGreaterThan(5); // Passes
116+ fact(5)->isGreaterThan(10); // Fails
117+
118+ fact(5)->isPositive(); // Passes
119+ fact(-3)->isPositive(); // Fails
120+
121+ fact(-3)->isNegative(); // Passes
122+ fact(5)->isNegative(); // Fails
123+
124+ fact(0)->isZero(); // Passes
125+ fact(0.0)->isZero(); // Passes
126+ fact(1)->isZero(); // Fails
127+
128+ fact(5)->isBetween(1, 10); // Passes
129+ fact(15)->isBetween(1, 10); // Fails
59130```
60131
61- ### Array Methods
132+ ### Special assertions
62133``` php
63- fact([1, 2, 3])->count(3)->contains(2)->doesNotContain(4)->hasSize(3)->isEmptyArray()->isNotEmptyArray();
134+ fact('01ARZ3NDEKTSV4RRFFQ69G5FAV')->ulid(); // Passes (if valid ULID)
135+ fact('invalid-ulid')->ulid(); // Fails
64136```
65137
66- ### Type Checking Methods
138+ ### String assertions
67139``` php
68- fact(42)->isInt()->isString()->instanceOf(int::class)->hasProperty('name')->hasMethod('doSomething');
140+ fact('abc123')->matchesRegularExpression('/^[a-z]+\d+$/'); // Passes
141+ fact('123abc')->matchesRegularExpression('/^[a-z]+\d+$/'); // Fails
142+
143+ fact('123abc')->notMatchesRegularExpression('/^[a-z]+\d+$/'); // Passes
144+ fact('abc123')->notMatchesRegularExpression('/^[a-z]+\d+$/'); // Fails
145+
146+ fact('hello world')->containsString('world'); // Passes
147+ fact('hello world')->containsString('foo'); // Fails
148+
149+ fact('hello world')->notContainsString('foo'); // Passes
150+ fact('hello world')->notContainsString('world'); // Fails
151+
152+ fact('Hello World')->containsStringIgnoringCase('world'); // Passes
153+ fact('Hello World')->containsStringIgnoringCase('foo'); // Fails
154+
155+ fact('Hello World')->notContainsStringIgnoringCase('foo'); // Passes
156+ fact('Hello World')->notContainsStringIgnoringCase('world'); // Fails
157+
158+ fact('hello world')->startsWith('hello'); // Passes
159+ fact('world hello')->startsWith('hello'); // Fails
160+
161+ fact('file.txt')->endsWith('.txt'); // Passes
162+ fact('txt.file')->endsWith('.txt'); // Fails
163+
164+ fact('abc')->hasLength(3); // Passes
165+ fact('abcd')->hasLength(3); // Fails
166+
167+ fact('')->isEmptyString(); // Passes
168+ fact('hello')->isEmptyString(); // Fails
69169```
70170
71- ### Special Methods
171+ ### Type Checking assertions
72172``` php
73- fact('01ARZ3NDEKTSV4RRFFQ69G5FAV')->ulid();
74- ```
75- ...
76- ;
173+ fact(new stdClass())->instanceOf(stdClass::class); // Passes
174+ fact(new stdClass())->instanceOf(Exception::class); // Fails
175+
176+ fact(new stdClass())->notInstanceOf(Exception::class); // Passes
177+ fact(new stdClass())->notInstanceOf(stdClass::class); // Fails
178+
179+ fact(42)->isInt(); // Passes
180+ fact('42')->isInt(); // Fails
181+
182+ fact('text')->isString(); // Passes
183+ fact(42)->isString(); // Fails
77184
78- fact(
79- [
80- 'a' => [ 'any' => 'thing'] ,
81- 'b' => [ 'any' => 'thing', 'type' => 'candy', 'color' => 'green'] ,
82- 'c' => [ 'miss' => 'kiss', 'foo' => 'bar', 'any' => 'thing'] ,
83- 'd' => [ 'any' => 'thing'] ,
84- ]
85- )->arrayContainsAssociativeArray(
86- [
87- 'c' => [ 'foo' => 'bar', 'miss' => 'kiss'] ,
88- 'b' => [ 'color' => 'green'] ,
89- ]
90- ); // true
185+ fact((object)['name' => 'John'])->hasProperty('name'); // Passes
186+ fact((object)['name' => 'John'])->hasProperty('age'); // Fails
91187
188+ fact(new stdClass())->hasMethod('__construct'); // Passes
189+ fact(new stdClass())->hasMethod('nonExistentMethod'); // Fails
92190```
93191
94192## Pull requests are always welcome
0 commit comments