Skip to content

Commit 4c214e5

Browse files
authored
Merge pull request #58: Normalize assertions and expectations
2 parents c33d236 + d213627 commit 4c214e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+895
-545
lines changed

src/Assert.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
use Testo\Assert\Api\Builtin\ObjectType;
1313
use Testo\Assert\Api\Builtin\StringType;
1414
use Testo\Assert\Api\Json\JsonAbstract;
15+
use Testo\Assert\Internal\Assertion\AssertArray;
1516
use Testo\Assert\Internal\Assertion\AssertFloat;
1617
use Testo\Assert\Internal\Assertion\AssertInt;
18+
use Testo\Assert\Internal\Assertion\AssertIterable;
1719
use Testo\Assert\Internal\Assertion\AssertJson;
1820
use Testo\Assert\Internal\Assertion\AssertObject;
1921
use Testo\Assert\Internal\Assertion\AssertString;
20-
use Testo\Assert\Internal\Assertion\AssertArray;
2122
use Testo\Assert\State\AssertException;
22-
use Testo\Assert\State\AssertTypeFailure;
23+
use Testo\Assert\State\Assertion\AssertionException;
24+
use Testo\Assert\State\Test\Fail;
2325
use Testo\Assert\StaticState;
2426
use Testo\Assert\Support;
25-
use Testo\Assert\Internal\Assertion\AssertIterable;
2627

2728
/**
2829
* Assertion utilities.
@@ -40,7 +41,7 @@ final class Assert
4041
public static function same(mixed $expected, mixed $actual, string $message = ''): void
4142
{
4243
$actual === $expected
43-
? StaticState::log('Assert same: `' . Support::stringify($expected) . '`', $message)
44+
? StaticState::success($actual, 'is the same', $message)
4445
: StaticState::fail(AssertException::compare($expected, $actual, $message));
4546
}
4647

@@ -55,7 +56,7 @@ public static function same(mixed $expected, mixed $actual, string $message = ''
5556
public static function notSame(mixed $expected, mixed $actual, string $message = ''): void
5657
{
5758
$actual !== $expected
58-
? StaticState::log('Assert not same: `' . Support::stringify($expected) . '`', $message)
59+
? StaticState::success($actual, 'is not same as `' . Support::stringify($expected) . '`', $message)
5960
: StaticState::fail(AssertException::compare(
6061
$expected,
6162
$actual,
@@ -76,7 +77,7 @@ public static function notSame(mixed $expected, mixed $actual, string $message =
7677
public static function equals(mixed $expected, mixed $actual, string $message = ''): void
7778
{
7879
$actual == $expected
79-
? StaticState::log('Assert equals: `' . Support::stringify($expected) . '`', $message)
80+
? StaticState::success($actual, 'equals to `' . Support::stringify($expected) . '`', $message)
8081
: StaticState::fail(AssertException::compare(
8182
$expected,
8283
$actual,
@@ -96,7 +97,7 @@ public static function equals(mixed $expected, mixed $actual, string $message =
9697
public static function notEquals(mixed $expected, mixed $actual, string $message = ''): void
9798
{
9899
$actual != $expected
99-
? StaticState::log('Assert not equals: `' . Support::stringify($expected) . '`', $message)
100+
? StaticState::success($actual, 'is not equals to `' . Support::stringify($expected) . '`', $message)
100101
: StaticState::fail(AssertException::compare(
101102
$expected,
102103
$actual,
@@ -107,38 +108,38 @@ public static function notEquals(mixed $expected, mixed $actual, string $message
107108
}
108109

109110
/**
110-
* Asserts that the condition is true.
111+
* Asserts that the value is true.
111112
*
112-
* @param bool $condition The condition asserting to be true.
113+
* @param mixed $actual The actual value to check.
113114
* @param string $message Short description about what exactly is being asserted.
114115
* @throws AssertException when the assertion fails.
115116
*/
116-
public static function true(bool $condition, string $message = ''): void
117+
public static function true(mixed $actual, string $message = ''): void
117118
{
118-
$condition === true
119-
? StaticState::log('Assert true', $message)
119+
$actual === true
120+
? StaticState::success($actual, 'is exactly `true`', $message)
120121
: StaticState::fail(AssertException::compare(
121122
true,
122-
$condition,
123+
$actual,
123124
$message,
124125
'Failed asserting that value `%2$s` is `%1$s`',
125126
));
126127
}
127128

