|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tests\Unit\Coverage; |
|
6
|
|
|
|
|
7
|
|
|
use Paraunit\Configuration\TempFilenameFactory; |
|
8
|
|
|
use Paraunit\Coverage\CoverageFetcher; |
|
9
|
|
|
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface; |
|
10
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage; |
|
11
|
|
|
use Tests\BaseUnitTestCase; |
|
12
|
|
|
use Tests\Stub\StubbedParaunitProcess; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class CoverageFetcherTest |
|
16
|
|
|
* @package Tests\Unit\Coverage |
|
17
|
|
|
*/ |
|
18
|
|
|
class CoverageFetcherTest extends BaseUnitTestCase |
|
19
|
|
|
{ |
|
20
|
|
View Code Duplication |
public function testFetch() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$process = new StubbedParaunitProcess('test.php', 'uniqueId'); |
|
23
|
|
|
|
|
24
|
|
|
$filename = $this->getTempFilename(); |
|
25
|
|
|
copy($this->getCoverageStubFilePath(), $filename); |
|
26
|
|
|
$this->assertFileExists($filename, 'Test malformed, stub log file not found'); |
|
27
|
|
|
|
|
28
|
|
|
$tempFilenameFactory = $this->prophesize(TempFilenameFactory::class); |
|
29
|
|
|
$tempFilenameFactory->getFilenameForCoverage('uniqueId') |
|
30
|
|
|
->shouldBeCalled() |
|
31
|
|
|
->willReturn($filename); |
|
32
|
|
|
$missingCoverageContainer = $this->prophesize(TestResultHandlerInterface::class); |
|
33
|
|
|
$missingCoverageContainer->addProcessToFilenames($process) |
|
34
|
|
|
->shouldNotBeCalled(); |
|
35
|
|
|
|
|
36
|
|
|
$fetcher = new CoverageFetcher($tempFilenameFactory->reveal(), $missingCoverageContainer->reveal()); |
|
37
|
|
|
|
|
38
|
|
|
$result = $fetcher->fetch($process); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertInstanceOf(CodeCoverage::class, $result); |
|
41
|
|
|
$this->assertNotEmpty($result->getData()); |
|
42
|
|
|
$this->assertFileNotExists($filename, 'Coverage file should be deleted to preserve memory'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testFetchIgnoresMissingCoverageFiles() |
|
46
|
|
|
{ |
|
47
|
|
|
$process = new StubbedParaunitProcess('test.php', 'uniqueId'); |
|
48
|
|
|
|
|
49
|
|
|
$tempFilenameFactory = $this->prophesize(TempFilenameFactory::class); |
|
50
|
|
|
$tempFilenameFactory->getFilenameForCoverage('uniqueId') |
|
51
|
|
|
->shouldBeCalled() |
|
52
|
|
|
->willReturn('/path/to/missing/file'); |
|
53
|
|
|
$missingCoverageContainer = $this->prophesize(TestResultHandlerInterface::class); |
|
54
|
|
|
$missingCoverageContainer->addProcessToFilenames($process) |
|
55
|
|
|
->shouldBeCalled(); |
|
56
|
|
|
|
|
57
|
|
|
$fetcher = new CoverageFetcher($tempFilenameFactory->reveal(), $missingCoverageContainer->reveal()); |
|
58
|
|
|
|
|
59
|
|
|
$result = $fetcher->fetch($process); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertInstanceOf(CodeCoverage::class, $result); |
|
62
|
|
|
$this->assertEmpty($result->getData()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
public function testFetchIgnoresWrongFiles() |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$process = new StubbedParaunitProcess('test.php', 'uniqueId'); |
|
68
|
|
|
|
|
69
|
|
|
$filename = $this->getTempFilename(); |
|
70
|
|
|
copy($this->getWrongCoverageStubFilePath(), $filename); |
|
71
|
|
|
$this->assertFileExists($filename, 'Test malformed, stub log file not found'); |
|
72
|
|
|
|
|
73
|
|
|
$tempFilenameFactory = $this->prophesize(TempFilenameFactory::class); |
|
74
|
|
|
$tempFilenameFactory->getFilenameForCoverage('uniqueId') |
|
75
|
|
|
->shouldBeCalled() |
|
76
|
|
|
->willReturn($filename); |
|
77
|
|
|
$missingCoverageContainer = $this->prophesize(TestResultHandlerInterface::class); |
|
78
|
|
|
$missingCoverageContainer->addProcessToFilenames($process) |
|
79
|
|
|
->shouldBeCalled(); |
|
80
|
|
|
|
|
81
|
|
|
$fetcher = new CoverageFetcher($tempFilenameFactory->reveal(), $missingCoverageContainer->reveal()); |
|
82
|
|
|
|
|
83
|
|
|
$result = $fetcher->fetch($process); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertInstanceOf(CodeCoverage::class, $result); |
|
86
|
|
|
$this->assertEmpty($result->getData()); |
|
87
|
|
|
$this->assertFileNotExists($filename, 'Coverage file should be deleted to preserve memory'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function getTempFilename(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return uniqid(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'testfile', true) . '.php'; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.