1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MyTester; |
6
|
|
|
|
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
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Test assertion functions |
18
|
|
|
*/ |
19
|
|
|
public function testAssertion(): void |
20
|
|
|
{ |
21
|
|
|
$this->assertSame("abc", "abc"); |
22
|
|
|
$this->assertNotSame("abc", "def"); |
23
|
|
|
$this->assertTrue(true); |
24
|
|
|
$this->assertTruthy(1); |
25
|
|
|
$this->assertFalse(false); |
26
|
|
|
$this->assertFalsey(0); |
27
|
|
|
$this->assertNull(null); |
28
|
|
|
$this->assertNotNull("abc"); |
29
|
|
|
$testArray = ["abc"]; |
30
|
|
|
$this->assertContains("abc", $testArray); |
31
|
|
|
$this->assertNotContains("def", $testArray); |
32
|
|
|
$this->assertCount(1, $testArray); |
33
|
|
|
$this->assertNotCount(0, $testArray); |
34
|
|
|
$this->assertType("array", $testArray); |
35
|
|
|
$this->assertType(__CLASS__, $this); |
36
|
|
|
$this->assertType("string", "abc"); |
37
|
|
|
$this->assertType("bool", true); |
38
|
|
|
$this->assertType("int", 42); |
39
|
|
|
$this->assertType("null", null); |
40
|
|
|
$this->assertType("object", new stdClass()); |
41
|
|
|
$this->assertType("scalar", 42); |
42
|
|
|
$this->assertThrowsException(function () { |
43
|
|
|
throw new \RuntimeException("abc", 1); |
44
|
|
|
}, \RuntimeException::class); |
45
|
|
|
$this->assertThrowsException(function () { |
46
|
|
|
throw new \RuntimeException("abc"); |
47
|
|
|
}, \RuntimeException::class); |
48
|
|
|
$this->assertThrowsException(function () { |
49
|
|
|
throw new \RuntimeException("abc", 1); |
50
|
|
|
}, \RuntimeException::class); |
51
|
|
|
$this->assertThrowsException(function () { |
52
|
|
|
throw new \RuntimeException("abc"); |
53
|
|
|
}, \RuntimeException::class, "abc"); |
54
|
|
|
$this->assertThrowsException(function () { |
55
|
|
|
throw new \RuntimeException("abc", 1); |
56
|
|
|
}, \RuntimeException::class, "abc"); |
57
|
|
|
$this->assertThrowsException(function () { |
58
|
|
|
throw new \RuntimeException("abc", 1); |
59
|
|
|
}, \RuntimeException::class, "abc", 1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test custom assertions and custom messages |
64
|
|
|
*/ |
65
|
|
|
public function testCustomAssertion(): void |
66
|
|
|
{ |
67
|
|
|
$this->assert("5 > 2", "5 is not greater that 2."); |
68
|
|
|
$this->assert("5 >= 2", "5 is not greater or equal to 2."); |
69
|
|
|
$this->assert("2 < 5", "2 is not lesser than 5."); |
70
|
|
|
$this->assert("2 <= 5", "2 is not lesser or equal to 5."); |
71
|
|
|
$this->assert("abc != def", "abc is def."); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testShowStringOrArray(): void |
75
|
|
|
{ |
76
|
|
|
$text = "abc"; |
77
|
|
|
$this->assertSame($text, $this->showStringOrArray($text)); |
78
|
|
|
$this->assertSame("(array)", $this->showStringOrArray([])); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|