Skip to content

Commit cbedd79

Browse files
committed
Throw RuntimeException from CodeCoverage namespace
1 parent 0e6ad85 commit cbedd79

File tree

10 files changed

+216
-14
lines changed

10 files changed

+216
-14
lines changed

src/Report/Clover.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

12+
use PHPUnit\Framework\Error\Warning;
1213
use SebastianBergmann\CodeCoverage\CodeCoverage;
1314
use SebastianBergmann\CodeCoverage\Node\File;
1415
use SebastianBergmann\CodeCoverage\RuntimeException;
@@ -19,7 +20,7 @@
1920
final class Clover
2021
{
2122
/**
22-
* @throws \RuntimeException
23+
* @throws RuntimeException
2324
*/
2425
public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string
2526
{
@@ -235,13 +236,13 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
235236

236237
if ($target !== null) {
237238
if (!$this->createDirectory(\dirname($target))) {
238-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
239+
throw new RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
239240
}
240241

241242
if (@\file_put_contents($target, $buffer) === false) {
242243
throw new RuntimeException(
243244
\sprintf(
244-
'Could not write to "%s',
245+
'Could not write to "%s"',
245246
$target
246247
)
247248
);
@@ -253,6 +254,10 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
253254

254255
private function createDirectory(string $directory): bool
255256
{
256-
return !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory));
257+
try {
258+
return !(!\is_dir($directory) && !\mkdir($directory, 0777, true) && !\is_dir($directory));
259+
} catch (Warning $warning) {
260+
return false;
261+
}
257262
}
258263
}

src/Report/Crap4j.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(int $threshold = 30)
2626
}
2727

2828
/**
29-
* @throws \RuntimeException
29+
* @throws RuntimeException
3030
*/
3131
public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string
3232
{
@@ -117,13 +117,13 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
117117

118118
if ($target !== null) {
119119
if (!$this->createDirectory(\dirname($target))) {
120-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
120+
throw new RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
121121
}
122122

123123
if (@\file_put_contents($target, $buffer) === false) {
124124
throw new RuntimeException(
125125
\sprintf(
126-
'Could not write to "%s',
126+
'Could not write to "%s"',
127127
$target
128128
)
129129
);

src/Report/Html/Facade.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, s
4949
/**
5050
* @throws RuntimeException
5151
* @throws \InvalidArgumentException
52-
* @throws \RuntimeException
5352
*/
5453
public function process(CodeCoverage $coverage, string $target): void
5554
{
@@ -94,7 +93,7 @@ public function process(CodeCoverage $coverage, string $target): void
9493

9594
if ($node instanceof DirectoryNode) {
9695
if (!$this->createDirectory($target . $id)) {
97-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $target . $id));
96+
throw new RuntimeException(\sprintf('Directory "%s" was not created', $target . $id));
9897
}
9998

10099
$directory->render($node, $target . $id . '/index.html');
@@ -103,7 +102,7 @@ public function process(CodeCoverage $coverage, string $target): void
103102
$dir = \dirname($target . $id);
104103

105104
if (!$this->createDirectory($dir)) {
106-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $dir));
105+
throw new RuntimeException(\sprintf('Directory "%s" was not created', $dir));
107106
}
108107

109108
$file->render($node, $target . $id . '.html');

src/Report/PHP.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
final class PHP
1919
{
2020
/**
21-
* @throws \SebastianBergmann\CodeCoverage\RuntimeException
21+
* @throws RuntimeException
2222
*/
2323
public function process(CodeCoverage $coverage, ?string $target = null): string
2424
{
@@ -41,13 +41,13 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
4141

4242
if ($target !== null) {
4343
if (!$this->createDirectory(\dirname($target))) {
44-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
44+
throw new RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
4545
}
4646

4747
if (@\file_put_contents($target, $buffer) === false) {
4848
throw new RuntimeException(
4949
\sprintf(
50-
'Could not write to "%s',
50+
'Could not write to "%s"',
5151
$target
5252
)
5353
);

tests/_files/BankAccount.php.txt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
$coverage = new SebastianBergmann\CodeCoverage\CodeCoverage;
3+
$coverage->setData(array (
4+
'%s%etests/_files/BankAccount.php' =>
5+
array (
6+
8 =>
7+
array (
8+
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
9+
1 => 'BankAccountTest::testDepositWithdrawMoney',
10+
),
11+
9 => NULL,
12+
13 =>
13+
array (
14+
),
15+
14 =>
16+
array (
17+
),
18+
15 =>
19+
array (
20+
),
21+
16 =>
22+
array (
23+
),
24+
18 =>
25+
array (
26+
),
27+
22 =>
28+
array (
29+
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
30+
1 => 'BankAccountTest::testDepositWithdrawMoney',
31+
),
32+
24 =>
33+
array (
34+
0 => 'BankAccountTest::testDepositWithdrawMoney',
35+
),
36+
25 => NULL,
37+
29 =>
38+
array (
39+
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
40+
1 => 'BankAccountTest::testDepositWithdrawMoney',
41+
),
42+
31 =>
43+
array (
44+
0 => 'BankAccountTest::testDepositWithdrawMoney',
45+
),
46+
32 => NULL,
47+
),
48+
));
49+
$coverage->setTests(array (
50+
'BankAccountTest::testBalanceIsInitiallyZero' =>
51+
array (
52+
'size' => 'unknown',
53+
'status' => -1,
54+
),
55+
'BankAccountTest::testBalanceCannotBecomeNegative' =>
56+
array (
57+
'size' => 'unknown',
58+
'status' => -1,
59+
),
60+
'BankAccountTest::testBalanceCannotBecomeNegative2' =>
61+
array (
62+
'size' => 'unknown',
63+
'status' => -1,
64+
),
65+
'BankAccountTest::testDepositWithdrawMoney' =>
66+
array (
67+
'size' => 'unknown',
68+
'status' => -1,
69+
),
70+
));
71+
72+
$filter = $coverage->filter();
73+
$filter->setWhitelistedFiles(array (
74+
'%s%etests/_files/BankAccount.php' => true,
75+
));
76+
77+
return $coverage;

tests/tests/CloverTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

12+
use SebastianBergmann\CodeCoverage\RuntimeException;
1213
use SebastianBergmann\CodeCoverage\TestCase;
1314

1415
/**
@@ -45,4 +46,22 @@ public function testCloverForClassWithAnonymousFunction(): void
4546
$clover->process($this->getCoverageForClassWithAnonymousFunction())
4647
);
4748
}
49+
50+
public function testCloverThrowsRuntimeExceptionWhenUnableToWriteToTarget(): void
51+
{
52+
$this->expectException(RuntimeException::class);
53+
$this->expectExceptionMessage('Could not write to "stdout://"');
54+
55+
$clover = new Clover;
56+
$clover->process($this->getCoverageForBankAccount(), 'stdout://');
57+
}
58+
59+
public function testCloverThrowsRuntimeExceptionWhenTargetDirCouldNotBeCreated(): void
60+
{
61+
$this->expectException(RuntimeException::class);
62+
$this->expectExceptionMessage('Directory "/foo/bar" was not created');
63+
64+
$clover = new Clover;
65+
$clover->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
66+
}
4867
}

tests/tests/Crap4jTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

12+
use SebastianBergmann\CodeCoverage\RuntimeException;
1213
use SebastianBergmann\CodeCoverage\TestCase;
1314

1415
/**
@@ -45,4 +46,22 @@ public function testForClassWithAnonymousFunction(): void
4546
$crap4j->process($this->getCoverageForClassWithAnonymousFunction(), null, 'CoverageForClassWithAnonymousFunction')
4647
);
4748
}
49+
50+
public function testCrap4jThrowsRuntimeExceptionWhenUnableToWriteToTarget(): void
51+
{
52+
$this->expectException(RuntimeException::class);
53+
$this->expectExceptionMessage('Could not write to "stdout://"');
54+
55+
$Crap4j = new Crap4j;
56+
$Crap4j->process($this->getCoverageForBankAccount(), 'stdout://');
57+
}
58+
59+
public function testCrap4jThrowsRuntimeExceptionWhenTargetDirWasNotCreated(): void
60+
{
61+
$this->expectException(RuntimeException::class);
62+
$this->expectExceptionMessage('Directory "/foo/bar" was not created');
63+
64+
$Crap4j = new Crap4j;
65+
$Crap4j->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
66+
}
4867
}

tests/tests/HTMLTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report\Html;
1111

12+
use SebastianBergmann\CodeCoverage\RuntimeException;
1213
use SebastianBergmann\CodeCoverage\TestCase;
1314

1415
class HTMLTest extends TestCase
@@ -69,6 +70,15 @@ public function testForClassWithAnonymousFunction(): void
6970
$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
7071
}
7172

73+
public function testThrowsRuntimeExceptionWhenTargetDirDoesNotExist(): void
74+
{
75+
$this->expectException(RuntimeException::class);
76+
$this->expectExceptionMessage('Directory "/foo/bar/baz/" does not exist.');
77+
78+
$report = new Facade();
79+
$report->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
80+
}
81+
7282
/**
7383
* @param string $expectedFilesPath
7484
* @param string $actualFilesPath

tests/tests/PHPTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of the php-code-coverage package.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Report;
11+
12+
use SebastianBergmann\CodeCoverage\RuntimeException;
13+
use SebastianBergmann\CodeCoverage\TestCase;
14+
15+
/**
16+
* @covers SebastianBergmann\CodeCoverage\Report\PHP
17+
*/
18+
class PHPTest extends TestCase
19+
{
20+
public function testPHPForBankAccountTest(): void
21+
{
22+
$report = new PHP();
23+
24+
$this->assertStringMatchesFormatFile(
25+
TEST_FILES_PATH . 'BankAccount.php.txt',
26+
\str_replace(\PHP_EOL, "\n", $report->process($this->getCoverageForBankAccount()))
27+
);
28+
}
29+
30+
public function testReportThrowsRuntimeExceptionWhenUnableToWriteToTarget(): void
31+
{
32+
$this->expectException(RuntimeException::class);
33+
$this->expectExceptionMessage('Could not write to "stdin://"');
34+
35+
$report = new PHP();
36+
$report->process($this->getCoverageForBankAccount(), 'stdin://');
37+
}
38+
39+
public function testReportThrowsRuntimeExceptionWhenTargetDirCouldNotBeCreated(): void
40+
{
41+
$this->expectException(RuntimeException::class);
42+
$this->expectExceptionMessage('Directory "/foo/bar" was not created');
43+
44+
$report = new PHP();
45+
$report->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
46+
}
47+
}

tests/tests/XmlTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report\Xml;
1111

12+
use SebastianBergmann\CodeCoverage\RuntimeException;
1213
use SebastianBergmann\CodeCoverage\TestCase;
1314

1415
class XmlTest extends TestCase
@@ -30,7 +31,11 @@ protected function tearDown(): void
3031

3132
foreach ($tmpFilesIterator as $path => $fileInfo) {
3233
/* @var \SplFileInfo $fileInfo */
33-
\unlink($fileInfo->getPathname());
34+
if (!\is_dir($fileInfo->getPathname())) {
35+
\unlink($fileInfo->getPathname());
36+
} else {
37+
\rmdir($fileInfo->getPathname());
38+
}
3439
}
3540
}
3641

@@ -64,6 +69,27 @@ public function testForClassWithAnonymousFunction(): void
6469
$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
6570
}
6671

72+
public function testReportThrowsRuntimeExceptionWhenUnableToCreateTargetDir(): void
73+
{
74+
$this->expectException(RuntimeException::class);
75+
$this->expectExceptionMessage("'/foo/bar/baz/' could not be created.");
76+
77+
$xml = new Facade('1.0.0');
78+
$xml->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
79+
}
80+
81+
public function testReportThrowsRuntimeExceptionWhenUnableToWriteToTargetDir(): void
82+
{
83+
$target = self::$TEST_TMP_PATH . '/non-writable-dir';
84+
@\mkdir($target, 0555);
85+
86+
$this->expectException(RuntimeException::class);
87+
$this->expectExceptionMessage("'$target/' exists but is not writable");
88+
89+
$xml = new Facade('1.0.0');
90+
$xml->process($this->getCoverageForBankAccount(), $target);
91+
}
92+
6793
/**
6894
* @param string $expectedFilesPath
6995
* @param string $actualFilesPath

0 commit comments

Comments
 (0)