From ac1bfe6a4569c4c2664873d7e4755b3af4a0be53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 12 Jun 2017 16:39:22 +0200 Subject: [PATCH 01/10] [PhpUnitBridge] Added a CoverageListener to enhance the code coverage report --- .../Bridge/PhpUnit/CoverageListener.php | 44 ++++++++++ .../PhpUnit/Legacy/CoverageListener.php | 35 ++++++++ .../PhpUnit/Legacy/CoverageListenerTrait.php | 81 +++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 src/Symfony/Bridge/PhpUnit/CoverageListener.php create mode 100644 src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php create mode 100644 src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php diff --git a/src/Symfony/Bridge/PhpUnit/CoverageListener.php b/src/Symfony/Bridge/PhpUnit/CoverageListener.php new file mode 100644 index 0000000000000..ffd8b74911a36 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/CoverageListener.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\PhpUnit; + +use PHPUnit\Framework\BaseTestListener; +use PHPUnit\Framework\Test; +use Symfony\Bridge\PhpUnit\Legacy\CoverageListenerTrait; + +if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { + class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListener', 'Symfony\Bridge\PhpUnit\CoverageListener'); +// Using an early return instead of a else does not work when using the PHPUnit +// phar due to some weird PHP behavior (the class gets defined without executing +// the code before it and so the definition is not properly conditional) +} else { + /** + * CoverageListener adds `@covers ` on each test suite when possible + * to make the code coverage more accurate. + * + * @author Grégoire Pineau + */ + class CoverageListener extends BaseTestListener + { + private $trait; + + public function __construct(callable $sutFqcnResolver = null) + { + $this->trait = new CoverageListenerTrait($sutFqcnResolver); + } + + public function startTest(Test $test) + { + $this->trait->startTest($test); + } + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php new file mode 100644 index 0000000000000..38944e98bf3f4 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\PhpUnit\Legacy; + +/** + * CoverageListener adds `@covers ` on each test suite when possible + * to make the code coverage more accurate. + * + * @author Grégoire Pineau + * + * @internal + */ +class CoverageListener extends \PHPUnit_Framework_BaseTestListener +{ + private $trait; + + public function __construct(callable $sutFqcnResolver = null) + { + $this->trait = new CoverageListenerTrait($sutFqcnResolver); + } + + public function startTest(\PHPUnit_Framework_Test $test) + { + $this->trait->startTest($test); + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php new file mode 100644 index 0000000000000..5aded4a77c78d --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\PhpUnit\Legacy; + +use PHPUnit\Framework\Test; + +/** + * PHP 5.3 compatible trait-like shared implementation. + * + * @author Grégoire Pineau + * + * @internal + */ +class CoverageListenerTrait +{ + private $sutFqcnResolver; + + public function __construct(callable $sutFqcnResolver = null) + { + $this->sutFqcnResolver = $sutFqcnResolver; + } + + public function startTest($test) + { + $annotations = $test->getAnnotations(); + + if (isset($annotations['class']['covers']) || isset($annotations['method']['covers'])) { + return; + } + + $sutFqcn = $this->findSutFqcn($test); + if (!$sutFqcn) { + return; + } + + $testClass = \PHPUnit\Util\Test::class; + if (!class_exists($testClass, false)) { + $testClass = \PHPUnit_Util_Test::class; + } + + $r = new \ReflectionProperty($testClass, 'annotationCache'); + $r->setAccessible(true); + + $cache = $r->getValue(); + $cache = array_replace_recursive($cache, array( + get_class($test) => array( + 'covers' => array($sutFqcn), + ), + )); + $r->setValue($testClass, $cache); + } + + private function findSutFqcn($test) + { + if ($this->sutFqcnResolver) { + $resolver = $this->sutFqcnResolver; + + return $resolver($test); + } + + $class = get_class($test); + + $sutFqcn = str_replace('Tests\\', '', $class); + $sutFqcn = preg_replace('{Test$}', '', $sutFqcn); + + if (!class_exists($sutFqcn)) { + return; + } + + return $sutFqcn; + } +} From 30381f4e8530f595ba4be06c19721ca9f7c5e596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 21 Sep 2017 11:47:11 +0200 Subject: [PATCH 02/10] Added test for the CoverageListener --- .../coverage/phpunit-with-listener.xml.dist | 27 +++++++++++++++++++ .../phpunit-without-listener.xml.dist | 23 ++++++++++++++++ .../Tests-Fixtures/coverage/src/Bar.php | 27 +++++++++++++++++++ .../Tests-Fixtures/coverage/src/Foo.php | 18 +++++++++++++ .../Tests-Fixtures/coverage/tests/BarTest.php | 23 ++++++++++++++++ .../coverage/tests/bootstrap.php | 19 +++++++++++++ .../PhpUnit/Tests/CoverageListenerTest.php | 27 +++++++++++++++++++ 7 files changed, 164 insertions(+) create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-without-listener.xml.dist create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Bar.php create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Foo.php create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/BarTest.php create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/bootstrap.php create mode 100644 src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist new file mode 100644 index 0000000000000..b184150dfd0f9 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist @@ -0,0 +1,27 @@ + + + + + + + tests + + + + + + src + + + + + + + diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-without-listener.xml.dist b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-without-listener.xml.dist new file mode 100644 index 0000000000000..6201535933767 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-without-listener.xml.dist @@ -0,0 +1,23 @@ + + + + + + + tests + + + + + + src + + + diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Bar.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Bar.php new file mode 100644 index 0000000000000..3eae8151e33ce --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Bar.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +class Bar +{ + private $foo; + + public function __construct(Foo $foo) + { + $this->foo = $foo; + } + + public function barZ() + { + $this->foo->fooZ(); + + return 'bar'; + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Foo.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Foo.php new file mode 100644 index 0000000000000..e799ca4a69a47 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Foo.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +class Foo +{ + public function fooZ() + { + return 'foo'; + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/BarTest.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/BarTest.php new file mode 100644 index 0000000000000..19d8295666943 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/BarTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use PHPUnit\Framework\TestCase; + +class BarTest extends TestCase +{ + public function testBar() + { + $foo = new Foo(); + $bar = new Bar($foo); + + $this->assertSame('bar', $bar->barZ()); + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/bootstrap.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/bootstrap.php new file mode 100644 index 0000000000000..d193c0bad3381 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/bootstrap.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require __DIR__.'/../src/Bar.php'; +require __DIR__.'/../src/Foo.php'; + +require __DIR__.'/../../../Legacy/CoverageListenerTrait.php'; +if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { + require __DIR__.'/../../../Legacy/CoverageListener.php'; +} +require __DIR__.'/../../../CoverageListener.php'; diff --git a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php new file mode 100644 index 0000000000000..943deb4d3d322 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php @@ -0,0 +1,27 @@ +markTestSkipped('This test cannot be run on Windows.'); + } + + $dir = __DIR__.'/../Tests-Fixtures/coverage'; + $php = PHP_BINARY; + $phpunit = $_SERVER['argv'][0]; + + exec("$php -d zend_extension=xdebug.so $phpunit -c $dir/phpunit-with-listener.xml.dist $dir/tests/ --coverage-text", $output); + $output = implode("\n", $output); + $this->assertNotContains('Foo', $output); + + exec("$php -d zend_extension=xdebug.so $phpunit -c $dir/phpunit-without-listener.xml.dist $dir/tests/ --coverage-text", $output); + $output = implode("\n", $output); + $this->assertContains('Foo', $output); + } +} From e764cfed2800d9dcf90750abdb19301dbd332299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Sep 2017 11:33:50 +0200 Subject: [PATCH 03/10] Added an option to throw a warning if the SUT solver can not find the SUT --- src/Symfony/Bridge/PhpUnit/CoverageListener.php | 4 ++-- src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php | 4 ++-- .../Bridge/PhpUnit/Legacy/CoverageListenerTrait.php | 8 +++++++- .../coverage/phpunit-with-listener.xml.dist | 7 ++++++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/CoverageListener.php b/src/Symfony/Bridge/PhpUnit/CoverageListener.php index ffd8b74911a36..e6b4e7ec98b8b 100644 --- a/src/Symfony/Bridge/PhpUnit/CoverageListener.php +++ b/src/Symfony/Bridge/PhpUnit/CoverageListener.php @@ -31,9 +31,9 @@ class CoverageListener extends BaseTestListener { private $trait; - public function __construct(callable $sutFqcnResolver = null) + public function __construct(callable $sutFqcnResolver = null, $warningOnSutNotFound = false) { - $this->trait = new CoverageListenerTrait($sutFqcnResolver); + $this->trait = new CoverageListenerTrait($sutFqcnResolver, $warningOnSutNotFound); } public function startTest(Test $test) diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php index 38944e98bf3f4..0227828515760 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListener.php @@ -23,9 +23,9 @@ class CoverageListener extends \PHPUnit_Framework_BaseTestListener { private $trait; - public function __construct(callable $sutFqcnResolver = null) + public function __construct(callable $sutFqcnResolver = null, $warningOnSutNotFound = false) { - $this->trait = new CoverageListenerTrait($sutFqcnResolver); + $this->trait = new CoverageListenerTrait($sutFqcnResolver, $warningOnSutNotFound); } public function startTest(\PHPUnit_Framework_Test $test) diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php index 5aded4a77c78d..cc98dcdb5fd34 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php @@ -12,6 +12,7 @@ namespace Symfony\Bridge\PhpUnit\Legacy; use PHPUnit\Framework\Test; +use PHPUnit\Framework\Warning; /** * PHP 5.3 compatible trait-like shared implementation. @@ -23,10 +24,12 @@ class CoverageListenerTrait { private $sutFqcnResolver; + private $warningOnSutNotFound; - public function __construct(callable $sutFqcnResolver = null) + public function __construct(callable $sutFqcnResolver = null, $warningOnSutNotFound = false) { $this->sutFqcnResolver = $sutFqcnResolver; + $this->warningOnSutNotFound = $warningOnSutNotFound; } public function startTest($test) @@ -39,6 +42,9 @@ public function startTest($test) $sutFqcn = $this->findSutFqcn($test); if (!$sutFqcn) { + if ($this->warningOnSutNotFound) { + $test->getTestResultObject()->addWarning($test, new Warning('Could not find the tested class.'), 0); + } return; } diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist index b184150dfd0f9..1984359ebdd78 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist @@ -22,6 +22,11 @@ - + + + + true + + From b99c193c81b8bd6758fb7a14843eee0884552d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Sep 2017 12:04:42 +0200 Subject: [PATCH 04/10] Added more tests + support more annotations --- .../PhpUnit/Legacy/CoverageListenerTrait.php | 11 ++++++--- .../coverage/tests/CoversDefaultClassTest.php | 23 +++++++++++++++++++ .../coverage/tests/CoversNothingTest.php | 23 +++++++++++++++++++ .../coverage/tests/CoversTest.php | 23 +++++++++++++++++++ .../coverage/tests/SutNotFindTest.php | 20 ++++++++++++++++ .../PhpUnit/Tests/CoverageListenerTest.php | 12 ++++++---- 6 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversDefaultClassTest.php create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversNothingTest.php create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversTest.php create mode 100644 src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/SutNotFindTest.php diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php index cc98dcdb5fd34..930532212441c 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php @@ -36,8 +36,12 @@ public function startTest($test) { $annotations = $test->getAnnotations(); - if (isset($annotations['class']['covers']) || isset($annotations['method']['covers'])) { - return; + $ignoredAnnotations = array('covers', 'coversDefaultClass', 'coversNothing'); + + foreach ($ignoredAnnotations as $annotation) { + if (isset($annotations['class'][$annotation]) || isset($annotations['method'][$annotation])) { + return; + } } $sutFqcn = $this->findSutFqcn($test); @@ -45,6 +49,7 @@ public function startTest($test) if ($this->warningOnSutNotFound) { $test->getTestResultObject()->addWarning($test, new Warning('Could not find the tested class.'), 0); } + return; } @@ -75,7 +80,7 @@ private function findSutFqcn($test) $class = get_class($test); - $sutFqcn = str_replace('Tests\\', '', $class); + $sutFqcn = str_replace('\\Tests\\', '\\', $class); $sutFqcn = preg_replace('{Test$}', '', $sutFqcn); if (!class_exists($sutFqcn)) { diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversDefaultClassTest.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversDefaultClassTest.php new file mode 100644 index 0000000000000..d764638d04958 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversDefaultClassTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use PHPUnit\Framework\TestCase; + +/** + * @coversDefaultClass \DateTime + */ +class CoversDefaultClassTest extends TestCase +{ + public function test() + { + $this->assertTrue(true); + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversNothingTest.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversNothingTest.php new file mode 100644 index 0000000000000..e60ea97e57bbd --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversNothingTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use PHPUnit\Framework\TestCase; + +/** + * @coversNothing + */ +class CoversNothingTest extends TestCase +{ + public function test() + { + $this->assertTrue(true); + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversTest.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversTest.php new file mode 100644 index 0000000000000..f6d3406046d86 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversTest.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use PHPUnit\Framework\TestCase; + +class CoversTest extends TestCase +{ + /** + * @covers \DateTime + */ + public function test() + { + $this->assertTrue(true); + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/SutNotFindTest.php b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/SutNotFindTest.php new file mode 100644 index 0000000000000..934ee77dc1873 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/SutNotFindTest.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use PHPUnit\Framework\TestCase; + +class SutNotFoundTest extends TestCase +{ + public function test() + { + $this->assertTrue(true); + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php index 943deb4d3d322..81fdfeb393161 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php @@ -16,12 +16,16 @@ public function test() $php = PHP_BINARY; $phpunit = $_SERVER['argv'][0]; - exec("$php -d zend_extension=xdebug.so $phpunit -c $dir/phpunit-with-listener.xml.dist $dir/tests/ --coverage-text", $output); - $output = implode("\n", $output); - $this->assertNotContains('Foo', $output); - exec("$php -d zend_extension=xdebug.so $phpunit -c $dir/phpunit-without-listener.xml.dist $dir/tests/ --coverage-text", $output); $output = implode("\n", $output); $this->assertContains('Foo', $output); + + exec("$php -d zend_extension=xdebug.so $phpunit -c $dir/phpunit-with-listener.xml.dist $dir/tests/ --coverage-text", $output); + $output = implode("\n", $output); + $this->assertNotContains('Foo', $output); + $this->assertContains("SutNotFoundTest::test\nCould not find the tested class.", $output); + $this->assertNotContains("CoversTest::test\nCould not find the tested class.", $output); + $this->assertNotContains("CoversDefaultClassTest::test\nCould not find the tested class.", $output); + $this->assertNotContains("CoversNothingTest::test\nCould not find the tested class.", $output); } } From 87aebab3c902683dfff12c6ddc8f9c13d3b99265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Sep 2017 12:10:00 +0200 Subject: [PATCH 05/10] Better FS structure & avoid to run fixture test in the main test suite --- src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php | 2 +- .../Fixtures}/coverage/phpunit-with-listener.xml.dist | 0 .../Fixtures}/coverage/phpunit-without-listener.xml.dist | 0 .../{Tests-Fixtures => Tests/Fixtures}/coverage/src/Bar.php | 0 .../{Tests-Fixtures => Tests/Fixtures}/coverage/src/Foo.php | 0 .../Fixtures}/coverage/tests/BarTest.php | 4 ++++ .../Fixtures}/coverage/tests/CoversDefaultClassTest.php | 0 .../Fixtures}/coverage/tests/CoversNothingTest.php | 0 .../Fixtures}/coverage/tests/CoversTest.php | 0 .../Fixtures}/coverage/tests/SutNotFindTest.php | 0 .../Fixtures}/coverage/tests/bootstrap.php | 6 +++--- 11 files changed, 8 insertions(+), 4 deletions(-) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/phpunit-with-listener.xml.dist (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/phpunit-without-listener.xml.dist (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/src/Bar.php (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/src/Foo.php (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/tests/BarTest.php (71%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/tests/CoversDefaultClassTest.php (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/tests/CoversNothingTest.php (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/tests/CoversTest.php (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/tests/SutNotFindTest.php (100%) rename src/Symfony/Bridge/PhpUnit/{Tests-Fixtures => Tests/Fixtures}/coverage/tests/bootstrap.php (69%) diff --git a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php index 81fdfeb393161..3c8444d4645f0 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php @@ -12,7 +12,7 @@ public function test() $this->markTestSkipped('This test cannot be run on Windows.'); } - $dir = __DIR__.'/../Tests-Fixtures/coverage'; + $dir = __DIR__.'/../Tests/Fixtures/coverage'; $php = PHP_BINARY; $phpunit = $_SERVER['argv'][0]; diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/phpunit-with-listener.xml.dist similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-with-listener.xml.dist rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/phpunit-with-listener.xml.dist diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-without-listener.xml.dist b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/phpunit-without-listener.xml.dist similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/phpunit-without-listener.xml.dist rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/phpunit-without-listener.xml.dist diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Bar.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Bar.php similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Bar.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Bar.php diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Foo.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Foo.php similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/src/Foo.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Foo.php diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/BarTest.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php similarity index 71% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/BarTest.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php index 19d8295666943..9646f4de80513 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/BarTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php @@ -15,6 +15,10 @@ class BarTest extends TestCase { public function testBar() { + if (!class_exists('Foo')) { + $this->markTestSkipped('This test is not part of the main Symfony test suite. It\'s here to test the CoverageListener.'); + } + $foo = new Foo(); $bar = new Bar($foo); diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversDefaultClassTest.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/CoversDefaultClassTest.php similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversDefaultClassTest.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/CoversDefaultClassTest.php diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversNothingTest.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/CoversNothingTest.php similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversNothingTest.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/CoversNothingTest.php diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversTest.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/CoversTest.php similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/CoversTest.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/CoversTest.php diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/SutNotFindTest.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/SutNotFindTest.php similarity index 100% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/SutNotFindTest.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/SutNotFindTest.php diff --git a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/bootstrap.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/bootstrap.php similarity index 69% rename from src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/bootstrap.php rename to src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/bootstrap.php index d193c0bad3381..9647a8658d212 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests-Fixtures/coverage/tests/bootstrap.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/bootstrap.php @@ -12,8 +12,8 @@ require __DIR__.'/../src/Bar.php'; require __DIR__.'/../src/Foo.php'; -require __DIR__.'/../../../Legacy/CoverageListenerTrait.php'; +require __DIR__.'/../../../../Legacy/CoverageListenerTrait.php'; if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { - require __DIR__.'/../../../Legacy/CoverageListener.php'; + require __DIR__.'/../../../../Legacy/CoverageListener.php'; } -require __DIR__.'/../../../CoverageListener.php'; +require __DIR__.'/../../../../CoverageListener.php'; From 239bc354370f1feac21e6f435c5125ef64e43a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Sep 2017 12:23:48 +0200 Subject: [PATCH 06/10] Added a BC layer for older PHPUnit --- .../PhpUnit/Legacy/CoverageListenerTrait.php | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php index 930532212441c..e5412f86ad637 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php @@ -25,11 +25,13 @@ class CoverageListenerTrait { private $sutFqcnResolver; private $warningOnSutNotFound; + private $warnings; public function __construct(callable $sutFqcnResolver = null, $warningOnSutNotFound = false) { $this->sutFqcnResolver = $sutFqcnResolver; $this->warningOnSutNotFound = $warningOnSutNotFound; + $this->warnings = array(); } public function startTest($test) @@ -47,7 +49,13 @@ public function startTest($test) $sutFqcn = $this->findSutFqcn($test); if (!$sutFqcn) { if ($this->warningOnSutNotFound) { - $test->getTestResultObject()->addWarning($test, new Warning('Could not find the tested class.'), 0); + $message = 'Could not find the tested class.'; + // addWarning does not exist on old PHPUnit version + if (method_exists($test->getTestResultObject(), 'addWarning')) { + $test->getTestResultObject()->addWarning($test, new Warning($message), 0); + } else { + $this->warnings[] = sprintf("%s::%s\n%s", get_class($test), $test->getName(), $message); + } } return; @@ -89,4 +97,17 @@ private function findSutFqcn($test) return $sutFqcn; } + + public function __destruct() + { + if (!$this->warnings) { + return; + } + + echo "\n"; + + foreach ($this->warnings as $key => $warning) { + echo sprintf("%d) %s\n", ++$key, $warning); + } + } } From a710cf26da43bcd1f2e29bd14e4098f3f3f94c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Sep 2017 12:30:50 +0200 Subject: [PATCH 07/10] 'Fixed' tests on HHVM --- src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php index 3c8444d4645f0..216b860a4bc2c 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php @@ -12,6 +12,10 @@ public function test() $this->markTestSkipped('This test cannot be run on Windows.'); } + if (defined('HHVM_VERSION')) { + $this->markTestSkipped('This test cannot be run on HHVM.'); + } + $dir = __DIR__.'/../Tests/Fixtures/coverage'; $php = PHP_BINARY; $phpunit = $_SERVER['argv'][0]; From 1e92acd059d7e3a849b3966d3b4bef9513cb9a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Sep 2017 12:41:31 +0200 Subject: [PATCH 08/10] Fixed tests on Windows --- .../Bridge/PhpUnit/Tests/Fixtures/coverage/src/Bar.php | 2 ++ .../Bridge/PhpUnit/Tests/Fixtures/coverage/src/Foo.php | 2 ++ .../PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php | 8 +++++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Bar.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Bar.php index 3eae8151e33ce..b50305a402634 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Bar.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Bar.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +namespace PhpUnitCoverageTest; + class Bar { private $foo; diff --git a/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Foo.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Foo.php index e799ca4a69a47..f811ae70b10a0 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Foo.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/src/Foo.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +namespace PhpUnitCoverageTest; + class Foo { public function fooZ() diff --git a/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php index 9646f4de80513..b49fc706a9cfa 100644 --- a/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php +++ b/src/Symfony/Bridge/PhpUnit/Tests/Fixtures/coverage/tests/BarTest.php @@ -9,18 +9,20 @@ * file that was distributed with this source code. */ +namespace PhpUnitCoverageTest\Tests; + use PHPUnit\Framework\TestCase; class BarTest extends TestCase { public function testBar() { - if (!class_exists('Foo')) { + if (!class_exists('PhpUnitCoverageTest\Foo')) { $this->markTestSkipped('This test is not part of the main Symfony test suite. It\'s here to test the CoverageListener.'); } - $foo = new Foo(); - $bar = new Bar($foo); + $foo = new \PhpUnitCoverageTest\Foo(); + $bar = new \PhpUnitCoverageTest\Bar($foo); $this->assertSame('bar', $bar->barZ()); } From aad02e67092bfae676e82ad1413d3f522d9d8176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Fri, 22 Sep 2017 12:43:33 +0200 Subject: [PATCH 09/10] Added note in CHANGELOG --- src/Symfony/Bridge/PhpUnit/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Symfony/Bridge/PhpUnit/CHANGELOG.md b/src/Symfony/Bridge/PhpUnit/CHANGELOG.md index 1cb07da782b6d..39ae3a881b3b1 100644 --- a/src/Symfony/Bridge/PhpUnit/CHANGELOG.md +++ b/src/Symfony/Bridge/PhpUnit/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.4.0 +----- + + * added a `CoverageListener` to enhance the code coverage report + 3.3.0 ----- From b80489f7309176ef7d1cfc1c4370dd26e872bb63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Tue, 26 Sep 2017 18:25:43 +0200 Subject: [PATCH 10/10] Fixed issue with older PHPUnit Version --- src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php index e5412f86ad637..0a1603e646cbf 100644 --- a/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php +++ b/src/Symfony/Bridge/PhpUnit/Legacy/CoverageListenerTrait.php @@ -51,7 +51,7 @@ public function startTest($test) if ($this->warningOnSutNotFound) { $message = 'Could not find the tested class.'; // addWarning does not exist on old PHPUnit version - if (method_exists($test->getTestResultObject(), 'addWarning')) { + if (method_exists($test->getTestResultObject(), 'addWarning') && class_exists(Warning::class)) { $test->getTestResultObject()->addWarning($test, new Warning($message), 0); } else { $this->warnings[] = sprintf("%s::%s\n%s", get_class($test), $test->getName(), $message);