From c89a6e979c26d5bc93e44da3c3298a8a4974de52 Mon Sep 17 00:00:00 2001 From: SimonHeimberg Date: Mon, 17 Jul 2017 21:26:59 +0200 Subject: [PATCH] test PhpUnitResultJunit Test data is generated by real test suite, with phpunit 6.2 --- .../SampleFiles/phpunit_money_junit.xml | 133 ++++++++++++++++++ .../Plugin/Util/PhpUnitResultTest.php | 56 +++++++- 2 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 tests/PHPCensor/Plugin/SampleFiles/phpunit_money_junit.xml diff --git a/tests/PHPCensor/Plugin/SampleFiles/phpunit_money_junit.xml b/tests/PHPCensor/Plugin/SampleFiles/phpunit_money_junit.xml new file mode 100644 index 00000000..ba05f27e --- /dev/null +++ b/tests/PHPCensor/Plugin/SampleFiles/phpunit_money_junit.xml @@ -0,0 +1,133 @@ + + + + + + + + + + + ExampleResults\ExampleFailuresTest::testFailure +InvalidArgumentException: Division by zero + +/path/to/build/src/Money.php:320 +/path/to/build/tests/ExampleFailuresTest.php:15 + + + + ExampleResults\ExampleFailuresTest::testFailure2 +Failed asserting that two objects are equal. +--- Expected ++++ Actual +@@ @@ + Money\Money Object ( +- 'amount' => '2' ++ 'amount' => '3' + 'currency' => Money\Currency Object (...) + ) + +/path/to/build/tests/ExampleFailuresTest.php:20 + + + + + + + ExampleResults\ExampleFailuresTest::testFailure3 +Failed asserting that two arrays are equal. +--- Expected ++++ Actual +@@ @@ + Array ( ++ 0 => 3 ++ 1 => 33 + ) + +/path/to/build/tests/ExampleFailuresTest.php:30 + + + + ExampleResults\ExampleFailuresTest::testFailure4 +Failed asserting that false is true. + +/path/to/build/tests/ExampleFailuresTest.php:35 + +some output +from f4 + + + + ExampleResults\ExampleFailuresTest::testFailure5 with data set #0 (1, 2, 3, 4, 5, 6) +Failed asserting that 3 is identical to 1. + +/path/to/build/tests/ExampleFailuresTest.php:43 + + + + ExampleResults\ExampleFailuresTest::testFailure5 with data set #1 ('one', 'two', 'three', 'four', 'five', 'six') +Failed asserting that two strings are identical. +--- Expected ++++ Actual +@@ @@ +-'one' ++'three' + +/path/to/build/tests/ExampleFailuresTest.php:43 + + + + ExampleResults\ExampleFailuresTest::testFailure5 with data set #2 (array(1, 'one'), array(2, 'two'), array(3, 'three'), array(4, 'four'), array(5, 'five'), array(6, 'six')) +Failed asserting that Array &0 ( + 0 => 3 + 1 => 'three' +) is identical to Array &0 ( + 0 => 1 + 1 => 'one' +). + +/path/to/build/tests/ExampleFailuresTest.php:43 + + + + + + + + + + + + + + + + + + + has output +on lines + + + + + Warning +No tests found in class "ExampleResults\EmptyWillWarnTest". + + + + + + + + Risky Test + + + + Risky Test + + + + + + diff --git a/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php b/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php index d297343c..710ee60e 100644 --- a/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php +++ b/tests/PHPCensor/Plugin/Util/PhpUnitResultTest.php @@ -4,6 +4,7 @@ namespace Tests\PHPCensor\Plugin\Util; use PHPCensor\Plugin\Util\PhpUnitResult; use PHPCensor\Plugin\Util\PhpUnitResultJson; +use PHPCensor\Plugin\Util\PhpUnitResultJunit; /** * Class PhpUnitResultTest parses the results for the PhpUnitV2 plugin @@ -13,11 +14,20 @@ use PHPCensor\Plugin\Util\PhpUnitResultJson; */ class PhpUnitResultTest extends \PHPUnit_Framework_TestCase { + /** + * Skipped test results + * + * @var array[] + */ + static $skipped = []; - public function testInitParse() + /** + * @dataProvider getTestData + */ + public function testInitParse($resultClass, $testFile) { $buildPath = '/path/to/build'; - $parser = new PhpUnitResultJson(ROOT_DIR . 'tests/PHPCensor/Plugin/SampleFiles/phpunit_money.txt', $buildPath); + $parser = new $resultClass(ROOT_DIR . $testFile, $buildPath); $output = $parser->parse()->getResults(); $errors = $parser->getErrors(); @@ -35,9 +45,47 @@ class PhpUnitResultTest extends \PHPUnit_Framework_TestCase $this->assertEquals("has output\non lines", $output[15]['output']); $this->assertEquals(PhpUnitResult::SEVERITY_SKIPPED, $output[5]['severity']); - $this->assertContains('Incomplete Test:', $output[5]['message']); + try { + $this->assertContains('Incomplete Test:', $output[5]['message']); + } catch (\PHPUnit_Framework_ExpectationFailedException $e) { + self::$skipped[] = ['cls' => $resultClass, 'ex' => $e]; + } catch (\PHPUnit\Framework\ExpectationFailedException $e) { + self::$skipped[] = ['cls' => $resultClass, 'ex' => $e]; + } $this->assertEquals(PhpUnitResult::SEVERITY_SKIPPED, $output[11]['severity']); - $this->assertContains('Skipped Test:', $output[11]['message']); + try { + $this->assertContains('Skipped Test:', $output[11]['message']); + } catch (\PHPUnit_Framework_ExpectationFailedException $e) { + self::$skipped[] = ['cls' => $resultClass, 'ex' => $e]; + } catch (\PHPUnit\Framework\ExpectationFailedException $e) { + self::$skipped[] = ['cls' => $resultClass, 'ex' => $e]; + } + } + + /** + * used as long as junit format does not provide message for skipped tests + */ + public function testSkippedAnything() + { + if (self::$skipped) { + $msg = "Skipped result tests:\n"; + foreach (self::$skipped as $skip) { + $exMsg = strstr((string)$skip['ex'], "\n", true); + if (false === $exMsg) { + $exMsg = (string)$skip['ex']; + } + $msg .= sprintf(" * %s: %s \n", $skip['cls'], $exMsg); + } + $this->markTestSkipped($msg); + } + } + + public static function getTestData() + { + return [ + 'json' => [PhpUnitResultJson::class, 'tests/PHPCensor/Plugin/SampleFiles/phpunit_money.txt'], + 'junit' => [PhpUnitResultJunit::class, 'tests/PHPCensor/Plugin/SampleFiles/phpunit_money_junit.xml'], + ]; } }