From 9cb4477163a02a2535d7a8e07eed31c7411314ff Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 10:22:22 +0100 Subject: [PATCH 01/13] Update for PHPUnit 9.1 --- ProphecyTestCase.php | 96 ++++++++++++++-------------------- Tests/ProphecyTestCaseTest.php | 53 +++++++++++-------- 2 files changed, 68 insertions(+), 81 deletions(-) diff --git a/ProphecyTestCase.php b/ProphecyTestCase.php index 2229677..0f2c968 100644 --- a/ProphecyTestCase.php +++ b/ProphecyTestCase.php @@ -1,91 +1,71 @@ -getProphet()->prophesize($classOrInterface); + if (\is_string($classOrInterface)) { + $this->recordDoubledType($classOrInterface); + } + + return $this->prophet()->prophesize($classOrInterface); } - protected function verifyMockObjects() + protected function verifyMockObjects(): void { parent::verifyMockObjects(); - if (null === $this->prophet) { - return; - } - - try { - $this->prophet->checkPredictions(); - } catch (\Exception $e) { - /** Intentionally left empty */ - } - - $this->countProphecyAssertions(); - - if (isset($e)) { - throw $e; + if ($this->prophet !== null) { + try { + $this->prophet->checkPredictions(); + } catch (PredictionException $e) { + throw new AssertionFailedError($e->getMessage()); + } finally { + $this->countProphecyAssertions(); + } } } - protected function tearDown() + private function countProphecyAssertions(): void { - if (null !== $this->prophet && !$this->prophecyAssertionsCounted) { - // Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects. - $this->countProphecyAssertions(); - } - - $this->prophet = null; - } + foreach ($this->prophet->getProphecies() as $objectProphecy) { + foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) { + foreach ($methodProphecies as $methodProphecy) { + \assert($methodProphecy instanceof MethodProphecy); - protected function onNotSuccessfulTest(\Exception $e) - { - if ($e instanceof PredictionException) { - $e = new \PHPUnit_Framework_AssertionFailedError($e->getMessage(), $e->getCode(), $e); + $this->addToAssertionCount(\count($methodProphecy->getCheckedPredictions())); + } + } } - - return parent::onNotSuccessfulTest($e); } - /** - * @return Prophet - */ - private function getProphet() + private function prophet(): Prophet { - if (null === $this->prophet) { - $this->prophet = new Prophet(); + if ($this->prophet === null) { + $this->prophet = new Prophet; } return $this->prophet; } - - private function countProphecyAssertions() - { - $this->prophecyAssertionsCounted = true; - - foreach ($this->prophet->getProphecies() as $objectProphecy) { - foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) { - foreach ($methodProphecies as $methodProphecy) { - $this->addToAssertionCount(count($methodProphecy->getCheckedPredictions())); - } - } - } - } } diff --git a/Tests/ProphecyTestCaseTest.php b/Tests/ProphecyTestCaseTest.php index 0750d16..39e0658 100644 --- a/Tests/ProphecyTestCaseTest.php +++ b/Tests/ProphecyTestCaseTest.php @@ -1,63 +1,70 @@ -run(); - $this->assertEquals(0, $result->errorCount()); - $this->assertEquals(0, $result->failureCount()); + $this->assertSame(0, $result->errorCount()); + $this->assertSame(0, $result->failureCount()); $this->assertCount(1, $result); - $this->assertEquals(1, $test->getNumAssertions()); + $this->assertSame(1, $test->getNumAssertions()); } - public function testSpyPredictionFailure() + public function testSpyPredictionFailure(): void { $test = new SpyFailure('testMethod'); + $result = $test->run(); - $this->assertEquals(0, $result->errorCount()); - $this->assertEquals(1, $result->failureCount()); + $this->assertSame(0, $result->errorCount()); + $this->assertSame(1, $result->failureCount()); $this->assertCount(1, $result); - $this->assertEquals(1, $test->getNumAssertions()); + $this->assertSame(1, $test->getNumAssertions()); } - public function testMockPredictionFailure() + public function testMockPredictionFailure(): void { $test = new MockFailure('testMethod'); + $result = $test->run(); - $this->assertEquals(0, $result->errorCount()); - $this->assertEquals(1, $result->failureCount()); + $this->assertSame(0, $result->errorCount()); + $this->assertSame(1, $result->failureCount()); $this->assertCount(1, $result); - $this->assertEquals(1, $test->getNumAssertions()); + $this->assertSame(1, $test->getNumAssertions()); } - public function testDoublingError() + public function testDoublingError(): void { $test = new Error('testMethod'); + $result = $test->run(); - $this->assertEquals(1, $result->errorCount()); - $this->assertEquals(0, $result->failureCount()); + $this->assertSame(1, $result->errorCount()); + $this->assertSame(0, $result->failureCount()); $this->assertCount(1, $result); - $this->assertEquals(0, $test->getNumAssertions()); + $this->assertSame(0, $test->getNumAssertions()); } } From 86e75db4386025d6c1cd48114b82217b40aaa94e Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 10:22:56 +0100 Subject: [PATCH 02/13] Allow installation of development versions --- composer.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/composer.json b/composer.json index 6937d75..b03a036 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,8 @@ "email": "stof@notk.org" } ], + "minimum-stability": "dev", + "prefer-stable": true, "require": { "php": ">=5.3.3", "phpspec/prophecy": "~1.3" From c8662884d83d28d5a09d9135d9a44bc539afb62e Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 10:23:31 +0100 Subject: [PATCH 03/13] Depend on PHP 7.3 (required by PHPUnit 9) and PHPUnit --- composer.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index b03a036..8c01197 100644 --- a/composer.json +++ b/composer.json @@ -14,11 +14,9 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "php": ">=5.3.3", - "phpspec/prophecy": "~1.3" - }, - "suggest": { - "phpunit/phpunit": "if it is not installed globally" + "php": "^7.3", + "phpspec/prophecy": "^1.3", + "phpunit/phpunit":"^9.1" }, "autoload": { "psr-4": { From c73c17c88acced89aa8ca520af4b1e2baa04f430 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 10:23:39 +0100 Subject: [PATCH 04/13] Bump --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8c01197..6490597 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "2.0-dev" } } } From 40378c776bac00b889664fa54ef3570bacc25949 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 12:01:28 +0100 Subject: [PATCH 05/13] Restore whitespace before namespace declaration --- ProphecyTestCase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/ProphecyTestCase.php b/ProphecyTestCase.php index 0f2c968..de97f2a 100644 --- a/ProphecyTestCase.php +++ b/ProphecyTestCase.php @@ -1,4 +1,5 @@ Date: Tue, 24 Mar 2020 12:01:44 +0100 Subject: [PATCH 06/13] This can be null --- ProphecyTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProphecyTestCase.php b/ProphecyTestCase.php index de97f2a..f44ff12 100644 --- a/ProphecyTestCase.php +++ b/ProphecyTestCase.php @@ -14,7 +14,7 @@ abstract class ProphecyTestCase extends TestCase { /** - * @var Prophet + * @var Prophet|null */ private $prophet; From 90839916690da932fa886ea6cc4cdc07d6c1ac35 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 12:02:13 +0100 Subject: [PATCH 07/13] Use getProphet() instead of prophet() --- ProphecyTestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ProphecyTestCase.php b/ProphecyTestCase.php index f44ff12..3eb6dce 100644 --- a/ProphecyTestCase.php +++ b/ProphecyTestCase.php @@ -30,7 +30,7 @@ protected function prophesize(?string $classOrInterface = null): ObjectProphecy $this->recordDoubledType($classOrInterface); } - return $this->prophet()->prophesize($classOrInterface); + return $this->getProphet()->prophesize($classOrInterface); } protected function verifyMockObjects(): void @@ -61,7 +61,7 @@ private function countProphecyAssertions(): void } } - private function prophet(): Prophet + private function getProphet(): Prophet { if ($this->prophet === null) { $this->prophet = new Prophet; From 32f5023600cbed419c07280782f04706038461b5 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 12:02:26 +0100 Subject: [PATCH 08/13] Use early return --- ProphecyTestCase.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ProphecyTestCase.php b/ProphecyTestCase.php index 3eb6dce..3e0f278 100644 --- a/ProphecyTestCase.php +++ b/ProphecyTestCase.php @@ -37,14 +37,16 @@ protected function verifyMockObjects(): void { parent::verifyMockObjects(); - if ($this->prophet !== null) { - try { - $this->prophet->checkPredictions(); - } catch (PredictionException $e) { - throw new AssertionFailedError($e->getMessage()); - } finally { - $this->countProphecyAssertions(); - } + if ($this->prophet === null) { + return; + } + + try { + $this->prophet->checkPredictions(); + } catch (PredictionException $e) { + throw new AssertionFailedError($e->getMessage()); + } finally { + $this->countProphecyAssertions(); } } From 5accf5beae4014a2f81bada6a2ce0be8a3fdc001 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 12:04:32 +0100 Subject: [PATCH 09/13] Test with PHP 7.3 and PHP 7.4 on Travis CI --- .travis.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 46ac9a0..2df2267 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,11 +3,8 @@ language: php sudo: false php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 - - hhvm + - 7.3 + - 7.4 install: composer install From fe5e619efd55f05be88764e5061153371e6e176c Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 12:04:53 +0100 Subject: [PATCH 10/13] Use project-local version of PHPUnit instead of globally installed PHPUnit --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2df2267..2e19896 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,4 +8,4 @@ php: install: composer install -script: phpunit --coverage-text +script: ./vendor/bin/phpunit --coverage-text From 3b6e8120e5d8349f715f6797103cee94cd4c4a27 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 12:18:29 +0100 Subject: [PATCH 11/13] Restore whitespace before namespace declaration --- Tests/ProphecyTestCaseTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/ProphecyTestCaseTest.php b/Tests/ProphecyTestCaseTest.php index 39e0658..74b0d6e 100644 --- a/Tests/ProphecyTestCaseTest.php +++ b/Tests/ProphecyTestCaseTest.php @@ -1,4 +1,5 @@ Date: Tue, 24 Mar 2020 12:22:37 +0100 Subject: [PATCH 12/13] Make spies work again --- ProphecyTestCase.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ProphecyTestCase.php b/ProphecyTestCase.php index 3e0f278..e2c7361 100644 --- a/ProphecyTestCase.php +++ b/ProphecyTestCase.php @@ -18,6 +18,11 @@ abstract class ProphecyTestCase extends TestCase */ private $prophet; + /** + * @var bool + */ + private $prophecyAssertionsCounted = false; + /** * @throws DoubleException * @throws InterfaceNotFoundException @@ -50,8 +55,20 @@ protected function verifyMockObjects(): void } } + protected function tearDown(): void + { + if (null !== $this->prophet && !$this->prophecyAssertionsCounted) { + // Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects. + $this->countProphecyAssertions(); + } + + $this->prophet = null; + } + private function countProphecyAssertions(): void { + $this->prophecyAssertionsCounted = true; + foreach ($this->prophet->getProphecies() as $objectProphecy) { foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) { foreach ($methodProphecies as $methodProphecy) { From 0cf38aa0c71401ff227c47e97ae2201c2d9fc8ba Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 24 Mar 2020 12:24:08 +0100 Subject: [PATCH 13/13] Use @after annotation instead of tearDown() method name --- ProphecyTestCase.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ProphecyTestCase.php b/ProphecyTestCase.php index e2c7361..4e21da5 100644 --- a/ProphecyTestCase.php +++ b/ProphecyTestCase.php @@ -55,7 +55,10 @@ protected function verifyMockObjects(): void } } - protected function tearDown(): void + /** + * @after + */ + protected function prophecyTearDown(): void { if (null !== $this->prophet && !$this->prophecyAssertionsCounted) { // Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects.