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\Xml;
12
13use SebastianBergmann\CodeCoverage\TestCase;
14
15class XMLTest 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 . 'XML';
24    }
25
26    protected function tearDown()
27    {
28        parent::tearDown();
29
30        $tmpFilesIterator = new \FilesystemIterator(self::$TEST_TMP_PATH);
31
32        foreach ($tmpFilesIterator as $path => $fileInfo) {
33            /* @var \SplFileInfo $fileInfo */
34            unlink($fileInfo->getPathname());
35        }
36    }
37
38    public function testForBankAccountTest()
39    {
40        $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount';
41
42        $xml = new Facade;
43        $xml->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH);
44
45        $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
46    }
47
48    public function testForFileWithIgnoredLines()
49    {
50        $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines';
51
52        $xml = new Facade;
53        $xml->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH);
54
55        $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
56    }
57
58    public function testForClassWithAnonymousFunction()
59    {
60        $expectedFilesPath =
61            self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForClassWithAnonymousFunction';
62
63        $xml = new Facade;
64        $xml->process($this->getCoverageForClassWithAnonymousFunction(), self::$TEST_TMP_PATH);
65
66        $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
67    }
68
69    /**
70     * @param string $expectedFilesPath
71     * @param string $actualFilesPath
72     */
73    private function assertFilesEquals($expectedFilesPath, $actualFilesPath)
74    {
75        $expectedFilesIterator = new \FilesystemIterator($expectedFilesPath);
76        $actualFilesIterator   = new \FilesystemIterator($actualFilesPath);
77
78        $this->assertEquals(
79            iterator_count($expectedFilesIterator),
80            iterator_count($actualFilesIterator),
81            'Generated files and expected files not match'
82        );
83
84        foreach ($expectedFilesIterator as $path => $fileInfo) {
85            /* @var \SplFileInfo $fileInfo */
86            $filename = $fileInfo->getFilename();
87
88            $actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename;
89
90            $this->assertFileExists($actualFile);
91
92            $this->assertStringMatchesFormatFile(
93                $fileInfo->getPathname(),
94                file_get_contents($actualFile),
95                "${filename} not match"
96            );
97        }
98    }
99}
100