-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCase.php
47 lines (39 loc) · 1.47 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types = 1);
namespace Framework\Test;
/** Every unit test should base on this class. It provides the assertion functions. */
class TestCase
{
public function assertFalse(bool $assertion): void
{
$this->assertTrue(!$assertion);
}
public function assertTrue(bool $assertion): void
{
if (!$assertion) {
throw new AssertionFailedException('true', gettype(true), $assertion ? 'true' : 'false', gettype(true),);
}
}
public function assertEquals(mixed $expected, mixed $actual): void
{
if (gettype($expected) !== gettype($actual)) {
throw new AssertionFailedException($this->mixedToString($expected), gettype($expected), $this->mixedToString($actual), gettype($actual));
}
if (gettype($expected) === 'array') {
if (json_encode($expected) === json_encode($actual)) {
return;
}
throw new AssertionFailedException($this->mixedToString($expected), gettype($expected), $this->mixedToString($actual), gettype($actual));
}
if ($expected !== $actual) {
throw new AssertionFailedException($this->mixedToString($expected), gettype($expected), $this->mixedToString($actual), gettype($actual));
}
}
private function mixedToString(mixed $value): string
{
return match (gettype($value)) {
'array' => json_encode($value),
default => $value,
};
}
}