-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
PhptTestCaseTest.php
254 lines (208 loc) · 6.08 KB
/
PhptTestCaseTest.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner;
use const PHP_EOL;
use function file_put_contents;
use function sys_get_temp_dir;
use function touch;
use function unlink;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Util\PHP\AbstractPhpProcess;
/**
* @medium
*/
final class PhptTestCaseTest extends TestCase
{
private const EXPECT_CONTENT = <<<'EOF'
--TEST--
EXPECT test
--FILE--
<?php echo "Hello PHPUnit!"; ?>
--EXPECT--
Hello PHPUnit!
EOF;
private const EXPECTF_CONTENT = <<<'EOF'
--TEST--
EXPECTF test
--FILE--
<?php echo "Hello PHPUnit!"; ?>
--EXPECTF--
Hello %s!
EOF;
private const EXPECTREGEX_CONTENT = <<<'EOF'
--TEST--
EXPECTREGEX test
--FILE--
<?php echo "Hello PHPUnit!"; ?>
--EXPECTREGEX--
Hello [HPU]{4}[nit]{3}!
EOF;
private const EXPECT_MISSING_ASSERTION_CONTENT = <<<'EOF'
--TEST--
Missing EXPECTF value test
--EXPECTF--
--FILE--
<?php echo "Hello PHPUnit!"; ?>
EOF;
private const FILE_SECTION = <<<'EOF'
<?php echo "Hello PHPUnit!"; ?>
EOF;
/**
* @var string
*/
private $dirname;
/**
* @var string
*/
private $filename;
/**
* @var PhptTestCase
*/
private $testCase;
/**
* @var AbstractPhpProcess|\PHPUnit\Framework\MockObject\MockObject
*/
private $phpProcess;
protected function setUp(): void
{
$this->dirname = sys_get_temp_dir();
$this->filename = $this->dirname . '/phpunit.phpt';
touch($this->filename);
$this->phpProcess = $this->getMockForAbstractClass(AbstractPhpProcess::class, [], '', false);
$this->testCase = new PhptTestCase($this->filename, $this->phpProcess);
}
protected function tearDown(): void
{
@unlink($this->filename);
$this->phpProcess = null;
$this->testCase = null;
}
public function testAlwaysReportsNumberOfAssertionsIsOne(): void
{
$this->assertSame(1, $this->testCase->getNumAssertions());
}
public function testAlwaysReportsItDoesNotUseADataprovider(): void
{
$this->assertSame(false, $this->testCase->usesDataProvider());
}
public function testShouldNotRunTestSectionIfSkipifSectionReturnsOutputWithSkipWord(): void
{
$skipifSection = '<?php echo "skip: Reason"; ?>' . PHP_EOL;
$phptContent = self::EXPECT_CONTENT . PHP_EOL;
$phptContent .= '--SKIPIF--' . PHP_EOL;
$phptContent .= $skipifSection;
$this->setPhpContent($phptContent);
$this->phpProcess
->expects($this->once())
->method('runJob')
->with($skipifSection)
->willReturn(['stdout' => 'skip: Reason', 'stderr' => '']);
$this->testCase->run(new TestResult);
}
public function testShouldSkipTestWhenPhptFileIsEmpty(): void
{
$this->setPhpContent('');
$result = new TestResult;
$this->testCase->run($result);
$this->assertCount(1, $result->skipped());
$this->assertSame('Invalid PHPT file', $result->skipped()[0]->thrownException()->getMessage());
}
public function testShouldSkipTestWhenFileSectionIsMissing(): void
{
$this->setPhpContent(
<<<'EOF'
--TEST--
Something to describe it
--EXPECT--
Something
EOF
);
$result = new TestResult;
$this->testCase->run($result);
$this->assertCount(1, $result->skipped());
$this->assertSame('Invalid PHPT file', $result->skipped()[0]->thrownException()->getMessage());
}
public function testShouldSkipTestWhenThereIsNoExpecOrExpectifOrExpecregexSectionInPhptFile(): void
{
$this->setPhpContent(
<<<EOF
--TEST--
Something to describe it
--FILE--
<?php declare(strict_types=1);
echo "Hello world!\n";
?>
EOF
);
$result = new TestResult;
$this->testCase->run($result);
$this->assertCount(1, $result->skipped());
$skipMessage = $result->skipped()[0]->thrownException()->getMessage();
$this->assertSame('Invalid PHPT file', $skipMessage);
}
public function testShouldSkipTestWhenSectionHeaderIsMalformed(): void
{
$this->setPhpContent(
<<<'EOF'
----
--TEST--
This is not going to work out
--EXPECT--
Tears and misery
EOF
);
$result = new TestResult;
$this->testCase->run($result);
$this->assertCount(1, $result->skipped());
$skipMessage = $result->skipped()[0]->thrownException()->getMessage();
$this->assertSame('Invalid PHPT file: empty section header', $skipMessage);
}
public function testShouldSkipTestWhenExpectHasNoValue(): void
{
$this->setPhpContent(self::EXPECT_MISSING_ASSERTION_CONTENT);
$result = new TestResult;
$this->testCase->run($result);
$this->assertCount(1, $result->errors());
$skipMessage = $result->errors()[0]->thrownException()->getMessage();
$this->assertSame('No PHPT expectation found', $skipMessage);
}
/**
* @testdox PHPT tests return their filename as test name
*/
public function testPHPTReturnsFilenameAsTestName(): void
{
$this->assertSame($this->filename, $this->testCase->getName());
}
/**
* @testdox PHPT tests return their filename as unique sortId
*/
public function testPHPTReturnsFilenameAsSortId(): void
{
$this->assertSame($this->filename, $this->testCase->sortId());
}
/**
* @testdox PHPT tests do not affect dependency resolution
*/
public function testPHPTDoesNotAffectDependencyResolution(): void
{
$this->assertSame([], $this->testCase->provides());
$this->assertSame([], $this->testCase->requires());
}
/**
* Defines the content of the current PHPT test.
*
* @param string $content
*/
private function setPhpContent($content): void
{
file_put_contents($this->filename, $content);
}
}