1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MyTester; |
5
|
|
|
|
6
|
|
|
use MyTester\Annotations\Attributes\DataProvider; |
|
|
|
|
7
|
|
|
use MyTester\Annotations\Attributes\Skip; |
8
|
|
|
use MyTester\Annotations\Attributes\Test; |
9
|
|
|
use MyTester\Annotations\Attributes\TestSuite; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Test suite for class TestCase |
13
|
|
|
* |
14
|
|
|
* @testSuite TestCase |
15
|
|
|
* @author Jakub Konečný |
16
|
|
|
* @property-read bool|int $one |
17
|
|
|
*/ |
18
|
|
|
#[TestSuite("TestCase")] |
|
|
|
|
19
|
|
|
final class TestCaseTest extends TestCase { |
20
|
|
|
/** @var bool|int */ |
21
|
|
|
private $one = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return bool|int |
25
|
|
|
*/ |
26
|
|
|
public function getOne() { |
27
|
|
|
return $this->one; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setUp(): void { |
31
|
|
|
$this->one = 1; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function tearDown(): void { |
35
|
|
|
$this->one = false; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function shutDown(): void { |
39
|
|
|
$this->assertFalsey($this->one); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testState(): void { |
43
|
|
|
$this->assertFalse($this->shouldFail); |
44
|
|
|
$this->assertSame(1, $this->getCounter()); |
45
|
|
|
$this->incCounter(); |
46
|
|
|
$this->assertSame(3, $this->getCounter()); |
47
|
|
|
$this->resetCounter(); |
48
|
|
|
$this->assertSame(0, $this->getCounter()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test parameters |
53
|
|
|
* |
54
|
|
|
* @dataProvider(dataProvider) |
55
|
|
|
*/ |
56
|
|
|
#[DataProvider("dataProvider")] |
|
|
|
|
57
|
|
|
public function testParams(string $text): void { |
58
|
|
|
$this->assertType("string", $text); |
59
|
|
|
$this->assertContains("a", $text); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function dataProvider(): array { |
63
|
|
|
return ["abc", "adef", ]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Test custom test's name |
68
|
|
|
* |
69
|
|
|
* @test Custom name |
70
|
|
|
*/ |
71
|
|
|
#[Test("Custom name")] |
|
|
|
|
72
|
|
|
public function testTestName(): void { |
73
|
|
|
$this->assertSame(1, $this->one); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test unconditional skipping |
78
|
|
|
* |
79
|
|
|
* @test Skip |
80
|
|
|
* @skip |
81
|
|
|
*/ |
82
|
|
|
#[Test("Skip")] |
|
|
|
|
83
|
|
|
#[Skip()] |
84
|
|
|
public function testSkip(): void { |
85
|
|
|
$this->assertTrue(false); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Test skipping based on boolean |
90
|
|
|
* |
91
|
|
|
* @test Boolean |
92
|
|
|
* @skip(true) |
93
|
|
|
*/ |
94
|
|
|
#[Test("Boolean")] |
|
|
|
|
95
|
|
|
#[Skip(true)] |
96
|
|
|
public function testSkipBoolean(): void { |
97
|
|
|
$this->assertTrue(false); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Test skipping based on integer |
102
|
|
|
* |
103
|
|
|
* @test Integer |
104
|
|
|
* @skip(1) |
105
|
|
|
*/ |
106
|
|
|
#[Test("Integer")] |
|
|
|
|
107
|
|
|
#[Skip(1)] |
108
|
|
|
public function testSkipInteger(): void { |
109
|
|
|
$this->assertTrue(false); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test skipping based on float |
114
|
|
|
* |
115
|
|
|
* @test Float |
116
|
|
|
* @skip(1.5) |
117
|
|
|
*/ |
118
|
|
|
#[Test("Integer")] |
|
|
|
|
119
|
|
|
#[Skip(1.5)] |
120
|
|
|
public function testSkipFloat(): void { |
121
|
|
|
$this->assertTrue(false); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Test skipping based on string |
126
|
|
|
* |
127
|
|
|
* @test String |
128
|
|
|
* @skip(abc) |
129
|
|
|
*/ |
130
|
|
|
#[Test("String")] |
|
|
|
|
131
|
|
|
#[Skip("abc")] |
132
|
|
|
public function testSkipString(): void { |
133
|
|
|
$this->assertTrue(false); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Test skipping based on PHP version |
138
|
|
|
* |
139
|
|
|
* @test PHP version |
140
|
|
|
* @skip(php=666) |
141
|
|
|
*/ |
142
|
|
|
#[Test("PHP version")] |
|
|
|
|
143
|
|
|
#[Skip(["php" => 666])] |
144
|
|
|
public function testSkipPhpVersion(): void { |
145
|
|
|
$this->assertTrue(false); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Test skipping based on sapi |
150
|
|
|
* |
151
|
|
|
* @test CGI sapi |
152
|
|
|
* @skip(sapi=abc) |
153
|
|
|
*/ |
154
|
|
|
#[Test("CGI sapi")] |
|
|
|
|
155
|
|
|
#[Skip(["sapi" => "abc"])] |
156
|
|
|
public function testCgiSapi(): void { |
157
|
|
|
$this->assertNotSame(PHP_SAPI, "abc"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test skipping based on loaded extension |
162
|
|
|
* |
163
|
|
|
* @test Extension |
164
|
|
|
* @skip(extension=abc) |
165
|
|
|
*/ |
166
|
|
|
#[Test("Extension")] |
|
|
|
|
167
|
|
|
#[Skip(["extension" => "abc"])] |
168
|
|
|
public function testSkipExtension(): void { |
169
|
|
|
$this->assertTrue(false); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
?> |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: