Skip to content

Throw RuntimeException from CodeCoverage namespace #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Report/Clover.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\CodeCoverage\Report;

use PHPUnit\Framework\Error\Warning;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Node\File;
use SebastianBergmann\CodeCoverage\RuntimeException;
Expand All @@ -19,7 +20,7 @@
final class Clover
{
/**
* @throws \RuntimeException
* @throws RuntimeException
*/
public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string
{
Expand Down Expand Up @@ -235,13 +236,13 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string

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

if (@\file_put_contents($target, $buffer) === false) {
throw new RuntimeException(
\sprintf(
'Could not write to "%s',
'Could not write to "%s"',
$target
)
);
Expand All @@ -253,6 +254,10 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string

private function createDirectory(string $directory): bool
{
return !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory));
try {
return !(!\is_dir($directory) && !\mkdir($directory, 0777, true) && !\is_dir($directory));
} catch (Warning $warning) {
return false;
}
}
}
6 changes: 3 additions & 3 deletions src/Report/Crap4j.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(int $threshold = 30)
}

/**
* @throws \RuntimeException
* @throws RuntimeException
*/
public function process(CodeCoverage $coverage, ?string $target = null, ?string $name = null): string
{
Expand Down Expand Up @@ -117,13 +117,13 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string

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

if (@\file_put_contents($target, $buffer) === false) {
throw new RuntimeException(
\sprintf(
'Could not write to "%s',
'Could not write to "%s"',
$target
)
);
Expand Down
5 changes: 2 additions & 3 deletions src/Report/Html/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, s
/**
* @throws RuntimeException
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function process(CodeCoverage $coverage, string $target): void
{
Expand Down Expand Up @@ -94,7 +93,7 @@ public function process(CodeCoverage $coverage, string $target): void

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

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

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

$file->render($node, $target . $id . '.html');
Expand Down
6 changes: 3 additions & 3 deletions src/Report/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
final class PHP
{
/**
* @throws \SebastianBergmann\CodeCoverage\RuntimeException
* @throws RuntimeException
*/
public function process(CodeCoverage $coverage, ?string $target = null): string
{
Expand All @@ -41,13 +41,13 @@ public function process(CodeCoverage $coverage, ?string $target = null): string

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

if (@\file_put_contents($target, $buffer) === false) {
throw new RuntimeException(
\sprintf(
'Could not write to "%s',
'Could not write to "%s"',
$target
)
);
Expand Down
77 changes: 77 additions & 0 deletions tests/_files/BankAccount.php.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
$coverage = new SebastianBergmann\CodeCoverage\CodeCoverage;
$coverage->setData(array (
'%s%etests/_files/BankAccount.php' =>
array (
8 =>
array (
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
1 => 'BankAccountTest::testDepositWithdrawMoney',
),
9 => NULL,
13 =>
array (
),
14 =>
array (
),
15 =>
array (
),
16 =>
array (
),
18 =>
array (
),
22 =>
array (
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
1 => 'BankAccountTest::testDepositWithdrawMoney',
),
24 =>
array (
0 => 'BankAccountTest::testDepositWithdrawMoney',
),
25 => NULL,
29 =>
array (
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1 => 'BankAccountTest::testDepositWithdrawMoney',
),
31 =>
array (
0 => 'BankAccountTest::testDepositWithdrawMoney',
),
32 => NULL,
),
));
$coverage->setTests(array (
'BankAccountTest::testBalanceIsInitiallyZero' =>
array (
'size' => 'unknown',
'status' => -1,
),
'BankAccountTest::testBalanceCannotBecomeNegative' =>
array (
'size' => 'unknown',
'status' => -1,
),
'BankAccountTest::testBalanceCannotBecomeNegative2' =>
array (
'size' => 'unknown',
'status' => -1,
),
'BankAccountTest::testDepositWithdrawMoney' =>
array (
'size' => 'unknown',
'status' => -1,
),
));

$filter = $coverage->filter();
$filter->setWhitelistedFiles(array (
'%s%etests/_files/BankAccount.php' => true,
));

return $coverage;
19 changes: 19 additions & 0 deletions tests/tests/CloverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\CodeCoverage\Report;

use SebastianBergmann\CodeCoverage\RuntimeException;
use SebastianBergmann\CodeCoverage\TestCase;

/**
Expand Down Expand Up @@ -45,4 +46,22 @@ public function testCloverForClassWithAnonymousFunction(): void
$clover->process($this->getCoverageForClassWithAnonymousFunction())
);
}

public function testCloverThrowsRuntimeExceptionWhenUnableToWriteToTarget(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not write to "stdout://"');

$clover = new Clover;
$clover->process($this->getCoverageForBankAccount(), 'stdout://');
}

public function testCloverThrowsRuntimeExceptionWhenTargetDirCouldNotBeCreated(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Directory "/foo/bar" was not created');

$clover = new Clover;
$clover->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
}
}
19 changes: 19 additions & 0 deletions tests/tests/Crap4jTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\CodeCoverage\Report;

use SebastianBergmann\CodeCoverage\RuntimeException;
use SebastianBergmann\CodeCoverage\TestCase;

/**
Expand Down Expand Up @@ -45,4 +46,22 @@ public function testForClassWithAnonymousFunction(): void
$crap4j->process($this->getCoverageForClassWithAnonymousFunction(), null, 'CoverageForClassWithAnonymousFunction')
);
}

public function testCrap4jThrowsRuntimeExceptionWhenUnableToWriteToTarget(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not write to "stdout://"');

$Crap4j = new Crap4j;
$Crap4j->process($this->getCoverageForBankAccount(), 'stdout://');
}

public function testCrap4jThrowsRuntimeExceptionWhenTargetDirWasNotCreated(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Directory "/foo/bar" was not created');

$Crap4j = new Crap4j;
$Crap4j->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
}
}
10 changes: 10 additions & 0 deletions tests/tests/HTMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\CodeCoverage\Report\Html;

use SebastianBergmann\CodeCoverage\RuntimeException;
use SebastianBergmann\CodeCoverage\TestCase;

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

public function testThrowsRuntimeExceptionWhenTargetDirDoesNotExist(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Directory "/foo/bar/baz/" does not exist.');

$report = new Facade();
$report->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
}

/**
* @param string $expectedFilesPath
* @param string $actualFilesPath
Expand Down
47 changes: 47 additions & 0 deletions tests/tests/PHPTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);
/*
* This file is part of the php-code-coverage package.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeCoverage\Report;

use SebastianBergmann\CodeCoverage\RuntimeException;
use SebastianBergmann\CodeCoverage\TestCase;

/**
* @covers SebastianBergmann\CodeCoverage\Report\PHP
*/
class PHPTest extends TestCase
{
public function testPHPForBankAccountTest(): void
{
$report = new PHP();

$this->assertStringMatchesFormatFile(
TEST_FILES_PATH . 'BankAccount.php.txt',
\str_replace(\PHP_EOL, "\n", $report->process($this->getCoverageForBankAccount()))
);
}

public function testReportThrowsRuntimeExceptionWhenUnableToWriteToTarget(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not write to "stdin://"');

$report = new PHP();
$report->process($this->getCoverageForBankAccount(), 'stdin://');
}

public function testReportThrowsRuntimeExceptionWhenTargetDirCouldNotBeCreated(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Directory "/foo/bar" was not created');

$report = new PHP();
$report->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
}
}
28 changes: 27 additions & 1 deletion tests/tests/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace SebastianBergmann\CodeCoverage\Report\Xml;

use SebastianBergmann\CodeCoverage\RuntimeException;
use SebastianBergmann\CodeCoverage\TestCase;

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

foreach ($tmpFilesIterator as $path => $fileInfo) {
/* @var \SplFileInfo $fileInfo */
\unlink($fileInfo->getPathname());
if (!\is_dir($fileInfo->getPathname())) {
\unlink($fileInfo->getPathname());
} else {
\rmdir($fileInfo->getPathname());
}
}
}

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

public function testReportThrowsRuntimeExceptionWhenUnableToCreateTargetDir(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("'/foo/bar/baz/' could not be created.");

$xml = new Facade('1.0.0');
$xml->process($this->getCoverageForBankAccount(), '/foo/bar/baz');
}

public function testReportThrowsRuntimeExceptionWhenUnableToWriteToTargetDir(): void
{
$target = self::$TEST_TMP_PATH . '/non-writable-dir';
@\mkdir($target, 0555);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage("'$target/' exists but is not writable");

$xml = new Facade('1.0.0');
$xml->process($this->getCoverageForBankAccount(), $target);
}

/**
* @param string $expectedFilesPath
* @param string $actualFilesPath
Expand Down