|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Bridge\PhpUnit\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\ExpectationFailedException; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Symfony\Bridge\PhpUnit\AssertDeprecationTrait; |
| 8 | + |
| 9 | +final class AssertDeprecationTraitTest extends TestCase |
| 10 | +{ |
| 11 | + public function testExpectedDeprecationHasBeenRaised() |
| 12 | + { |
| 13 | + $test = new class() { |
| 14 | + use AssertDeprecationTrait; |
| 15 | + |
| 16 | + public function run(): string |
| 17 | + { |
| 18 | + return self::assertDeprecation( |
| 19 | + 'Since foo/bar 47.11: Stop using this.', |
| 20 | + static function (): string { |
| 21 | + trigger_deprecation('foo/bar', '47.11', 'Stop using this.'); |
| 22 | + |
| 23 | + return 'foo'; |
| 24 | + } |
| 25 | + ); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + self::assertSame('foo', $test->run()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testExpectedDeprecationHasBeenRaisedAmongOthers() |
| 33 | + { |
| 34 | + $test = new class() { |
| 35 | + use AssertDeprecationTrait; |
| 36 | + |
| 37 | + public function run(): string |
| 38 | + { |
| 39 | + return self::assertDeprecation( |
| 40 | + 'Since foo/bar 47.11: Stop using this.', |
| 41 | + static function (): string { |
| 42 | + trigger_deprecation('fuz/baz', '0.8.15', 'Ignore me.'); |
| 43 | + trigger_deprecation('foo/bar', '47.11', 'Stop using this.'); |
| 44 | + |
| 45 | + return 'foo'; |
| 46 | + } |
| 47 | + ); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + $loggedDeprecations = []; |
| 52 | + $previous = null; |
| 53 | + $previous = set_error_handler(static function ($errno, $errstr) use (&$loggedDeprecations, &$previous) { |
| 54 | + if ($errno === E_USER_DEPRECATED) { |
| 55 | + $loggedDeprecations[] = $errstr; |
| 56 | + |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + return $previous(...func_get_args()); |
| 61 | + }) ?? static function () { |
| 62 | + return false; |
| 63 | + }; |
| 64 | + |
| 65 | + try { |
| 66 | + self::assertSame('foo', $test->run()); |
| 67 | + } finally { |
| 68 | + restore_error_handler(); |
| 69 | + } |
| 70 | + |
| 71 | + self::assertSame(['Since fuz/baz 0.8.15: Ignore me.'], $loggedDeprecations); |
| 72 | + } |
| 73 | + |
| 74 | + public function testNoDeprecationHasBeenRaised() |
| 75 | + { |
| 76 | + $test = new class() { |
| 77 | + use AssertDeprecationTrait; |
| 78 | + |
| 79 | + public function run(): string |
| 80 | + { |
| 81 | + return self::assertDeprecation( |
| 82 | + 'Since foo/bar 47.11: Stop using this.', |
| 83 | + static function (): void { |
| 84 | + } |
| 85 | + ); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + $e = null; |
| 90 | + try { |
| 91 | + $test->run(); |
| 92 | + } catch (ExpectationFailedException $e) { |
| 93 | + } |
| 94 | + |
| 95 | + self::assertNotNull($e); |
| 96 | + self::assertSame( |
| 97 | + "The following deprecation has not been raised: Since foo/bar 47.11: Stop using this.\nNo other deprecations have been observed.\nFailed asserting that false is true.", |
| 98 | + $e->getMessage() |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function testOtherDeprecationsHaveBeenRaised() |
| 103 | + { |
| 104 | + $test = new class() { |
| 105 | + use AssertDeprecationTrait; |
| 106 | + |
| 107 | + public function run(): string |
| 108 | + { |
| 109 | + return self::assertDeprecation( |
| 110 | + 'Since foo/bar 47.11: Stop using this.', |
| 111 | + static function (): void { |
| 112 | + trigger_deprecation('fuz/baz', '0.8.15', 'Ignore me.'); |
| 113 | + trigger_deprecation('fiz/buz', '0.8.16', 'And me as well.'); |
| 114 | + } |
| 115 | + ); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + $e = null; |
| 120 | + $loggedDeprecations = []; |
| 121 | + $previous = null; |
| 122 | + $previous = set_error_handler(static function ($errno, $errstr) use (&$loggedDeprecations, &$previous) { |
| 123 | + if ($errno === E_USER_DEPRECATED) { |
| 124 | + $loggedDeprecations[] = $errstr; |
| 125 | + |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + return $previous(...func_get_args()); |
| 130 | + }) ?? static function () { |
| 131 | + return false; |
| 132 | + }; |
| 133 | + try { |
| 134 | + $test->run(); |
| 135 | + } catch (ExpectationFailedException $e) { |
| 136 | + } finally { |
| 137 | + restore_error_handler(); |
| 138 | + } |
| 139 | + |
| 140 | + self::assertNotNull($e); |
| 141 | + self::assertSame( |
| 142 | + "The following deprecation has not been raised: Since foo/bar 47.11: Stop using this.\nInstead, the following deprecations have been observed:\n - Since fuz/baz 0.8.15: Ignore me.\n - Since fiz/buz 0.8.16: And me as well.\nFailed asserting that false is true.", |
| 143 | + $e->getMessage() |
| 144 | + ); |
| 145 | + self::assertSame([ |
| 146 | + 'Since fuz/baz 0.8.15: Ignore me.', |
| 147 | + 'Since fiz/buz 0.8.16: And me as well.', |
| 148 | + ], $loggedDeprecations); |
| 149 | + } |
| 150 | +} |
0 commit comments