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