Skip to content

Commit c20f871

Browse files
authored
Merge pull request #27: add Assert:equal(), Assert::notEqual()
2 parents 73ac506 + f3f48dd commit c20f871

File tree

5 files changed

+132
-3
lines changed

5 files changed

+132
-3
lines changed

src/Assert.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,47 @@ 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 equals(mixed $expected, mixed $actual, string $message = ''): void
62+
{
63+
$actual == $expected
64+
? StaticState::log('Assert equals: `' . Support::stringify($expected) . '`', $message)
65+
: StaticState::fail(AssertException::compare(
66+
$expected,
67+
$actual,
68+
$message,
69+
pattern: 'Failed asserting that `%1s` is equals to `%2s`',
70+
));
71+
}
72+
73+
/**
74+
* Asserts that two values are not equal (not strict).
75+
*
76+
* @param mixed $expected The expected value.
77+
* @param mixed $actual The actual value to compare against the expected value.
78+
* @param string $message Short description about what exactly is being asserted.
79+
* @throws AssertException when the assertion fails.
80+
*/
81+
public static function notEquals(mixed $expected, mixed $actual, string $message = ''): void
82+
{
83+
$actual != $expected
84+
? StaticState::log('Assert not equals: `' . Support::stringify($expected) . '`', $message)
85+
: StaticState::fail(AssertException::compare(
86+
$expected,
87+
$actual,
88+
$message,
89+
pattern: 'Failed asserting that `%1s` is not equals to `%2s`',
90+
showDiff: false,
91+
));
92+
}
93+
5394
/**
5495
* Asserts that the condition is true.
5596
*
@@ -104,7 +145,7 @@ public static function instanceOf(string $expected, mixed $actual, string $messa
104145
$expected,
105146
$actual,
106147
$message,
107-
'Expected instance of `%2$s`, got `%1$s`',
148+
'Expected instance of `%1$s`, got `%2$s`',
108149
));
109150
}
110151

@@ -128,7 +169,7 @@ public static function contains(mixed $needle, iterable $haystack, string $messa
128169
$needle,
129170
$haystack,
130171
$message,
131-
'Failed asserting that `%1$s` contains `%2$s`',
172+
'Failed asserting that `%2$s` contains `%1$s`',
132173
));
133174
}
134175

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Testo\Attribute\RetryPolicy;
1010
use Testo\Attribute\Test;
1111

12+
/**
13+
* Assertion examples.
14+
*/
1215
final class AsserTest
1316
{
1417
#[Test]
@@ -22,6 +25,8 @@ public function simpleAssertions(): void
2225
Assert::contains(1, [1,2,3]);
2326
Assert::contains(2, new \ArrayIterator([1,2,3]));
2427
Assert::instanceOf(\Exception::class, new \RuntimeException());
28+
Assert::equals(1, '1');
29+
Assert::notEquals(42, 43);
2530
}
2631

2732
#[Test]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Testo\Assert;
6+
7+
use Testo\Assert;
8+
use Testo\Attribute\Test;
9+
10+
/**
11+
* Assertion examples.
12+
*/
13+
final class AssertEquals
14+
{
15+
#[Test]
16+
public function numbers(): void
17+
{
18+
Assert::equals(1, 1);
19+
Assert::equals(1, 1.0);
20+
Assert::equals(1.0, 1);
21+
Assert::equals("2", 2);
22+
}
23+
24+
#[Test]
25+
public function arrays(): void
26+
{
27+
# Same
28+
Assert::equals([1, 2], [1, 2]);
29+
Assert::equals(
30+
['a' => 1, 'b' => 2],
31+
['a' => 1, 'b' => 2],
32+
);
33+
# Different order
34+
Assert::equals(
35+
['b' => 2, 'a' => 1],
36+
['a' => 1, 'b' => 2],
37+
);
38+
}
39+
40+
#[Test]
41+
public function objects(): void
42+
{
43+
Assert::equals(
44+
(object) ['a' => 1, 'b' => 2],
45+
(object) ['b' => 2, 'a' => 1],
46+
);
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Testo\Assert;
6+
7+
use Testo\Assert;
8+
use Testo\Attribute\Test;
9+
10+
/**
11+
* Assertion examples.
12+
*/
13+
final class AssertNotEquals
14+
{
15+
#[Test]
16+
public function numbers(): void
17+
{
18+
Assert::notEquals(1, 2);
19+
}
20+
21+
#[Test]
22+
public function arrays(): void
23+
{
24+
Assert::notEquals([2, 1], [1, 2]);
25+
}
26+
27+
#[Test]
28+
public function objects(): void
29+
{
30+
Assert::notEquals(
31+
(object) ['a' => 1],
32+
(object) ['a' => 2],
33+
);
34+
}
35+
}

0 commit comments

Comments
 (0)