Skip to content

Commit d0050e8

Browse files
Add tests
1 parent 59bf95d commit d0050e8

2 files changed

Lines changed: 191 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\Serialization;
11+
12+
use PHPUnit\Framework\Attributes\CoversClass;
13+
use PHPUnit\Framework\Attributes\Small;
14+
use PHPUnit\Framework\TestCase;
15+
16+
#[CoversClass(PharPrefixCouldNotBeStrippedException::class)]
17+
#[Small]
18+
final class PharPrefixCouldNotBeStrippedExceptionTest extends TestCase
19+
{
20+
public function testHasMessage(): void
21+
{
22+
$e = new PharPrefixCouldNotBeStrippedException;
23+
24+
$this->assertSame('Failed to strip PHAR prefix from serialized data', $e->getMessage());
25+
}
26+
}

tests/tests/Report/FacadeTest.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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 const DIRECTORY_SEPARATOR;
13+
use const PHP_EOL;
14+
use function file_get_contents;
15+
use function str_replace;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use SebastianBergmann\CodeCoverage\Serialization\Serializer;
18+
use SebastianBergmann\CodeCoverage\Serialization\Unserializer;
19+
use SebastianBergmann\CodeCoverage\TestCase;
20+
21+
#[CoversClass(Facade::class)]
22+
final class FacadeTest extends TestCase
23+
{
24+
protected function tearDown(): void
25+
{
26+
$this->removeTemporaryFiles();
27+
}
28+
29+
public function testCanBeCreatedFromCodeCoverageObject(): void
30+
{
31+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
32+
33+
$this->assertInstanceOf(Facade::class, $facade);
34+
}
35+
36+
public function testCanBeCreatedFromSerializedData(): void
37+
{
38+
$serializedFile = TEST_FILES_PATH . 'tmp' . DIRECTORY_SEPARATOR . 'FacadeTest_serialized.php';
39+
40+
(new Serializer)->serialize($serializedFile, $this->getLineCoverageForBankAccount());
41+
42+
$serializedData = (new Unserializer)->unserialize($serializedFile);
43+
44+
$facade = Facade::fromSerializedData($serializedData);
45+
46+
$this->assertInstanceOf(Facade::class, $facade);
47+
}
48+
49+
public function testFromSerializedDataCanRenderText(): void
50+
{
51+
$serializedFile = TEST_FILES_PATH . 'tmp' . DIRECTORY_SEPARATOR . 'FacadeTest_serialized.php';
52+
53+
(new Serializer)->serialize($serializedFile, $this->getLineCoverageForBankAccount());
54+
55+
$serializedData = (new Unserializer)->unserialize($serializedFile);
56+
57+
$facade = Facade::fromSerializedData($serializedData);
58+
$result = $facade->renderText(null);
59+
60+
$this->assertNotEmpty($result);
61+
$this->assertStringContainsString('Summary:', $result);
62+
}
63+
64+
public function testRenderTextReturnsString(): void
65+
{
66+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
67+
68+
$result = $facade->renderText(null);
69+
70+
$this->assertStringMatchesFormatFile(
71+
TEST_FILES_PATH . 'Report/Text/BankAccount-line.txt',
72+
str_replace(PHP_EOL, "\n", $result),
73+
);
74+
}
75+
76+
public function testRenderTextWritesToFile(): void
77+
{
78+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
79+
$target = TEST_FILES_PATH . 'tmp' . DIRECTORY_SEPARATOR . 'FacadeTest_text.txt';
80+
81+
$result = $facade->renderText($target);
82+
83+
$this->assertFileExists($target);
84+
$this->assertStringMatchesFormatFile(
85+
TEST_FILES_PATH . 'Report/Text/BankAccount-line.txt',
86+
str_replace(PHP_EOL, "\n", file_get_contents($target)),
87+
);
88+
}
89+
90+
public function testRenderClover(): void
91+
{
92+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
93+
$target = TEST_FILES_PATH . 'tmp' . DIRECTORY_SEPARATOR . 'FacadeTest_clover.xml';
94+
95+
$facade->renderClover($target, 'BankAccount');
96+
97+
$this->assertFileExists($target);
98+
99+
$this->assertStringMatchesFormatFile(
100+
TEST_FILES_PATH . 'Report/Clover/BankAccount-line.xml',
101+
file_get_contents($target),
102+
);
103+
}
104+
105+
public function testRenderOpenClover(): void
106+
{
107+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
108+
$target = TEST_FILES_PATH . 'tmp' . DIRECTORY_SEPARATOR . 'FacadeTest_openclover.xml';
109+
110+
$facade->renderOpenClover($target, 'BankAccount');
111+
112+
$this->assertFileExists($target);
113+
114+
$this->assertStringMatchesFormatFile(
115+
TEST_FILES_PATH . 'Report/OpenClover/BankAccount-line.xml',
116+
file_get_contents($target),
117+
);
118+
}
119+
120+
public function testRenderCobertura(): void
121+
{
122+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
123+
$target = TEST_FILES_PATH . 'tmp' . DIRECTORY_SEPARATOR . 'FacadeTest_cobertura.xml';
124+
125+
$facade->renderCobertura($target);
126+
127+
$this->assertFileExists($target);
128+
$this->assertStringContainsString('<?xml', file_get_contents($target));
129+
$this->assertStringContainsString('<coverage', file_get_contents($target));
130+
}
131+
132+
public function testRenderCrap4j(): void
133+
{
134+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
135+
$target = TEST_FILES_PATH . 'tmp' . DIRECTORY_SEPARATOR . 'FacadeTest_crap4j.xml';
136+
137+
$facade->renderCrap4j($target);
138+
139+
$this->assertFileExists($target);
140+
$this->assertStringContainsString('<?xml', file_get_contents($target));
141+
$this->assertStringContainsString('crap_result', file_get_contents($target));
142+
}
143+
144+
public function testRenderHtml(): void
145+
{
146+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
147+
$target = TEST_FILES_PATH . 'tmp';
148+
149+
$facade->renderHtml($target);
150+
151+
$this->assertFileExists($target . DIRECTORY_SEPARATOR . 'index.html');
152+
$this->assertDirectoryExists($target);
153+
}
154+
155+
public function testRenderXml(): void
156+
{
157+
$facade = Facade::fromObject($this->getLineCoverageForBankAccount());
158+
$target = TEST_FILES_PATH . 'tmp';
159+
160+
$facade->renderXml($target);
161+
162+
$this->assertFileExists($target . DIRECTORY_SEPARATOR . 'index.xml');
163+
$this->assertDirectoryExists($target);
164+
}
165+
}

0 commit comments

Comments
 (0)