From a2c784c39bf5741caf436f92fc8c5bfb91bac025 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Thu, 8 Sep 2022 16:45:47 +0200 Subject: [PATCH] Remove expectDeprecation*(), expectError*(), expectNotice*(), and expectWarning*() --- .psalm/baseline.xml | 24 +- ChangeLog-10.0.md | 4 + src/Framework/TestCase.php | 284 +----------------- src/Runner/TestResult/Collector.php | 93 +----- src/Runner/TestResult/Facade.php | 41 --- tests/end-to-end/regression/4663.phpt | 21 -- .../regression/4663/Issue4663Test.php | 23 -- tests/unit/Framework/TestCaseTest.php | 41 --- 8 files changed, 16 insertions(+), 515 deletions(-) delete mode 100644 tests/end-to-end/regression/4663.phpt delete mode 100644 tests/end-to-end/regression/4663/Issue4663Test.php diff --git a/.psalm/baseline.xml b/.psalm/baseline.xml index ca5ae1b0930..43330886c56 100644 --- a/.psalm/baseline.xml +++ b/.psalm/baseline.xml @@ -328,19 +328,7 @@ MockObject&RealInstanceType - - Facade::ignoredEvent() - Facade::ignoredEvent() - Facade::ignoredEvent() - Facade::ignoredEvent() - assertThat - assertThat - assertThat - assertThat - assertThat - assertThat - assertThat - assertThat + assertThat assertThat assertThat @@ -354,17 +342,7 @@ getMockForTrait getObjectForTrait onlyMethods - throw new AssertionFailedError('Failed asserting that a deprecation is triggered'); - throw new AssertionFailedError('Failed asserting that a notice is triggered'); - throw new AssertionFailedError('Failed asserting that a warning is triggered'); - throw new AssertionFailedError('Failed asserting that an error is triggered'); - - $this->expectedDeprecationMessage - $this->expectedErrorMessage - $this->expectedNoticeMessage - $this->expectedWarningMessage - $outputBufferingLevel diff --git a/ChangeLog-10.0.md b/ChangeLog-10.0.md index 9111560160c..37d001fde7f 100644 --- a/ChangeLog-10.0.md +++ b/ChangeLog-10.0.md @@ -77,6 +77,10 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi * [#4564](https://github.com/sebastianbergmann/phpunit/issues/4564): Remove `withConsecutive()` * [#4567](https://github.com/sebastianbergmann/phpunit/issues/4567): Remove support for generators in `assertCount()` and `Count` constraint * [#4601](https://github.com/sebastianbergmann/phpunit/issues/4601): Remove assertions that operate on class/object properties +* Removed the `expectDeprecation()`, `expectDeprecationMessage()`, and `expectDeprecationMessageMatches()` methods +* Removed the `expectError()`, `expectErrorMessage()`, and `expectErrorMessageMatches()` methods +* Removed the `expectNotice()`, `expectNoticeMessage()`, and `expectNoticeMessageMatches()` methods +* Removed the `expectWarning()`, `expectWarningMessage()`, and `expectWarningMessageMatches()` methods * Removed the `PHPUnit\Runner\TestSuiteLoader` interface * Removed the `` XML configuration element and its children * Removed the `groups` attribute on the `` element in the TestDox XML report diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index d549342ff78..b6b75681e99 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -58,13 +58,6 @@ use AssertionError; use DeepCopy\DeepCopy; use PHPUnit\Event; -use PHPUnit\Event\Test\DeprecationTriggered; -use PHPUnit\Event\Test\ErrorTriggered; -use PHPUnit\Event\Test\NoticeTriggered; -use PHPUnit\Event\Test\PhpDeprecationTriggered; -use PHPUnit\Event\Test\PhpNoticeTriggered; -use PHPUnit\Event\Test\PhpWarningTriggered; -use PHPUnit\Event\Test\WarningTriggered; use PHPUnit\Framework\Constraint\Exception as ExceptionConstraint; use PHPUnit\Framework\Constraint\ExceptionCode; use PHPUnit\Framework\Constraint\MessageIsOrContains; @@ -183,21 +176,9 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T /** * @psalm-var list */ - private array $customComparators = []; - private ?Event\Code\TestMethod $testValueObjectForEvents = null; - private bool $wasPrepared = false; - private bool $deprecationExpected = false; - private ?string $expectedDeprecationMessage = null; - private ?string $expectedDeprecationMessageRegularExpression = null; - private bool $errorExpected = false; - private ?string $expectedErrorMessage = null; - private ?string $expectedErrorMessageRegularExpression = null; - private bool $noticeExpected = false; - private ?string $expectedNoticeMessage = null; - private ?string $expectedNoticeMessageRegularExpression = null; - private bool $warningExpected = false; - private ?string $expectedWarningMessage = null; - private ?string $expectedWarningMessageRegularExpression = null; + private array $customComparators = []; + private ?Event\Code\TestMethod $testValueObjectForEvents = null; + private bool $wasPrepared = false; /** * Returns a matcher that matches when the method is executed @@ -435,98 +416,6 @@ final public function expectNotToPerformAssertions(): void $this->doesNotPerformAssertions = true; } - final public function expectDeprecation(): void - { - Facade::ignoreTestTriggeredDeprecationEventForExpectation(); - - $this->deprecationExpected = true; - } - - final public function expectDeprecationMessage(string $message): void - { - Facade::ignoreTestTriggeredDeprecationEventForExpectation(); - - $this->deprecationExpected = true; - $this->expectedDeprecationMessage = $message; - } - - final public function expectDeprecationMessageMatches(string $regularExpression): void - { - Facade::ignoreTestTriggeredDeprecationEventForExpectation(); - - $this->deprecationExpected = true; - $this->expectedDeprecationMessageRegularExpression = $regularExpression; - } - - final public function expectError(): void - { - Facade::ignoreTestTriggeredErrorEventForExpectation(); - - $this->errorExpected = true; - } - - final public function expectErrorMessage(string $message): void - { - Facade::ignoreTestTriggeredErrorEventForExpectation(); - - $this->errorExpected = true; - $this->expectedErrorMessage = $message; - } - - final public function expectErrorMessageMatches(string $regularExpression): void - { - Facade::ignoreTestTriggeredErrorEventForExpectation(); - - $this->errorExpected = true; - $this->expectedErrorMessageRegularExpression = $regularExpression; - } - - final public function expectNotice(): void - { - Facade::ignoreTestTriggeredNoticeEventForExpectation(); - - $this->noticeExpected = true; - } - - final public function expectNoticeMessage(string $message): void - { - Facade::ignoreTestTriggeredNoticeEventForExpectation(); - - $this->noticeExpected = true; - $this->expectedNoticeMessage = $message; - } - - final public function expectNoticeMessageMatches(string $regularExpression): void - { - Facade::ignoreTestTriggeredNoticeEventForExpectation(); - - $this->noticeExpected = true; - $this->expectedNoticeMessageRegularExpression = $regularExpression; - } - - final public function expectWarning(): void - { - Facade::ignoreTestTriggeredWarningEventForExpectation(); - - $this->warningExpected = true; - } - - final public function expectWarningMessage(string $message): void - { - Facade::ignoreTestTriggeredWarningEventForExpectation(); - - $this->warningExpected = true; - $this->expectedWarningMessage = $message; - } - - final public function expectWarningMessageMatches(string $regularExpression): void - { - Facade::ignoreTestTriggeredWarningEventForExpectation(); - - $this->warningExpected = true; - $this->expectedWarningMessageRegularExpression = $regularExpression; - } - final public function status(): TestStatus { return $this->status; @@ -745,22 +634,15 @@ final public function runBare(): void } catch (TimeoutException $e) { $this->status = TestStatus::risky($e->getMessage()); } catch (Throwable $_e) { - if (!$this->errorExpected || $_e::class !== 'Error') { - $e = $_e; - $this->status = TestStatus::error($_e->getMessage()); + $e = $_e; + $this->status = TestStatus::error($_e->getMessage()); - $emitter->testErrored( - $this->valueObjectForEvents(), - Event\Code\Throwable::from($_e) - ); - } + $emitter->testErrored( + $this->valueObjectForEvents(), + Event\Code\Throwable::from($_e) + ); } - $this->verifyDeprecationExpectations(); - $this->verifyErrorExpectations(); - $this->verifyNoticeExpectations(); - $this->verifyWarningExpectations(); - if ($this->stopOutputBuffering() && !isset($e)) { $this->performAssertionsOnOutput(); } @@ -2137,152 +2019,4 @@ private function expectedExceptionWasNotRaised(): void ); } } - - private function verifyDeprecationExpectations(): void - { - if (!$this->deprecationExpected) { - return; - } - - $this->numberOfAssertionsPerformed++; - - if (!Facade::hasIgnoredEvent()) { - throw new AssertionFailedError('Failed asserting that a deprecation is triggered'); - } - - $event = Facade::ignoredEvent(); - - assert($event instanceof DeprecationTriggered || $event instanceof PhpDeprecationTriggered); - - if ($this->expectedDeprecationMessage !== null) { - $this->assertThat( - $event->message(), - new MessageIsOrContains( - 'deprecation', - $this->expectedDeprecationMessage - ) - ); - } - - if ($this->expectedDeprecationMessageRegularExpression !== null) { - $this->assertThat( - $event->message(), - new MessageMatchesRegularExpression( - 'deprecation', - $this->expectedDeprecationMessage - ) - ); - } - } - - private function verifyErrorExpectations(): void - { - if (!$this->errorExpected) { - return; - } - - $this->numberOfAssertionsPerformed++; - - if (!Facade::hasIgnoredEvent()) { - throw new AssertionFailedError('Failed asserting that an error is triggered'); - } - - $event = Facade::ignoredEvent(); - - assert($event instanceof ErrorTriggered); - - if ($this->expectedErrorMessage !== null) { - $this->assertThat( - $event->message(), - new MessageIsOrContains( - 'error', - $this->expectedErrorMessage - ) - ); - } - - if ($this->expectedErrorMessageRegularExpression !== null) { - $this->assertThat( - $event->message(), - new MessageMatchesRegularExpression( - 'error', - $this->expectedErrorMessage - ) - ); - } - } - - private function verifyNoticeExpectations(): void - { - if (!$this->noticeExpected) { - return; - } - - $this->numberOfAssertionsPerformed++; - - if (!Facade::hasIgnoredEvent()) { - throw new AssertionFailedError('Failed asserting that a notice is triggered'); - } - - $event = Facade::ignoredEvent(); - - assert($event instanceof NoticeTriggered || $event instanceof PhpNoticeTriggered); - - if ($this->expectedNoticeMessage !== null) { - $this->assertThat( - $event->message(), - new MessageIsOrContains( - 'notice', - $this->expectedNoticeMessage - ) - ); - } - - if ($this->expectedNoticeMessageRegularExpression !== null) { - $this->assertThat( - $event->message(), - new MessageMatchesRegularExpression( - 'notice', - $this->expectedNoticeMessage - ) - ); - } - } - - private function verifyWarningExpectations(): void - { - if (!$this->warningExpected) { - return; - } - - $this->numberOfAssertionsPerformed++; - - if (!Facade::hasIgnoredEvent()) { - throw new AssertionFailedError('Failed asserting that a warning is triggered'); - } - - $event = Facade::ignoredEvent(); - - assert($event instanceof WarningTriggered || $event instanceof PhpWarningTriggered); - - if ($this->expectedWarningMessage !== null) { - $this->assertThat( - $event->message(), - new MessageIsOrContains( - 'warning', - $this->expectedWarningMessage - ) - ); - } - - if ($this->expectedWarningMessageRegularExpression !== null) { - $this->assertThat( - $event->message(), - new MessageMatchesRegularExpression( - 'warning', - $this->expectedWarningMessage - ) - ); - } - } } diff --git a/src/Runner/TestResult/Collector.php b/src/Runner/TestResult/Collector.php index 02c11d4644a..82de3f8df84 100644 --- a/src/Runner/TestResult/Collector.php +++ b/src/Runner/TestResult/Collector.php @@ -40,7 +40,6 @@ use PHPUnit\Event\UnknownSubscriberTypeException; use PHPUnit\Framework\TestSize\TestSize; use PHPUnit\Metadata\Api\Groups; -use PHPUnit\Runner\NoIgnoredEventException; /** * @internal This class is not covered by the backward compatibility promise for PHPUnit @@ -146,12 +145,7 @@ final class Collector /** * @psalm-var list */ - private array $testRunnerTriggeredDeprecationEvents = []; - private bool $ignoreTestTriggeredDeprecationEventForExpectation = false; - private bool $ignoreTestTriggeredErrorEventForExpectation = false; - private bool $ignoreTestTriggeredNoticeEventForExpectation = false; - private bool $ignoreTestTriggeredWarningEventForExpectation = false; - private DeprecationTriggered|PhpDeprecationTriggered|ErrorTriggered|NoticeTriggered|PhpNoticeTriggered|WarningTriggered|PhpWarningTriggered|null $ignoredEvent = null; + private array $testRunnerTriggeredDeprecationEvents = []; /** * @throws EventFacadeIsSealedException @@ -309,11 +303,7 @@ public function testFinished(Finished $event): void $this->numberOfTestsRun++; - $this->prepared = false; - $this->ignoreTestTriggeredDeprecationEventForExpectation = false; - $this->ignoreTestTriggeredErrorEventForExpectation = false; - $this->ignoreTestTriggeredNoticeEventForExpectation = false; - $this->ignoreTestTriggeredWarningEventForExpectation = false; + $this->prepared = false; } public function beforeTestClassMethodErrored(BeforeFirstTestMethodErrored $event): void @@ -394,12 +384,6 @@ public function testConsideredRisky(ConsideredRisky $event): void public function testTriggeredDeprecation(DeprecationTriggered $event): void { - if ($this->ignoreTestTriggeredDeprecationEventForExpectation) { - $this->ignoredEvent = $event; - - return; - } - if (!isset($this->testTriggeredDeprecationEvents[$event->test()->id()])) { $this->testTriggeredDeprecationEvents[$event->test()->id()] = []; } @@ -409,12 +393,6 @@ public function testTriggeredDeprecation(DeprecationTriggered $event): void public function testTriggeredPhpDeprecation(PhpDeprecationTriggered $event): void { - if ($this->ignoreTestTriggeredDeprecationEventForExpectation) { - $this->ignoredEvent = $event; - - return; - } - if (!isset($this->testTriggeredPhpDeprecationEvents[$event->test()->id()])) { $this->testTriggeredPhpDeprecationEvents[$event->test()->id()] = []; } @@ -433,12 +411,6 @@ public function testTriggeredPhpunitDeprecation(PhpunitDeprecationTriggered $eve public function testTriggeredError(ErrorTriggered $event): void { - if ($this->ignoreTestTriggeredErrorEventForExpectation) { - $this->ignoredEvent = $event; - - return; - } - if (!isset($this->testTriggeredErrorEvents[$event->test()->id()])) { $this->testTriggeredErrorEvents[$event->test()->id()] = []; } @@ -448,12 +420,6 @@ public function testTriggeredError(ErrorTriggered $event): void public function testTriggeredNotice(NoticeTriggered $event): void { - if ($this->ignoreTestTriggeredNoticeEventForExpectation) { - $this->ignoredEvent = $event; - - return; - } - if (!isset($this->testTriggeredNoticeEvents[$event->test()->id()])) { $this->testTriggeredNoticeEvents[$event->test()->id()] = []; } @@ -463,12 +429,6 @@ public function testTriggeredNotice(NoticeTriggered $event): void public function testTriggeredPhpNotice(PhpNoticeTriggered $event): void { - if ($this->ignoreTestTriggeredNoticeEventForExpectation) { - $this->ignoredEvent = $event; - - return; - } - if (!isset($this->testTriggeredPhpNoticeEvents[$event->test()->id()])) { $this->testTriggeredPhpNoticeEvents[$event->test()->id()] = []; } @@ -478,12 +438,6 @@ public function testTriggeredPhpNotice(PhpNoticeTriggered $event): void public function testTriggeredWarning(WarningTriggered $event): void { - if ($this->ignoreTestTriggeredWarningEventForExpectation) { - $this->ignoredEvent = $event; - - return; - } - if (!isset($this->testTriggeredWarningEvents[$event->test()->id()])) { $this->testTriggeredWarningEvents[$event->test()->id()] = []; } @@ -493,12 +447,6 @@ public function testTriggeredWarning(WarningTriggered $event): void public function testTriggeredPhpWarning(PhpWarningTriggered $event): void { - if ($this->ignoreTestTriggeredWarningEventForExpectation) { - $this->ignoredEvent = $event; - - return; - } - if (!isset($this->testTriggeredPhpWarningEvents[$event->test()->id()])) { $this->testTriggeredPhpWarningEvents[$event->test()->id()] = []; } @@ -541,41 +489,4 @@ public function hasWarningEvents(): bool !empty($this->testTriggeredPhpunitWarningEvents) || !empty($this->testRunnerTriggeredWarningEvents); } - - public function ignoreTestTriggeredDeprecationEventForExpectation(): void - { - $this->ignoreTestTriggeredDeprecationEventForExpectation = true; - } - - public function ignoreTestTriggeredErrorEventForExpectation(): void - { - $this->ignoreTestTriggeredErrorEventForExpectation = true; - } - - public function ignoreTestTriggeredNoticeEventForExpectation(): void - { - $this->ignoreTestTriggeredNoticeEventForExpectation = true; - } - - public function ignoreTestTriggeredWarningEventForExpectation(): void - { - $this->ignoreTestTriggeredWarningEventForExpectation = true; - } - - public function hasIgnoredEvent(): bool - { - return $this->ignoredEvent !== null; - } - - /** - * @throws NoIgnoredEventException - */ - public function ignoredEvent(): DeprecationTriggered|PhpDeprecationTriggered|ErrorTriggered|NoticeTriggered|PhpNoticeTriggered|WarningTriggered|PhpWarningTriggered - { - if ($this->ignoredEvent === null) { - throw new NoIgnoredEventException; - } - - return $this->ignoredEvent; - } } diff --git a/src/Runner/TestResult/Facade.php b/src/Runner/TestResult/Facade.php index 6af6d76d373..5c3f67199fe 100644 --- a/src/Runner/TestResult/Facade.php +++ b/src/Runner/TestResult/Facade.php @@ -9,15 +9,7 @@ */ namespace PHPUnit\TestRunner\TestResult; -use PHPUnit\Event\Test\DeprecationTriggered; -use PHPUnit\Event\Test\ErrorTriggered; -use PHPUnit\Event\Test\NoticeTriggered; -use PHPUnit\Event\Test\PhpDeprecationTriggered; -use PHPUnit\Event\Test\PhpNoticeTriggered; -use PHPUnit\Event\Test\PhpWarningTriggered; -use PHPUnit\Event\Test\WarningTriggered; use PHPUnit\Framework\TestSize\TestSize; -use PHPUnit\Runner\NoIgnoredEventException; /** * @internal This class is not covered by the backward compatibility promise for PHPUnit @@ -82,39 +74,6 @@ public static function hasTestMarkedIncompleteEvents(): bool return self::collector()->hasTestMarkedIncompleteEvents(); } - public static function ignoreTestTriggeredDeprecationEventForExpectation(): void - { - self::collector()->ignoreTestTriggeredDeprecationEventForExpectation(); - } - - public static function ignoreTestTriggeredErrorEventForExpectation(): void - { - self::collector()->ignoreTestTriggeredErrorEventForExpectation(); - } - - public static function ignoreTestTriggeredNoticeEventForExpectation(): void - { - self::collector()->ignoreTestTriggeredNoticeEventForExpectation(); - } - - public static function ignoreTestTriggeredWarningEventForExpectation(): void - { - self::collector()->ignoreTestTriggeredWarningEventForExpectation(); - } - - public static function hasIgnoredEvent(): bool - { - return self::collector()->hasIgnoredEvent(); - } - - /** - * @throws NoIgnoredEventException - */ - public static function ignoredEvent(): DeprecationTriggered|PhpDeprecationTriggered|ErrorTriggered|NoticeTriggered|PhpNoticeTriggered|WarningTriggered|PhpWarningTriggered - { - return self::collector()->ignoredEvent(); - } - private static function collector(): Collector { if (self::$collector === null) { diff --git a/tests/end-to-end/regression/4663.phpt b/tests/end-to-end/regression/4663.phpt deleted file mode 100644 index 5baf30a9d62..00000000000 --- a/tests/end-to-end/regression/4663.phpt +++ /dev/null @@ -1,21 +0,0 @@ ---TEST-- -https://github.com/sebastianbergmann/phpunit/issues/4663 ---FILE-- - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\TestFixture; - -use PHPUnit\Framework\TestCase; -use stdClass; - -final class Issue4663Test extends TestCase -{ - public function testTestThatExpectsAnErrorPassesWhenTheErrorOccurs(): void - { - $this->expectError(); - - implode('', [new stdClass]); - } -} diff --git a/tests/unit/Framework/TestCaseTest.php b/tests/unit/Framework/TestCaseTest.php index a70a73500c8..e3980f8fc89 100644 --- a/tests/unit/Framework/TestCaseTest.php +++ b/tests/unit/Framework/TestCaseTest.php @@ -9,11 +9,6 @@ */ namespace PHPUnit\Framework; -use const E_USER_DEPRECATED; -use const E_USER_ERROR; -use const E_USER_NOTICE; -use const E_USER_WARNING; -use function trigger_error; use PHPUnit\Framework\Attributes\ExcludeGlobalVariableFromBackup; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Stub; @@ -182,40 +177,4 @@ public function testGetNameReturnsMethodName(): void $this->assertSame($methodName, $testCase->nameWithDataSet()); } - - 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); - } }