|
| 1 | +<?php |
| 2 | +namespace Codeception\PHPUnit\Log; |
| 3 | + |
| 4 | +use Codeception\Configuration; |
| 5 | +use Codeception\Test\Interfaces\Reported; |
| 6 | +use Codeception\Test\Test; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use PHPUnit\Framework\TestSuite; |
| 9 | + |
| 10 | +class PhpUnit extends \PHPUnit\Util\Log\JUnit |
| 11 | +{ |
| 12 | + const SUITE_LEVEL = 1; |
| 13 | + const FILE_LEVEL = 2; |
| 14 | + |
| 15 | + protected $strictAttributes = ['file', 'name', 'class']; |
| 16 | + |
| 17 | + private $currentFile; |
| 18 | + private $currentFileSuite; |
| 19 | + |
| 20 | + public function startTest(\PHPUnit\Framework\Test $test) |
| 21 | + { |
| 22 | + if (method_exists($test, 'getFileName') ) { |
| 23 | + $filename = $test->getFileName(); |
| 24 | + } else { |
| 25 | + $reflector = new \ReflectionClass($test); |
| 26 | + $filename = $reflector->getFileName(); |
| 27 | + } |
| 28 | + |
| 29 | + if ($filename !== $this->currentFile) { |
| 30 | + if ($this->currentFile !== null) { |
| 31 | + parent::endTestSuite(new TestSuite()); |
| 32 | + } |
| 33 | + |
| 34 | + //initialize all values to avoid warnings |
| 35 | + $this->testSuiteAssertions[self::FILE_LEVEL] = 0; |
| 36 | + $this->testSuiteTests[self::FILE_LEVEL] = 0; |
| 37 | + $this->testSuiteTimes[self::FILE_LEVEL] = 0; |
| 38 | + $this->testSuiteErrors[self::FILE_LEVEL] = 0; |
| 39 | + $this->testSuiteFailures[self::FILE_LEVEL] = 0; |
| 40 | + $this->testSuiteSkipped[self::FILE_LEVEL] = 0; |
| 41 | + |
| 42 | + $this->testSuiteLevel = self::FILE_LEVEL; |
| 43 | + |
| 44 | + $this->currentFile = $filename; |
| 45 | + $this->currentFileSuite = $this->document->createElement('testsuite'); |
| 46 | + $this->currentFileSuite->setAttribute('file', $filename); |
| 47 | + |
| 48 | + $this->testSuites[self::SUITE_LEVEL]->appendChild($this->currentFileSuite); |
| 49 | + $this->testSuites[self::FILE_LEVEL] = $this->currentFileSuite; |
| 50 | + } |
| 51 | + |
| 52 | + if (!$test instanceof Reported) { |
| 53 | + parent::startTest($test); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $this->currentTestCase = $this->document->createElement('testcase'); |
| 58 | + |
| 59 | + $isStrict = Configuration::config()['settings']['strict_xml']; |
| 60 | + |
| 61 | + foreach ($test->getReportFields() as $attr => $value) { |
| 62 | + if ($isStrict and !in_array($attr, $this->strictAttributes)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + $this->currentTestCase->setAttribute($attr, $value); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public function endTest(\PHPUnit\Framework\Test $test, $time) |
| 70 | + { |
| 71 | + if ($this->currentTestCase !== null && $test instanceof Test) { |
| 72 | + $numAssertions = $test->getNumAssertions(); |
| 73 | + $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions; |
| 74 | + |
| 75 | + $this->currentTestCase->setAttribute( |
| 76 | + 'assertions', |
| 77 | + $numAssertions |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + if ($test instanceof TestCase) { |
| 82 | + parent::endTest($test, $time); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // In PhpUnit 7.4.*, parent::endTest ignores tests that aren't instances of TestCase |
| 87 | + // so I copied this code from PhpUnit 7.3.5 |
| 88 | + |
| 89 | + $this->currentTestCase->setAttribute( |
| 90 | + 'time', |
| 91 | + \sprintf('%F', $time) |
| 92 | + ); |
| 93 | + $this->testSuites[$this->testSuiteLevel]->appendChild( |
| 94 | + $this->currentTestCase |
| 95 | + ); |
| 96 | + $this->testSuiteTests[$this->testSuiteLevel]++; |
| 97 | + $this->testSuiteTimes[$this->testSuiteLevel] += $time; |
| 98 | + $this->currentTestCase = null; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Cleans the mess caused by test suite manipulation in startTest |
| 103 | + */ |
| 104 | + public function endTestSuite(TestSuite $suite) |
| 105 | + { |
| 106 | + if ($suite->getName()) { |
| 107 | + if ($this->currentFile) { |
| 108 | + //close last file in the test suite |
| 109 | + parent::endTestSuite(new TestSuite()); |
| 110 | + $this->currentFile = null; |
| 111 | + } |
| 112 | + $this->testSuiteLevel = self::SUITE_LEVEL; |
| 113 | + } |
| 114 | + parent::endTestSuite($suite); |
| 115 | + } |
| 116 | +} |
0 commit comments