From 533cc21db6de35fa760b3fe7ec30d6d268ac115f Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Wed, 5 Sep 2018 11:51:21 +0200 Subject: [PATCH] Closes #3270 --- ChangeLog-7.3.md | 7 ++++++ src/Util/TestDox/NamePrettifier.php | 23 ++++++++++++++++++- tests/TextUI/dataprovider-testdox.phpt | 11 ++++++++- tests/_files/DataProviderTestDoxTest.php | 29 ++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/ChangeLog-7.3.md b/ChangeLog-7.3.md index ce2efcccf2e..797663ad966 100644 --- a/ChangeLog-7.3.md +++ b/ChangeLog-7.3.md @@ -2,6 +2,12 @@ All notable changes of the PHPUnit 7.3 release series are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## [7.3.4] - 2018-MM-DD + +### Fixed + +* Fixed [#3270](https://github.com/sebastianbergmann/phpunit/issues/3270): Array / Object to string conversion in `NamePrettifier` + ## [7.3.3] - 2018-09-01 ### Fixed @@ -52,6 +58,7 @@ All notable changes of the PHPUnit 7.3 release series are documented in this fil * Fixed [#3222](https://github.com/sebastianbergmann/phpunit/pull/3222): Priority of `@covers` and `@coversNothing` is wrong * Fixed [#3225](https://github.com/sebastianbergmann/phpunit/issues/3225): `coverage-php` missing from `phpunit.xsd` +[7.3.4]: https://github.com/sebastianbergmann/phpunit/compare/7.3.3...7.3.4 [7.3.3]: https://github.com/sebastianbergmann/phpunit/compare/7.3.2...7.3.3 [7.3.2]: https://github.com/sebastianbergmann/phpunit/compare/7.3.1...7.3.2 [7.3.1]: https://github.com/sebastianbergmann/phpunit/compare/7.3.0...7.3.1 diff --git a/src/Util/TestDox/NamePrettifier.php b/src/Util/TestDox/NamePrettifier.php index 611498ba2ca..3cf85af02bc 100644 --- a/src/Util/TestDox/NamePrettifier.php +++ b/src/Util/TestDox/NamePrettifier.php @@ -10,6 +10,7 @@ namespace PHPUnit\Util\TestDox; use PHPUnit\Framework\TestCase; +use SebastianBergmann\Exporter\Exporter; /** * Prettifies class and method names for use in TestDox documentation. @@ -161,7 +162,27 @@ private function mapTestMethodParameterNamesToProvidedDataValues(TestCase $test) $i = 0; foreach ($reflector->getParameters() as $parameter) { - $providedData['$' . $parameter->getName()] = $providedDataValues[$i++]; + $value = $providedDataValues[$i++]; + + if (\is_object($value)) { + $reflector = new \ReflectionObject($value); + + if ($reflector->hasMethod('__toString')) { + $value = (string) $value; + } + } + + if (!\is_scalar($value)) { + $value = \gettype($value); + } + + if (\is_bool($value) || \is_numeric($value)) { + $exporter = new Exporter; + + $value = $exporter->export($value); + } + + $providedData['$' . $parameter->getName()] = $value; } return $providedData; diff --git a/tests/TextUI/dataprovider-testdox.phpt b/tests/TextUI/dataprovider-testdox.phpt index 125d1745f7a..6ebf02f87a9 100644 --- a/tests/TextUI/dataprovider-testdox.phpt +++ b/tests/TextUI/dataprovider-testdox.phpt @@ -17,7 +17,16 @@ DataProviderTestDox ✔ Does something with data set "two" ✔ Does something else with data set "one" ✔ Does something else with data set "two" + ✔ ... true ... + ✔ ... 1 ... + ✔ ... 1.0 ... + ✔ ... string ... + ✔ ... array ... + ✔ ... object ... + ✔ ... string ... + ✔ ... resource ... + ✔ ... NULL ... Time: %s, Memory: %s -OK (4 tests, 4 assertions) +OK (13 tests, 13 assertions) diff --git a/tests/_files/DataProviderTestDoxTest.php b/tests/_files/DataProviderTestDoxTest.php index e03209408a9..9bb9bf69209 100644 --- a/tests/_files/DataProviderTestDoxTest.php +++ b/tests/_files/DataProviderTestDoxTest.php @@ -28,6 +28,15 @@ public function testDoesSomethingElseWith(): void $this->assertTrue(true); } + /** + * @dataProvider placeHolderprovider + * @testdox ... $value ... + */ + public function testWithPlaceholders($value): void + { + $this->assertTrue(true); + } + public function provider() { return [ @@ -35,4 +44,24 @@ public function provider() 'two' => [2] ]; } + + public function placeHolderprovider(): array + { + return [ + 'boolean' => [true], + 'integer' => [1], + 'float' => [1.0], + 'string' => ['string'], + 'array' => [[1, 2, 3]], + 'object' => [new \stdClass], + 'stringableObject' => [new class { + public function __toString() + { + return 'string'; + } + }], + 'resource' => [\fopen(__FILE__, 'rb')], + 'null' => [null] + ]; + } }