Skip to content

Commit 7e9dd89

Browse files
committed
Add isFloat, isBool, isArray type checking methods and isJson string method with tests and README updates
1 parent 871d6bd commit 7e9dd89

File tree

7 files changed

+305
-0
lines changed

7 files changed

+305
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ fact('hello')->isEmptyString(); // Fails
169169

170170
fact('hello')->isNotEmptyString(); // Passes
171171
fact('')->isNotEmptyString(); // Fails
172+
173+
fact('{"key": "value"}')->isJson(); // Passes
174+
fact('invalid json')->isJson(); // Fails
172175
```
173176

174177
### Type Checking assertions
@@ -190,6 +193,15 @@ fact((object)['name' => 'John'])->hasProperty('age'); // Fails
190193

191194
fact(new stdClass())->hasMethod('__construct'); // Passes
192195
fact(new stdClass())->hasMethod('nonExistentMethod'); // Fails
196+
197+
fact(3.14)->isFloat(); // Passes
198+
fact(42)->isFloat(); // Fails
199+
200+
fact(true)->isBool(); // Passes
201+
fact(1)->isBool(); // Fails
202+
203+
fact([1, 2])->isArray(); // Passes
204+
fact('not array')->isArray(); // Fails
193205
```
194206

195207
## Pull requests are always welcome

src/Traits/StringAssertions.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,5 +293,25 @@ public function isNotEmptyString(string $message = ''): self
293293
return $this;
294294
}
295295

296+
/**
297+
* Asserts that a string is valid JSON.
298+
*
299+
* This method checks if the actual string is valid JSON.
300+
*
301+
* Example usage:
302+
* fact('{"key": "value"}')->isJson(); // Passes
303+
* fact('invalid json')->isJson(); // Fails
304+
*
305+
* @param string $message Optional custom error message.
306+
*
307+
* @return self Enables fluent chaining of assertion methods.
308+
*/
309+
public function isJson(string $message = ''): self
310+
{
311+
Assert::assertTrue(json_validate($this->variable), $message ?: 'String is not valid JSON.');
312+
313+
return $this;
314+
}
315+
296316
// endregion Length Methods
297317
}

src/Traits/TypeCheckingAssertions.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,64 @@ public function hasMethod(string $method, string $message = ''): self
135135