128129
/**
129-
* Asserts that the condition is false.
130+
* Asserts that the value is false.
130131
*
131-
* @param bool $condition The condition asserting to be false.
132+
* @param mixed $actual The actual value to check.
132133
* @param string $message Short description about what exactly is being asserted.
133134
* @throws AssertException when the assertion fails.
134135
*/
135-
public static function false(bool $condition, string $message = ''): void
136+
public static function false(mixed $actual, string $message = ''): void
136137
{
137-
$condition === false
138-
? StaticState::log('Assert false', $message)
138+
$actual === false
139+
? StaticState::success($actual, 'is exactly `false`', $message)
139140
: StaticState::fail(AssertException::compare(
140141
false,
141-
$condition,
142+
$actual,
142143
$message,
143144
'Failed asserting that value `%2$s` is `%1$s`',
144145
));
@@ -174,7 +175,7 @@ public static function contains(mixed $needle, iterable $haystack, string $messa
174175
{
175176
foreach ($haystack as $element) {
176177
if ($needle === $element) {
177-
StaticState::log('Assert contains', $message);
178+
StaticState::success($haystack, 'contains `' . Support::stringify($needle) . '`', $message);
178179
return;
179180
}
180181
}
@@ -198,7 +199,7 @@ public static function null(
198199
string $message = '',
199200
): void {
200201
$actual === null
201-
? StaticState::log('Assert `null`', $message)
202+
? StaticState::success($actual, 'is exactly `null`', $message)
202203
: StaticState::fail(AssertException::compare(null, $actual, $message));
203204
}
204205

@@ -221,7 +222,7 @@ public static function blank(
221222
if (
222223
$actual === null || $actual === '' || $actual === [] || ($actual instanceof \Countable && \count($actual) === 0)
223224
) {
224-
StaticState::log('Assert blank', $message);
225+
StaticState::success($actual, 'is blank', $message);
225226
return;
226227
}
227228
StaticState::fail(AssertException::compare(
@@ -245,7 +246,7 @@ public static function string(mixed $actual): StringType
245246
/**
246247
* Asserts that the given value is of `int` data type.
247248
*
248-
* @throws AssertTypeFailure
249+
* @throws AssertionException
249250
*/
250251
public static function int(mixed $actual): IntType
251252
{
@@ -255,7 +256,7 @@ public static function int(mixed $actual): IntType
255256
/**
256257
* Asserts that the given value is of `float` data type.
257258
*
258-
* @throws AssertTypeFailure
259+
* @throws AssertionException
259260
*/
260261
public static function float(mixed $actual): FloatType
261262
{
@@ -267,7 +268,7 @@ public static function float(mixed $actual): FloatType
267268
*
268269
* Numeric type includes integer, float, and numeric strings.
269270
*
270-
* @throws AssertTypeFailure
271+
* @throws AssertionException
271272
*
272273
* @deprecated To be implemented
273274
*/
@@ -295,7 +296,7 @@ public static function json(string $actual): JsonAbstract
295296
* Iterables include arrays and objects implementing iterable interface.
296297
* Does not work with Generators.
297298
*
298-
* @throws AssertTypeFailure
299+
* @throws AssertionException
299300
*/
300301
public static function iterable(mixed $actual): IterableType
301302
{
@@ -305,7 +306,7 @@ public static function iterable(mixed $actual): IterableType
305306
/**
306307
* Asserts that the given value is of `array` data type.
307308
*
308-
* @throws AssertTypeFailure
309+
* @throws AssertionException
309310
*/
310311
public static function array(mixed $actual): ArrayType
311312
{
@@ -315,7 +316,7 @@ public static function array(mixed $actual): ArrayType
315316
/**
316317
* Asserts that the given value is of `object` data type.
317318
*
318-
* @throws AssertTypeFailure
319+
* @throws AssertionException
319320
*
320321
* @deprecated To be implemented
321322
*/
@@ -337,7 +338,7 @@ public static function object(mixed $actual): ObjectType
337338
*/
338339
public static function fail(string $message = ''): never
339340
{
340-
$exception = AssertException::fail($message);
341+
$exception = new Fail($message);
341342
StaticState::expectFail($exception);
342343
StaticState::fail($exception);
343344
}

src/Assert/Api/Builtin/ArrayType.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ interface ArrayType extends IterableType
1616
/**
1717
* Asserts that the array contains given key.
1818
*
19-
* @param int|string $key The key to check for in the array.
20-
* @param string $message Optional message for the assertion.
19+
* @param int|string ...$keys The keys to check for existence in the array.
2120
* @throws AssertException when the assertion fails.
22-
*
2321
*/
24-
public function hasKey(int|string $key, string $message = ''): self;
22+
public function hasKeys(int|string ...$keys): static;
2523

2624
/**
2725
* Asserts that the array is a list.
@@ -31,7 +29,6 @@ public function hasKey(int|string $key, string $message = ''): self;
3129
*
3230
* @param string $message Optional message for the assertion.
3331
* @throws AssertException when the assertion fails.
34-
*
3532
*/
36-
public function isList(string $message = ''): self;
33+
public function isList(string $message = ''): static;
3734
}

src/Assert/Api/Builtin/IterableType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface IterableType
2222
* @param string $message Optional message for the assertion.
2323
* @throws AssertException when the assertion fails.
2424
*/
25-
public function contains(mixed $needle, string $message = ''): self;
25+
public function contains(mixed $needle, string $message = ''): static;
2626

2727
/**
2828
* Asserts that the iterable has the same number of elements as the expected iterable.
@@ -31,15 +31,15 @@ public function contains(mixed $needle, string $message = ''): self;
3131
* @param string $message Optional message for the assertion.
3232
* @throws AssertException when the assertion fails.
3333
*/
34-
public function sameSizeAs(iterable $expected, string $message = ''): self;
34+
public function sameSizeAs(iterable $expected, string $message = ''): static;
3535

3636
/**
3737
* Asserts that the iterable has the expected number of elements.
3838
*
3939
* @param int $expected The expected count of elements.
4040
* @throws AssertException when the assertion fails.
4141
*/
42-
public function hasCount(int $expected): self;
42+
public function hasCount(int $expected): static;
4343

4444
/**
4545
* Asserts that all values in the iterable are of the specified type.
@@ -49,5 +49,5 @@ public function hasCount(int $expected): self;
4949
* @param string $message Optional message for the assertion.
5050
* @throws AssertException when the assertion fails.
5151
*/
52-
public function allOf(string $type, string $message = ''): self;
52+
public function allOf(string $type, string $message = ''): static;
5353
}

src/Assert/Api/Builtin/NumericType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface NumericType
2020
* @param string $message Optional message for the assertion.
2121
* @throws AssertException when the assertion fails.
2222
*/
23-
public function greaterThan(int|float $min, string $message = ''): self;
23+
public function greaterThan(int|float $min, string $message = ''): static;
2424

2525
/**
2626
* Asserts that the numeric value is greater than or equal to given minimum.
@@ -29,7 +29,7 @@ public function greaterThan(int|float $min, string $message = ''): self;
2929
* @param string $message Optional message for the assertion.
3030
* @throws AssertException when the assertion fails.
3131
*/
32-
public function greaterThanOrEqual(int|float $min, string $message = ''): self;
32+
public function greaterThanOrEqual(int|float $min, string $message = ''): static;
3333

3434
/**
3535
* Asserts that numeric value is less than the given maximum.
@@ -38,7 +38,7 @@ public function greaterThanOrEqual(int|float $min, string $message = ''): self;
3838
* @param string $message Optional message for the assertion.
3939
* @throws AssertException when the assertion fails.
4040
*/
41-
public function lessThan(int|float $max, string $message = ''): self;
41+
public function lessThan(int|float $max, string $message = ''): static;
4242

4343
/**
4444
* Asserts that the numeric value is less than or equal to given maximum.
@@ -47,5 +47,5 @@ public function lessThan(int|float $max, string $message = ''): self;
4747
* @param string $message Optional message for the assertion.
4848
* @throws AssertException when the assertion fails.
4949
*/
50-
public function lessThanOrEqual(int|float $max, string $message = ''): self;
50+
public function lessThanOrEqual(int|float $max, string $message = ''): static;
5151
}

src/Assert/Api/Builtin/ObjectType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface ObjectType
2323
* @psalm-assert ExpectedType $actual
2424
* @phpstan-assert ExpectedType $actual
2525
*/
26-
public function instanceOf(string $expected, string $message = ''): self;
26+
public function instanceOf(string $expected, string $message = ''): static;
2727

2828
/**
2929
* Asserts that the object has the given property.
@@ -34,5 +34,5 @@ public function instanceOf(string $expected, string $message = ''): self;
3434
*
3535
* @deprecated To be implemented
3636
*/
37-
public function hasProperty(string $propertyName, string $message = ''): self;
37+
public function hasProperty(string $propertyName, string $message = ''): static;
3838
}

src/Assert/Interceptor/ExpectationsInterceptor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Testo\Assert\Interceptor;
66

77
use Testo\Assert\State\AssertException;
8+
use Testo\Assert\State\Record;
89
use Testo\Assert\StaticState;
910
use Testo\Interceptor\TestRunInterceptor;
1011
use Testo\Test\Dto\Status;
@@ -28,7 +29,7 @@ public function runTest(TestInfo $info, callable $next): TestResult
2829
$result = $next($info);
2930

3031
# Convert Error status to Failed if caused by an assertion failure
31-
$result->status === Status::Error && $result->failure instanceof AssertException and $result = $result
32+
$result->status === Status::Error && $result->failure instanceof Record and $result = $result
3233
->with(status: Status::Failed);
3334

3435
$state = StaticState::current();

0 commit comments

Comments
 (0)