From eeb41228033aa8a17da10f61b6c2bf271fc02bb4 Mon Sep 17 00:00:00 2001 From: Ewout Pieter den Ouden Date: Thu, 31 May 2018 23:29:00 +0200 Subject: [PATCH] Improve test coverage of Configuration - test a list of root config options not covered elsewhere - test validation errors reusing existing 'invalid colors' test - indirectly test integer default value of attribute via 'columns' - add 'disableCodeCoverageIgnore' found in missing coverage to schema --- phpunit.xsd | 1 + src/Util/Configuration.php | 1 + tests/Util/ConfigurationTest.php | 61 +++++++++++++++++++ .../_files/configuration.columns.default.xml | 1 + 4 files changed, 64 insertions(+) create mode 100644 tests/_files/configuration.columns.default.xml diff --git a/phpunit.xsd b/phpunit.xsd index 67f4bf5ee47..5a9cb52227c 100644 --- a/phpunit.xsd +++ b/phpunit.xsd @@ -223,6 +223,7 @@ + diff --git a/src/Util/Configuration.php b/src/Util/Configuration.php index 656da9545f8..21542b654a6 100644 --- a/src/Util/Configuration.php +++ b/src/Util/Configuration.php @@ -35,6 +35,7 @@ * convertErrorsToExceptions="true" * convertNoticesToExceptions="true" * convertWarningsToExceptions="true" + * disableCodeCoverageIgnore="false" * forceCoversAnnotation="false" * processIsolation="false" * stopOnError="false" diff --git a/tests/Util/ConfigurationTest.php b/tests/Util/ConfigurationTest.php index ee61fa8e944..5d95d126b0d 100644 --- a/tests/Util/ConfigurationTest.php +++ b/tests/Util/ConfigurationTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\TestSuiteSorter; use PHPUnit\TextUI\ResultPrinter; class ConfigurationTest extends TestCase @@ -71,6 +72,66 @@ public function testShouldReadColorsWhenInvalidInConfigurationFile(): void $this->assertEquals(ResultPrinter::COLOR_NEVER, $configurationValues['colors']); } + public function testInvalidConfigurationGeneratesValidationErrors(): void + { + $configurationFilename = \dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'configuration.colors.invalid.xml'; + $configurationInstance = Configuration::getInstance($configurationFilename); + + $this->assertTrue($configurationInstance->hasValidationErrors()); + $this->assertArraySubset([1 => ["Element 'phpunit', attribute 'colors': 'Something else' is not a valid value of the atomic type 'xs:boolean'."]], $configurationInstance->getValidationErrors()); + } + + public function testNonIntegerValueReturnsDefault(): void + { + $configurationFilename = \dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'configuration.columns.default.xml'; + $configurationInstance = Configuration::getInstance($configurationFilename); + $configurationValues = $configurationInstance->getPHPUnitConfiguration(); + + $this->assertEquals(80, $configurationValues['columns']); + } + + /** + * @dataProvider configurationRootOptionsProvider + * + * @param bool|int|string $expected + */ + public function testConfigurationRootOptions(string $optionName, string $optionValue, $expected): void + { + $tmpFilename = \sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phpunit.' . $optionName . \uniqid() . '.xml'; + $xml = "" . PHP_EOL; + \file_put_contents($tmpFilename, $xml); + + $configurationInstance = Configuration::getInstance($tmpFilename); + $this->assertFalse($configurationInstance->hasValidationErrors(), 'option causes validation error'); + + $configurationValues = $configurationInstance->getPHPUnitConfiguration(); + $this->assertEquals($expected, $configurationValues[$optionName]); + + @\unlink($tmpFilename); + } + + public function configurationRootOptionsProvider(): array + { + return [ + ['executionOrder', 'default', TestSuiteSorter::ORDER_DEFAULT], + ['executionOrder', 'random', TestSuiteSorter::ORDER_RANDOMIZED], + ['executionOrder', 'reverse', TestSuiteSorter::ORDER_REVERSED], + ['columns', 'max', 'max'], + ['stopOnFailure', 'true', true], + ['stopOnWarning', 'true', true], + ['stopOnIncomplete', 'true', true], + ['stopOnRisky', 'true', true], + ['stopOnSkipped', 'true', true], + ['failOnWarning', 'true', true], + ['failOnRisky', 'true', true], + ['disableCodeCoverageIgnore', 'true', true], + ['processIsolation', 'true', true], + ['testSuiteLoaderFile', '/path/to/file', '/path/to/file'], + ['reverseDefectList', 'true', true], + ['registerMockObjectsFromTestArgumentsRecursively', 'true', true], + ]; + } + public function testFilterConfigurationIsReadCorrectly(): void { $this->assertEquals( diff --git a/tests/_files/configuration.columns.default.xml b/tests/_files/configuration.columns.default.xml new file mode 100644 index 00000000000..c2cb278736c --- /dev/null +++ b/tests/_files/configuration.columns.default.xml @@ -0,0 +1 @@ +