1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MyTester; |
5
|
|
|
|
6
|
|
|
use MyTester\Annotations\Attributes\Fail; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Test suite for class Assert |
11
|
|
|
* |
12
|
|
|
* @author Jakub Konečný |
13
|
|
|
*/ |
14
|
|
|
final class AssertTest extends TestCase { |
15
|
|
|
public function shutDown(): void { |
16
|
|
|
$this->assertFalse($this->shouldFail); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Test assertion functions |
21
|
|
|
*/ |
22
|
|
|
public function testAssertion(): void { |
23
|
|
|
$this->assertFalse($this->shouldFail); |
24
|
|
|
$this->assertSame("abc", "abc"); |
25
|
|
|
$this->assertNotSame("abc", "def"); |
26
|
|
|
$this->assertTrue(true); |
27
|
|
|
$this->assertTruthy(1); |
28
|
|
|
$this->assertFalse(false); |
29
|
|
|
$this->assertFalsey(0); |
30
|
|
|
$this->assertNull(null); |
31
|
|
|
$this->assertNotNull("abc"); |
32
|
|
|
$testArray = ["abc"]; |
33
|
|
|
$this->assertContains("abc", $testArray); |
34
|
|
|
$this->assertNotContains("def", $testArray); |
35
|
|
|
$this->assertCount(1, $testArray); |
36
|
|
|
$this->assertNotCount(0, $testArray); |
37
|
|
|
$this->assertType("array", $testArray); |
38
|
|
|
$this->assertType(__CLASS__, $this); |
39
|
|
|
$this->assertType("string", "abc"); |
40
|
|
|
$this->assertType("bool", true); |
41
|
|
|
$this->assertType("int", 42); |
42
|
|
|
$this->assertType("null", null); |
43
|
|
|
$this->assertType("object", new stdClass()); |
44
|
|
|
$this->assertType("scalar", 42); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test assertion function, all should produce error |
49
|
|
|
* |
50
|
|
|
* @fail |
51
|
|
|
*/ |
52
|
|
|
#[Fail()] |
|
|
|
|
53
|
|
|
public function testAssertionFails(): void { |
54
|
|
|
$this->assertFalse($this->shouldFail); |
55
|
|
|
$actual = "abc"; |
56
|
|
|
$this->assertTrue(false); |
57
|
|
|
$this->assertTruthy(0); |
58
|
|
|
$this->assertFalse(true); |
59
|
|
|
$this->assertFalsey(0); |
60
|
|
|
$this->assertSame("def", $actual); |
61
|
|
|
$this->assertContains("def", $actual); |
62
|
|
|
$this->assertNotContains("abc", $actual); |
63
|
|
|
$this->assertCount(1, $actual); |
64
|
|
|
$this->assertCount(0, [$actual]); |
65
|
|
|
$this->assertNotCount(1, $actual); |
66
|
|
|
$this->assertNotCount(1, [$actual]); |
67
|
|
|
$this->assertSame("abc", "def"); |
68
|
|
|
$this->assertType("array", $actual); |
69
|
|
|
$this->assertType("abc", $this); |
70
|
|
|
$this->assertType("string", true); |
71
|
|
|
$this->assertType("bool", $actual); |
72
|
|
|
$this->assertType("int", "42"); |
73
|
|
|
$this->assertType("null", $actual); |
74
|
|
|
$this->assertType("object", true); |
75
|
|
|
$this->assertType("scalar", new stdClass()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test custom assertions and custom messages |
80
|
|
|
*/ |
81
|
|
|
public function testCustomAssertion(): void { |
82
|
|
|
$this->assert("5 > 2", "5 is not greater that 2."); |
83
|
|
|
$this->assert("5 >= 2", "5 is not greater or equal to 2."); |
84
|
|
|
$this->assert("2 < 5", "2 is not lesser than 5."); |
85
|
|
|
$this->assert("2 <= 5", "2 is not lesser or equal to 5."); |
86
|
|
|
$this->assert("abc != def", "abc is def."); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
?> |