|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +* This file is part of https://github.com/josantonius/php-error-handler repository. |
| 5 | +* |
| 6 | +* (c) Josantonius <[email protected]> |
| 7 | +* |
| 8 | +* For the full copyright and license information, please view the LICENSE |
| 9 | +* file that was distributed with this source code. |
| 10 | +*/ |
| 11 | + |
| 12 | +namespace Josantonius\ErrorHandler\Tests\ErrorHandler; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Josantonius\ErrorHandler\ErrorHandler; |
| 16 | +use Josantonius\ErrorHandler\Exceptions\WrongErrorLevelException; |
| 17 | +use ReflectionClass; |
| 18 | + |
| 19 | +class ConvertToExceptionsExceptTest extends TestCase |
| 20 | +{ |
| 21 | + private array $errorLevels; |
| 22 | + |
| 23 | + private ErrorHandler $errorHandler; |
| 24 | + |
| 25 | + public function setUp(): void |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->errorHandler = new ErrorHandler(); |
| 30 | + |
| 31 | + $this->errorLevels = $this->getPrivateProperty($this->errorHandler, 'errorLevels'); |
| 32 | + } |
| 33 | + |
| 34 | + public function testShouldSetConvertToExceptionsAllErrorsExceptNone(): void |
| 35 | + { |
| 36 | + $this->assertInstanceOf( |
| 37 | + ErrorHandler::class, |
| 38 | + $this->errorHandler->convertToExceptionsExcept() |
| 39 | + ); |
| 40 | + |
| 41 | + $exceptionable = $this->getPrivateProperty($this->errorHandler, 'exceptionable'); |
| 42 | + |
| 43 | + $this->assertSame(array_keys($this->errorLevels), $exceptionable); |
| 44 | + } |
| 45 | + |
| 46 | + public function testShouldSetConvertToExceptionsAllErrorsExceptSome(): void |
| 47 | + { |
| 48 | + $this->assertInstanceOf( |
| 49 | + ErrorHandler::class, |
| 50 | + $this->errorHandler->convertToExceptionsExcept(E_ERROR, E_PARSE) |
| 51 | + ); |
| 52 | + |
| 53 | + unset($this->errorLevels[E_ERROR], $this->errorLevels[E_PARSE]); |
| 54 | + |
| 55 | + $exceptionable = $this->getPrivateProperty($this->errorHandler, 'exceptionable'); |
| 56 | + |
| 57 | + $this->assertSame(array_keys($this->errorLevels), $exceptionable); |
| 58 | + } |
| 59 | + |
| 60 | + public function testShouldFailIfTheErrorLevelPassedToConvertToExceptionsExceptIsWrong(): void |
| 61 | + { |
| 62 | + $this->expectException(WrongErrorLevelException::class); |
| 63 | + |
| 64 | + $this->errorHandler->convertToExceptionsExcept(E_WARNING, 101200); |
| 65 | + } |
| 66 | + |
| 67 | + private function getPrivateProperty(ErrorHandler $object, string $property): mixed |
| 68 | + { |
| 69 | + $reflection = new ReflectionClass($object); |
| 70 | + |
| 71 | + $reflectionProperty = $reflection->getProperty($property); |
| 72 | + $reflectionProperty->setAccessible(true); |
| 73 | + return $reflectionProperty->getValue($object); |
| 74 | + } |
| 75 | +} |
0 commit comments