|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the PHP_CodeCoverage package. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +if (!defined('TEST_FILES_PATH')) { |
| 12 | + define( |
| 13 | + 'TEST_FILES_PATH', |
| 14 | + dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . |
| 15 | + '_files' . DIRECTORY_SEPARATOR |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +require_once TEST_FILES_PATH . '../TestCase.php'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests for the PHP_CodeCoverage_Report_XML class. |
| 23 | + * |
| 24 | + * @since Class available since Release 3.0.2 |
| 25 | + */ |
| 26 | +class PHP_CodeCoverage_Report_XMLTest extends PHP_CodeCoverage_TestCase |
| 27 | +{ |
| 28 | + static private $TEST_REPORT_PATH_SOURCE; |
| 29 | + |
| 30 | + public static function setUpBeforeClass() |
| 31 | + { |
| 32 | + parent::setUpBeforeClass(); |
| 33 | + |
| 34 | + self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'XML'; |
| 35 | + } |
| 36 | + |
| 37 | + protected function tearDown() |
| 38 | + { |
| 39 | + parent::tearDown(); |
| 40 | + |
| 41 | + $tmpFilesIterator = new FilesystemIterator(self::$TEST_TMP_PATH); |
| 42 | + foreach ($tmpFilesIterator as $path => $fileInfo) { |
| 43 | + /** @var SplFileInfo $fileInfo */ |
| 44 | + unlink($fileInfo->getPathname()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @covers PHP_CodeCoverage_Report_XML |
| 50 | + */ |
| 51 | + public function testXmlForBankAccountTest() |
| 52 | + { |
| 53 | + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount'; |
| 54 | + |
| 55 | + $xml = new PHP_CodeCoverage_Report_XML; |
| 56 | + $xml->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH); |
| 57 | + |
| 58 | + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @covers PHP_CodeCoverage_Report_XML |
| 63 | + */ |
| 64 | + public function testXmlForFileWithIgnoredLines() |
| 65 | + { |
| 66 | + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines'; |
| 67 | + |
| 68 | + $xml = new PHP_CodeCoverage_Report_XML; |
| 69 | + $xml->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH); |
| 70 | + |
| 71 | + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @covers PHP_CodeCoverage_Report_XML |
| 76 | + */ |
| 77 | + public function testXmlForClassWithAnonymousFunction() |
| 78 | + { |
| 79 | + $expectedFilesPath = |
| 80 | + self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForClassWithAnonymousFunction'; |
| 81 | + |
| 82 | + $xml = new PHP_CodeCoverage_Report_XML; |
| 83 | + $xml->process($this->getCoverageForClassWithAnonymousFunction(), self::$TEST_TMP_PATH); |
| 84 | + |
| 85 | + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param string $expectedFilesPath |
| 90 | + * @param string $actualFilesPath |
| 91 | + */ |
| 92 | + protected function assertFilesEquals($expectedFilesPath, $actualFilesPath) |
| 93 | + { |
| 94 | + $expectedFilesIterator = new FilesystemIterator($expectedFilesPath); |
| 95 | + $actualFilesIterator = new FilesystemIterator($actualFilesPath); |
| 96 | + |
| 97 | + $this->assertEquals( |
| 98 | + iterator_count($expectedFilesIterator), |
| 99 | + iterator_count($actualFilesIterator), |
| 100 | + 'Generated files and expected files not match' |
| 101 | + ); |
| 102 | + foreach ($expectedFilesIterator as $path => $fileInfo) { |
| 103 | + /** @var SplFileInfo $fileInfo */ |
| 104 | + $filename = $fileInfo->getFilename(); |
| 105 | + |
| 106 | + $actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename; |
| 107 | + |
| 108 | + $this->assertFileExists($actualFile); |
| 109 | + $this->assertStringMatchesFormatFile( |
| 110 | + $fileInfo->getPathname(), |
| 111 | + file_get_contents($actualFile), |
| 112 | + "${filename} not match" |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments