Skip to content

Commit eb3da87

Browse files
committed
feat(assert): add Assert:equal(), Assert::notEqual()
- feat: add new `Assert:equal()`, `Assert::notEqual()` - fix: change expected and actual values in correct order in default compare message
1 parent c42b58e commit eb3da87

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/Assert.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,42 @@ public static function notSame(mixed $expected, mixed $actual, string $message =
5050
));
5151
}
5252

53+
/**
54+
* Asserts that two values are equal (not strict).
55+
*
56+
* @param mixed $expected The expected value.
57+
* @param mixed $actual The actual value to compare against the expected value.
58+
* @param string $message Short description about what exactly is being asserted.
59+
* @throws AssertException when the assertion fails.
60+
*/
61+
public static function equal(mixed $expected, mixed $actual, string $message = ''): void
62+
{
63+
$actual == $expected
64+
? StaticState::log('Assert equal: `' . Support::stringify($expected) . '`', $message)
65+
: StaticState::fail(AssertException::compare($expected, $actual, $message));
66+
}
67+
68+
/**
69+
* Asserts that two values are not equal (not strict).
70+
*
71+
* @param mixed $expected The expected value.
72+
* @param mixed $actual The actual value to compare against the expected value.
73+
* @param string $message Short description about what exactly is being asserted.
74+
* @throws AssertException when the assertion fails.
75+
*/
76+
public static function notEqual(mixed $expected, mixed $actual, string $message = ''): void
77+
{
78+
$actual != $expected
79+
? StaticState::log('Assert not equal: `' . Support::stringify($expected) . '`', $message)
80+
: StaticState::fail(AssertException::compare(
81+
$expected,
82+
$actual,
83+
$message,
84+
pattern: 'Failed asserting that `%1s` is not equal to `%2s`',
85+
showDiff: false,
86+
));
87+
}
88+
5389
/**
5490
* Asserts that the condition is true.
5591
*
@@ -104,7 +140,7 @@ public static function instanceOf(string $expected, mixed $actual, string $messa
104140
$expected,
105141
$actual,
106142
$message,
107-
'Expected instance of `%2$s`, got `%1$s`',
143+
'Expected instance of `%1$s`, got `%2$s`',
108144
));
109145
}
110146

@@ -128,7 +164,7 @@ public static function contains(mixed $needle, iterable $haystack, string $messa
128164
$needle,
129165
$haystack,
130166
$message,
131-
'Failed asserting that `%1$s` contains `%2$s`',
167+
'Failed asserting that `%2$s` contains `%1$s`',
132168
));
133169
}
134170

src/Assert/State/AssertException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public static function compare(
4545

4646
$msg = \sprintf(
4747
$pattern,
48-
Support::stringify($actual),
4948
Support::stringify($expected),
49+
Support::stringify($actual),
5050
);
5151
return new self(
5252
assertion: $msg,

tests/Testo/AsserTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function simpleAssertions(): void
2222
Assert::contains(1, [1,2,3]);
2323
Assert::contains(2, new \ArrayIterator([1,2,3]));
2424
Assert::instanceOf(\Exception::class, new \RuntimeException());
25+
Assert::equal(1, '1');
26+
Assert::notEqual(42,43);
2527
}
2628

2729
#[Test]

0 commit comments

Comments
 (0)