diff --git a/.travis.yml b/.travis.yml index 23e5f24b84..da63922976 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,6 +37,7 @@ matrix: - php: 7.1 env: - DEPS=locked + - LEGACY_DEPS="phpunit/phpunit" - CS_CHECK=true - TEST_COVERAGE=true - php: 7.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 9177c70697..8ae93d2fad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,28 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 3.3.0 - TBD + +### Added + +- [#76](https://github.com/zendframework/zend-test/pull/76) adds support for PhpUnit 8 + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 3.2.3 - TBD ### Added diff --git a/autoload/phpunit-class-aliases.php b/autoload/phpunit-class-aliases.php index d5aa73fe3f..18100dfdf3 100644 --- a/autoload/phpunit-class-aliases.php +++ b/autoload/phpunit-class-aliases.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\Version; if (! class_exists(ExpectationFailedException::class)) { class_alias(\PHPUnit_Framework_ExpectationFailedException::class, ExpectationFailedException::class); @@ -15,3 +16,15 @@ class_alias(\PHPUnit_Framework_ExpectationFailedException::class, ExpectationFai if (! class_exists(TestCase::class)) { class_alias(\PHPUnit_Framework_TestCase::class, TestCase::class); } + +// Compatibility with PHPUnit 8.0 +// We need to use "magic" trait \Zend\Test\PHPUnit\TestCaseTrait +// and instead of setUp/tearDown method in test case +// we should have setUpCompat/tearDownCompat. +if (class_exists(Version::class) + && version_compare(Version::id(), '8.0.0') >= 0 +) { + class_alias(\Zend\Test\PHPUnit\TestCaseTypeHintTrait::class, \Zend\Test\PHPUnit\TestCaseTrait::class); +} else { + class_alias(\Zend\Test\PHPUnit\TestCaseNoTypeHintTrait::class, \Zend\Test\PHPUnit\TestCaseTrait::class); +} diff --git a/composer.json b/composer.json index a2309db8d8..73ce2a7106 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "require": { "php": "^5.6 || ^7.0", - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0", "sebastian/version": "^1.0.4 || ^2.0", "zendframework/zend-console": "^2.6", "zendframework/zend-dom": "^2.6", @@ -30,7 +30,7 @@ "zendframework/zend-view": "^2.6.3" }, "require-dev": { - "mikey179/vfsStream": "~1.2", + "mikey179/vfsstream": "~1.2", "symfony/finder": "^2.2", "zendframework/zend-coding-standard": "~1.0.0", "zendframework/zend-i18n": "^2.6", diff --git a/composer.lock b/composer.lock index dd3ae58c87..559ee77c3b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bf86eb359734daf37a5f184d6a328b1b", + "content-hash": "5d7ec82ed524ab9ac008eeee868ab314", "packages": [ { "name": "container-interop/container-interop", diff --git a/src/PHPUnit/Controller/AbstractControllerTestCase.php b/src/PHPUnit/Controller/AbstractControllerTestCase.php index 0fe8a0eb2c..354ce6b80c 100644 --- a/src/PHPUnit/Controller/AbstractControllerTestCase.php +++ b/src/PHPUnit/Controller/AbstractControllerTestCase.php @@ -18,10 +18,13 @@ use Zend\Stdlib\Exception\LogicException; use Zend\Stdlib\Parameters; use Zend\Stdlib\ResponseInterface; +use Zend\Test\PHPUnit\TestCaseTrait; use Zend\Uri\Http as HttpUri; abstract class AbstractControllerTestCase extends TestCase { + use TestCaseTrait; + /** * @var \Zend\Mvc\ApplicationInterface */ @@ -52,8 +55,10 @@ abstract class AbstractControllerTestCase extends TestCase /** * Reset the application for isolation + * + * @internal */ - protected function setUp() + protected function setUpCompat() { $this->usedConsoleBackup = Console::isConsole(); $this->reset(); @@ -61,8 +66,10 @@ protected function setUp() /** * Restore params + * + * @internal */ - protected function tearDown() + protected function tearDownCompat() { Console::overrideIsConsole($this->usedConsoleBackup); diff --git a/src/PHPUnit/TestCaseNoTypeHintTrait.php b/src/PHPUnit/TestCaseNoTypeHintTrait.php new file mode 100644 index 0000000000..63f4a442ef --- /dev/null +++ b/src/PHPUnit/TestCaseNoTypeHintTrait.php @@ -0,0 +1,37 @@ +setUpCompat(); + } + } + + protected function tearDown() + { + if (method_exists($this, 'tearDownCompat')) { + $this->tearDownCompat(); + } + } + + public static function setUpBeforeClass() + { + if (method_exists(static::class, 'setUpBeforeClassCompat')) { + static::setUpBeforeClassCompat(); + } + } + + public static function tearDownAfterClass() + { + if (method_exists(static::class, 'tearDownAfterClassCompat')) { + static::tearDownAfterClassCompat(); + } + } +} diff --git a/src/PHPUnit/TestCaseTypeHintTrait.php b/src/PHPUnit/TestCaseTypeHintTrait.php new file mode 100644 index 0000000000..7bd62635ca --- /dev/null +++ b/src/PHPUnit/TestCaseTypeHintTrait.php @@ -0,0 +1,37 @@ +setUpCompat(); + } + } + + protected function tearDown() : void + { + if (method_exists($this, 'tearDownCompat')) { + $this->tearDownCompat(); + } + } + + public static function setUpBeforeClass() : void + { + if (method_exists(static::class, 'setUpBeforeClassCompat')) { + static::setUpBeforeClassCompat(); + } + } + + public static function tearDownAfterClass() : void + { + if (method_exists(static::class, 'tearDownAfterClassCompat')) { + static::tearDownAfterClassCompat(); + } + } +} diff --git a/test/PHPUnit/Controller/AbstractConsoleControllerTestCaseTest.php b/test/PHPUnit/Controller/AbstractConsoleControllerTestCaseTest.php index 07c16c4b53..3490ad271e 100644 --- a/test/PHPUnit/Controller/AbstractConsoleControllerTestCaseTest.php +++ b/test/PHPUnit/Controller/AbstractConsoleControllerTestCaseTest.php @@ -20,12 +20,12 @@ class AbstractConsoleControllerTestCaseTest extends AbstractConsoleControllerTes { use ExpectedExceptionTrait; - protected function setUp() + protected function setUpCompat() { $this->setApplicationConfig( include __DIR__ . '/../../_files/application.config.php' ); - parent::setUp(); + parent::setUpCompat(); } public function testUseOfRouter() diff --git a/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php b/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php index e36cee4c63..f793d0bbf1 100644 --- a/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php +++ b/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php @@ -50,7 +50,7 @@ public static function rmdir($dir) return rmdir($dir); } - protected function setUp() + protected function setUpCompat() { $this->traceErrorCache = $this->traceError; $this->tearDownCacheDir(); @@ -58,14 +58,14 @@ protected function setUp() $this->setApplicationConfig( include __DIR__ . '/../../_files/application.config.php' ); - parent::setUp(); + parent::setUpCompat(); } - protected function tearDown() + protected function tearDownCompat() { $this->traceError = $this->traceErrorCache; $this->tearDownCacheDir(); - parent::tearDown(); + parent::tearDownCompat(); } public function testModuleCacheIsDisabled() @@ -107,13 +107,13 @@ public function testApplicationClassAndTestRestoredConsoleFlag() $this->assertTrue(Console::isConsole(), '3. Console::isConsole returned false after tearDown'); Console::overrideIsConsole(false); - parent::setUp(); + parent::setUpCompat(); $this->assertFalse(Console::isConsole(), '4. Console::isConsole returned true after parent::setUp'); $this->getApplication(); $this->assertFalse(Console::isConsole(), '5. Console::isConsole returned true after retrieving application'); - parent::tearDown(); + parent::tearDownCompat(); $this->assertFalse(Console::isConsole(), '6. Console.isConsole returned true after parent::tearDown'); } @@ -169,9 +169,9 @@ public function testAssertExceptionDetailsPresentWhenTraceErrorIsEnabled() $this->assertTrue($caught, 'Did not catch expected exception!'); - $this->assertContains('actual module name is "baz"', $message); - $this->assertContains("Exception 'RuntimeException' with message 'Expected exception message'", $message); - $this->assertContains(__FILE__, $message); + $this->assertContainsCompat('actual module name is "baz"', $message); + $this->assertContainsCompat("Exception 'RuntimeException' with message 'Expected exception message'", $message); + $this->assertContainsCompat(__FILE__, $message); } public function testAssertExceptionDetailsNotPresentWhenTraceErrorIsDisabled() @@ -193,9 +193,12 @@ public function testAssertExceptionDetailsNotPresentWhenTraceErrorIsDisabled() $this->assertTrue($caught, 'Did not catch expected exception!'); - $this->assertContains('actual module name is "baz"', $message); - $this->assertNotContains("Exception 'RuntimeException' with message 'Expected exception message'", $message); - $this->assertNotContains(__FILE__, $message); + $this->assertContainsCompat('actual module name is "baz"', $message); + $this->assertNotContainsCompat( + "Exception 'RuntimeException' with message 'Expected exception message'", + $message + ); + $this->assertNotContainsCompat(__FILE__, $message); } public function testAssertNotModuleName() @@ -535,4 +538,22 @@ public function testRequestWithRouteParam($param) $this->dispatch(sprintf('/with-param/%s', $param)); $this->assertResponseStatusCode(200); } + + private function assertContainsCompat($needle, $haystack) + { + if (method_exists($this, 'assertStringContainsString')) { + $this->assertStringContainsString($needle, $haystack); + } else { + $this->assertContains($needle, $haystack); + } + } + + private function assertNotContainsCompat($needle, $haystack) + { + if (method_exists($this, 'assertStringNotContainsString')) { + $this->assertStringNotContainsString($needle, $haystack); + } else { + $this->assertNotContains($needle, $haystack); + } + } } diff --git a/test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php b/test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php index 8290c597ed..5a8aab7408 100644 --- a/test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php +++ b/test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php @@ -24,12 +24,12 @@ class AbstractHttpControllerTestCaseTest extends AbstractHttpControllerTestCase { use ExpectedExceptionTrait; - public function setUp() + protected function setUpCompat() { $this->setApplicationConfig( include __DIR__ . '/../../_files/application.config.php' ); - parent::setUp(); + parent::setUpCompat(); } public function testUseOfRouter() diff --git a/test/PHPUnit/Controller/MemoryLeakTest.php b/test/PHPUnit/Controller/MemoryLeakTest.php index 41de9de0ff..c8f11ed570 100644 --- a/test/PHPUnit/Controller/MemoryLeakTest.php +++ b/test/PHPUnit/Controller/MemoryLeakTest.php @@ -12,7 +12,7 @@ class MemoryLeakTest extends AbstractControllerTestCase { public static $memStart; - public static function setUpBeforeClass() + protected static function setUpBeforeClassCompat() { self::$memStart = memory_get_usage(true); } diff --git a/test/PHPUnit/Util/ModuleLoaderTest.php b/test/PHPUnit/Util/ModuleLoaderTest.php index 2e89d727c6..726076aa8b 100644 --- a/test/PHPUnit/Util/ModuleLoaderTest.php +++ b/test/PHPUnit/Util/ModuleLoaderTest.php @@ -10,12 +10,14 @@ use PHPUnit\Framework\TestCase; use Zend\ModuleManager\Exception\RuntimeException; +use Zend\Test\PHPUnit\TestCaseTrait; use Zend\Test\Util\ModuleLoader; use ZendTest\Test\ExpectedExceptionTrait; class ModuleLoaderTest extends TestCase { use ExpectedExceptionTrait; + use TestCaseTrait; public function tearDownCacheDir() { @@ -35,12 +37,12 @@ public static function rmdir($dir) return rmdir($dir); } - public function setUp() + protected function setUpCompat() { $this->tearDownCacheDir(); } - public function tearDown() + protected function tearDownCompat() { $this->tearDownCacheDir(); }