136136
return $this;
137137
}
138+
139+
/**
140+
* Asserts that a variable is of type float.
141+
*
142+
* This method checks if the actual value is a floating-point number.
143+
*
144+
* Example usage:
145+
* fact(3.14)->isFloat(); // Passes
146+
* fact(42)->isFloat(); // Fails
147+
*
148+
* @param string $message Optional custom error message.
149+
*
150+
* @return self Enables fluent chaining of assertion methods.
151+
*/
152+
public function isFloat(string $message = ''): self
153+
{
154+
Assert::assertIsFloat($this->variable, $message);
155+
156+
return $this;
157+
}
158+
159+
/**
160+
* Asserts that a variable is of type bool.
161+
*
162+
* This method checks if the actual value is a boolean.
163+
*
164+
* Example usage:
165+
* fact(true)->isBool(); // Passes
166+
* fact(1)->isBool(); // Fails
167+
*
168+
* @param string $message Optional custom error message.
169+
*
170+
* @return self Enables fluent chaining of assertion methods.
171+
*/
172+
public function isBool(string $message = ''): self
173+
{
174+
Assert::assertIsBool($this->variable, $message);
175+
176+
return $this;
177+
}
178+
179+
/**
180+
* Asserts that a variable is of type array.
181+
*
182+
* This method checks if the actual value is an array.
183+
*
184+
* Example usage:
185+
* fact([1, 2])->isArray(); // Passes
186+
* fact('not array')->isArray(); // Fails
187+
*
188+
* @param string $message Optional custom error message.
189+
*
190+
* @return self Enables fluent chaining of assertion methods.
191+
*/
192+
public function isArray(string $message = ''): self
193+
{
194+
Assert::assertIsArray($this->variable, $message);
195+
196+
return $this;
197+
}
138198
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\Asserts\IsArray;
4+
5+
use K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\FluentAssertionsTestCase;
6+
use K2gl\PHPUnitFluentAssertions\FluentAssertions;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
10+
use function K2gl\PHPUnitFluentAssertions\fact;
11+
12+
#[CoversMethod(className: FluentAssertions::class, methodName: 'isArray')]
13+
final class IsArrayTest extends FluentAssertionsTestCase
14+
{
15+
#[DataProvider('isArrayDataProvider')]
16+
public function testIsArray(mixed $variable): void
17+
{
18+
// act
19+
fact($variable)->isArray();
20+
21+
// assert
22+
$this->correctAssertionExecuted();
23+
}
24+
25+
#[DataProvider('notIsArrayDataProvider')]
26+
public function testNotIsArray(mixed $variable): void
27+
{
28+
// assert
29+
$this->incorrectAssertionExpected();
30+
31+
// act
32+
fact($variable)->isArray();
33+
}
34+
35+
public static function isArrayDataProvider(): array
36+
{
37+
return [
38+
[[]],
39+
[[1, 2, 3]],
40+
[['key' => 'value']],
41+
];
42+
}
43+
44+
public static function notIsArrayDataProvider(): array
45+
{
46+
return [
47+
['string'],
48+
[42],
49+
[true],
50+
[null],
51+
];
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\Asserts\IsBool;
4+
5+
use K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\FluentAssertionsTestCase;
6+
use K2gl\PHPUnitFluentAssertions\FluentAssertions;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
10+
use function K2gl\PHPUnitFluentAssertions\fact;
11+
12+
#[CoversMethod(className: FluentAssertions::class, methodName: 'isBool')]
13+
final class IsBoolTest extends FluentAssertionsTestCase
14+
{
15+
#[DataProvider('isBoolDataProvider')]
16+
public function testIsBool(mixed $variable): void
17+
{
18+
// act
19+
fact($variable)->isBool();
20+
21+
// assert
22+
$this->correctAssertionExecuted();
23+
}
24+
25+
#[DataProvider('notIsBoolDataProvider')]
26+
public function testNotIsBool(mixed $variable): void
27+
{
28+
// assert
29+
$this->incorrectAssertionExpected();
30+
31+
// act
32+
fact($variable)->isBool();
33+
}
34+
35+
public static function isBoolDataProvider(): array
36+
{
37+
return [
38+
[true],
39+
[false],
40+
];
41+
}
42+
43+
public static function notIsBoolDataProvider(): array
44+
{
45+
return [
46+
[1],
47+
[0],
48+
['true'],
49+
[null],
50+
];
51+
}
52+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\Asserts\IsFloat;
4+
5+
use K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\FluentAssertionsTestCase;
6+
use K2gl\PHPUnitFluentAssertions\FluentAssertions;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
10+
use function K2gl\PHPUnitFluentAssertions\fact;
11+
12+
#[CoversMethod(className: FluentAssertions::class, methodName: 'isFloat')]
13+
final class IsFloatTest extends FluentAssertionsTestCase
14+
{
15+
#[DataProvider('isFloatDataProvider')]
16+
public function testIsFloat(mixed $variable): void
17+
{
18+
// act
19+
fact($variable)->isFloat();
20+
21+
// assert
22+
$this->correctAssertionExecuted();
23+
}
24+
25+
#[DataProvider('notIsFloatDataProvider')]
26+
public function testNotIsFloat(mixed $variable): void
27+
{
28+
// assert
29+
$this->incorrectAssertionExpected();
30+
31+
// act
32+
fact($variable)->isFloat();
33+
}
34+
35+
public static function isFloatDataProvider(): array
36+
{
37+
return [
38+
[3.14],
39+
[0.0],
40+
[-1.5],
41+
];
42+
}
43+
44+
public static function notIsFloatDataProvider(): array
45+
{
46+
return [
47+
[42],
48+
['3.14'],
49+
[true],
50+
[null],
51+
];
52+
}
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\Asserts\IsJson;
4+
5+
use K2gl\PHPUnitFluentAssertions\Tests\FluentAssertions\FluentAssertionsTestCase;
6+
use K2gl\PHPUnitFluentAssertions\FluentAssertions;
7+
use PHPUnit\Framework\Attributes\CoversMethod;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
10+
use function K2gl\PHPUnitFluentAssertions\fact;
11+
12+
#[CoversMethod(className: FluentAssertions::class, methodName: 'isJson')]
13+
final class IsJsonTest extends FluentAssertionsTestCase
14+
{
15+
#[DataProvider('isJsonDataProvider')]
16+
public function testIsJson(mixed $variable): void
17+
{
18+
// act
19+
fact($variable)->isJson();
20+
21+
// assert
22+
$this->correctAssertionExecuted();
23+
}
24+
25+
#[DataProvider('notIsJsonDataProvider')]
26+
public function testNotIsJson(mixed $variable): void
27+
{
28+
// assert
29+
$this->incorrectAssertionExpected();
30+
31+
// act
32+
fact($variable)->isJson();
33+
}
34+
35+
public static function isJsonDataProvider(): array
36+
{
37+
return [
38+
['{"key": "value"}'],
39+
['[1, 2, 3]'],
40+
['"string"'],
41+
['42'],
42+
['true'],
43+
];
44+
}
45+
46+
public static function notIsJsonDataProvider(): array
47+
{
48+
return [
49+
['invalid json'],
50+
['{key: value}'],
51+
[''],
52+
['not json'],
53+
];
54+
}
55+
}

0 commit comments

Comments
 (0)