|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Config\Repository as Config; |
| 4 | +use Illuminate\Container\Container; |
| 5 | +use Illuminate\Log\LogManager; |
| 6 | +use Laravel\Lumen\Concerns\RegistersExceptionHandlers; |
| 7 | +use Mockery as m; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class HandleExceptionsTest extends TestCase |
| 11 | +{ |
| 12 | + use RegistersExceptionHandlers; |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + $this->container = new Container; |
| 17 | + |
| 18 | + $this->config = new Config(); |
| 19 | + |
| 20 | + $this->container->singleton('config', function () { |
| 21 | + return $this->config; |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + protected function tearDown(): void |
| 26 | + { |
| 27 | + $this->container::setInstance(null); |
| 28 | + |
| 29 | + m::close(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testPhpDeprecations() |
| 33 | + { |
| 34 | + $logger = m::mock(LogManager::class); |
| 35 | + $this->container->instance('log', $logger); |
| 36 | + $logger->shouldReceive('channel')->with('deprecations')->andReturnSelf(); |
| 37 | + $logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s', |
| 38 | + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', |
| 39 | + '/home/user/laravel/routes/web.php', |
| 40 | + 17 |
| 41 | + )); |
| 42 | + |
| 43 | + $this->handleError( |
| 44 | + E_DEPRECATED, |
| 45 | + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', |
| 46 | + '/home/user/laravel/routes/web.php', |
| 47 | + 17 |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function testUserDeprecations() |
| 52 | + { |
| 53 | + $logger = m::mock(LogManager::class); |
| 54 | + $this->container->instance('log', $logger); |
| 55 | + $logger->shouldReceive('channel')->with('deprecations')->andReturnSelf(); |
| 56 | + $logger->shouldReceive('warning')->with(sprintf('%s in %s on line %s', |
| 57 | + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', |
| 58 | + '/home/user/laravel/routes/web.php', |
| 59 | + 17 |
| 60 | + )); |
| 61 | + |
| 62 | + $this->handleError( |
| 63 | + E_USER_DEPRECATED, |
| 64 | + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', |
| 65 | + '/home/user/laravel/routes/web.php', |
| 66 | + 17 |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function testErrors() |
| 71 | + { |
| 72 | + $logger = m::mock(LogManager::class); |
| 73 | + $this->container->instance('log', $logger); |
| 74 | + $logger->shouldNotReceive('channel'); |
| 75 | + $logger->shouldNotReceive('warning'); |
| 76 | + |
| 77 | + $this->expectException(ErrorException::class); |
| 78 | + $this->expectExceptionMessage('Something went wrong'); |
| 79 | + |
| 80 | + $this->handleError( |
| 81 | + E_ERROR, |
| 82 | + 'Something went wrong', |
| 83 | + '/home/user/laravel/src/Providers/AppServiceProvider.php', |
| 84 | + 17 |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + public function testEnsuresDeprecationsDriver() |
| 89 | + { |
| 90 | + $logger = m::mock(LogManager::class); |
| 91 | + $this->container->instance('log', $logger); |
| 92 | + $logger->shouldReceive('channel')->andReturnSelf(); |
| 93 | + $logger->shouldReceive('warning'); |
| 94 | + |
| 95 | + $this->config->set('logging.channels.stack', [ |
| 96 | + 'driver' => 'stack', |
| 97 | + 'channels' => ['single'], |
| 98 | + 'ignore_exceptions' => false, |
| 99 | + ]); |
| 100 | + $this->config->set('logging.deprecations', 'stack'); |
| 101 | + |
| 102 | + $this->handleError( |
| 103 | + E_USER_DEPRECATED, |
| 104 | + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', |
| 105 | + '/home/user/laravel/routes/web.php', |
| 106 | + 17 |
| 107 | + ); |
| 108 | + |
| 109 | + $this->assertEquals( |
| 110 | + [ |
| 111 | + 'driver' => 'stack', |
| 112 | + 'channels' => ['single'], |
| 113 | + 'ignore_exceptions' => false, |
| 114 | + ], |
| 115 | + $this->config->get('logging.channels.deprecations') |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + public function testEnsuresNullDeprecationsDriver() |
| 120 | + { |
| 121 | + $logger = m::mock(LogManager::class); |
| 122 | + $this->container->instance('log', $logger); |
| 123 | + $logger->shouldReceive('channel')->andReturnSelf(); |
| 124 | + $logger->shouldReceive('warning'); |
| 125 | + |
| 126 | + $this->config->set('logging.channels.null', [ |
| 127 | + 'driver' => 'monolog', |
| 128 | + 'handler' => NullHandler::class, |
| 129 | + ]); |
| 130 | + |
| 131 | + $this->handleError( |
| 132 | + E_USER_DEPRECATED, |
| 133 | + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', |
| 134 | + '/home/user/laravel/routes/web.php', |
| 135 | + 17 |
| 136 | + ); |
| 137 | + |
| 138 | + $this->assertEquals( |
| 139 | + NullHandler::class, |
| 140 | + $this->config->get('logging.channels.deprecations.handler') |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + public function testNoDeprecationsDriverIfNoDeprecationsHereSend() |
| 145 | + { |
| 146 | + $this->assertEquals(null, $this->config->get('logging.deprecations')); |
| 147 | + $this->assertEquals(null, $this->config->get('logging.channels.deprecations')); |
| 148 | + } |
| 149 | + |
| 150 | + public function testIgnoreDeprecationIfLoggerUnresolvable() |
| 151 | + { |
| 152 | + $this->handleError( |
| 153 | + E_DEPRECATED, |
| 154 | + 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated', |
| 155 | + '/home/user/laravel/routes/web.php', |
| 156 | + 17 |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + protected function make($abstract, array $parameters = []) |
| 161 | + { |
| 162 | + return $this->container->make($abstract, $parameters); |
| 163 | + } |
| 164 | +} |
0 commit comments