From 1ba2e3e1bb091acda3139f8a9259fa8161f3242d Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 3 Aug 2019 12:53:24 +0200 Subject: [PATCH] Closes #3775 --- ChangeLog-8.4.md | 4 ++ src/Framework/TestCase.php | 64 +++++++++++++++++++++++++++ tests/unit/Framework/TestCaseTest.php | 36 +++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/ChangeLog-8.4.md b/ChangeLog-8.4.md index 9ec720f4975..19055436f5d 100644 --- a/ChangeLog-8.4.md +++ b/ChangeLog-8.4.md @@ -4,5 +4,9 @@ All notable changes of the PHPUnit 8.4 release series are documented in this fil ## [8.4.0] - 2019-10-04 +### Added + +* Implemented [#3775](https://github.com/sebastianbergmann/phpunit/issues/3775): Explicit API for expecting PHP errors, warnings, and notices + [8.4.0]: https://github.com/sebastianbergmann/phpunit/compare/8.3...8.4.0 diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index eb252f5c658..66e0aefa50a 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -14,6 +14,10 @@ use PHPUnit\Framework\Constraint\ExceptionCode; use PHPUnit\Framework\Constraint\ExceptionMessage; use PHPUnit\Framework\Constraint\ExceptionMessageRegularExpression; +use PHPUnit\Framework\Error\Deprecated; +use PHPUnit\Framework\Error\Error; +use PHPUnit\Framework\Error\Notice; +use PHPUnit\Framework\Error\Warning as WarningError; use PHPUnit\Framework\MockObject\Generator as MockGenerator; use PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount as AnyInvokedCountMatcher; use PHPUnit\Framework\MockObject\Matcher\InvokedAtIndex as InvokedAtIndexMatcher; @@ -511,6 +515,66 @@ public function expectNotToPerformAssertions(): void $this->doesNotPerformAssertions = true; } + public function expectDeprecation(): void + { + $this->expectException(Deprecated::class); + } + + public function expectDeprecationMessage(string $message): void + { + $this->expectExceptionMessage($message); + } + + public function expectDeprecationMessageMatches(string $regularExpression): void + { + $this->expectExceptionMessageRegExp($regularExpression); + } + + public function expectNotice(): void + { + $this->expectException(Notice::class); + } + + public function expectNoticeMessage(string $message): void + { + $this->expectExceptionMessage($message); + } + + public function expectNoticeMessageMatches(string $regularExpression): void + { + $this->expectExceptionMessageRegExp($regularExpression); + } + + public function expectWarning(): void + { + $this->expectException(WarningError::class); + } + + public function expectWarningMessage(string $message): void + { + $this->expectExceptionMessage($message); + } + + public function expectWarningMessageMatches(string $regularExpression): void + { + $this->expectExceptionMessageRegExp($regularExpression); + } + + public function expectError(): void + { + $this->expectException(Error::class); + } + + public function expectErrorMessage(string $message): void + { + $this->expectExceptionMessage($message); + } + + public function expectErrorMessageMatches(string $regularExpression): void + { + $this->expectExceptionMessageRegExp($regularExpression); + } + public function getStatus(): int { return $this->status; diff --git a/tests/unit/Framework/TestCaseTest.php b/tests/unit/Framework/TestCaseTest.php index fe6e7d087b0..5108d187672 100644 --- a/tests/unit/Framework/TestCaseTest.php +++ b/tests/unit/Framework/TestCaseTest.php @@ -1143,6 +1143,42 @@ public function testHasOutputReturnsTrueWhenTestGeneratesOutput(): void $this->assertTrue($test->hasOutput()); } + public function testDeprecationCanBeExpected(): void + { + $this->expectDeprecation(); + $this->expectDeprecationMessage('foo'); + $this->expectDeprecationMessageMatches('/foo/'); + + \trigger_error('foo', \E_USER_DEPRECATED); + } + + public function testNoticeCanBeExpected(): void + { + $this->expectNotice(); + $this->expectNoticeMessage('foo'); + $this->expectNoticeMessageMatches('/foo/'); + + \trigger_error('foo', \E_USER_NOTICE); + } + + public function testWarningCanBeExpected(): void + { + $this->expectWarning(); + $this->expectWarningMessage('foo'); + $this->expectWarningMessageMatches('/foo/'); + + \trigger_error('foo', \E_USER_WARNING); + } + + public function testErrorCanBeExpected(): void + { + $this->expectError(); + $this->expectErrorMessage('foo'); + $this->expectErrorMessageMatches('/foo/'); + + \trigger_error('foo', \E_USER_ERROR); + } + /** * @return array */