1<?php
2/*
3 * This file is part of the php-code-coverage package.
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
11namespace SebastianBergmann\CodeCoverage\Report\Html;
12
13use SebastianBergmann\CodeCoverage\TestCase;
14
15class HTMLTest extends TestCase
16{
17    private static $TEST_REPORT_PATH_SOURCE;
18
19    public static function setUpBeforeClass()
20    {
21        parent::setUpBeforeClass();
22
23        self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'HTML';
24    }
25
26    protected function tearDown()
27    {
28        parent::tearDown();
29
30        $tmpFilesIterator = new \RecursiveIteratorIterator(
31            new \RecursiveDirectoryIterator(self::$TEST_TMP_PATH, \RecursiveDirectoryIterator::SKIP_DOTS),
32            \RecursiveIteratorIterator::CHILD_FIRST
33        );
34
35        foreach ($tmpFilesIterator as $path => $fileInfo) {
36            /* @var \SplFileInfo $fileInfo */
37            $pathname = $fileInfo->getPathname();
38            $fileInfo->isDir() ? rmdir($pathname) : unlink($pathname);
39        }
40    }
41
42    public function testForBankAccountTest()
43    {
44        $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount';
45
46        $report = new Facade;
47        $report->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH);
48
49        $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
50    }
51
52    public function testForFileWithIgnoredLines()
53    {
54        $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines';
55
56        $report = new Facade;
57        $report->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH);
58
59        $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
60    }
61
62    public function testForClassWithAnonymousFunction()
63    {
64        $expectedFilesPath =
65            self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForClassWithAnonymousFunction';
66
67        $report = new Facade;
68        $report->process($this->getCoverageForClassWithAnonymousFunction(), self::$TEST_TMP_PATH);
69
70        $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
71    }
72
73    /**
74     * @param string $expectedFilesPath
75     * @param string $actualFilesPath
76     */
77    private function assertFilesEquals($expectedFilesPath, $actualFilesPath)
78    {
79        $expectedFilesIterator = new \FilesystemIterator($expectedFilesPath);
80        $actualFilesIterator   = new \RegexIterator(new \FilesystemIterator($actualFilesPath), '/.html/');
81
82        $this->assertEquals(
83            iterator_count($expectedFilesIterator),
84            iterator_count($actualFilesIterator),
85            'Generated files and expected files not match'
86        );
87
88        foreach ($expectedFilesIterator as $path => $fileInfo) {
89            /* @var \SplFileInfo $fileInfo */
90            $filename = $fileInfo->getFilename();
91
92            $actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename;
93
94            $this->assertFileExists($actualFile);
95
96            $this->assertStringMatchesFormatFile(
97                $fileInfo->getPathname(),
98                str_replace(PHP_EOL, "\n", file_get_contents($actualFile)),
99                "${filename} not match"
100            );
101        }
102    }
103}
104