Skip to content

Commit 5681b6d

Browse files
committed
test: main upload of v2.0.0 version
Closes #4
1 parent 30957fb commit 5681b6d

10 files changed

+615
-0
lines changed

tests/ErrorExceptionTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\ErrorHandled;
16+
use Josantonius\ErrorHandler\ErrorException;
17+
18+
class ErrorExceptionTest extends TestCase
19+
{
20+
private ErrorException $errorException;
21+
22+
public function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
$errorHandled = new ErrorHandled(
27+
E_ERROR,
28+
'Error message',
29+
'Error.php',
30+
8,
31+
'Error'
32+
);
33+
34+
$this->errorException = new ErrorException($errorHandled);
35+
}
36+
37+
public function testShouldGetFile(): void
38+
{
39+
$this->assertEquals('Error.php', $this->errorException->getFile());
40+
}
41+
42+
public function testShouldGetMessage(): void
43+
{
44+
$this->assertEquals('Error message', $this->errorException->getMessage());
45+
}
46+
47+
public function testShouldGetLevel(): void
48+
{
49+
$this->assertEquals(E_ERROR, $this->errorException->getLevel());
50+
}
51+
52+
public function testShouldGetLine(): void
53+
{
54+
$this->assertEquals(8, $this->errorException->getLine());
55+
}
56+
57+
public function testShouldGetName(): void
58+
{
59+
$this->assertEquals('Error', $this->errorException->getName());
60+
}
61+
}

tests/ErrorHandledTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\ErrorHandled;
16+
17+
class ErrorHandledTest extends TestCase
18+
{
19+
private ErrorHandled $errorHandled;
20+
21+
public function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->errorHandled = new ErrorHandled(
26+
E_ERROR,
27+
'Error message',
28+
'Error.php',
29+
8,
30+
'Error'
31+
);
32+
}
33+
34+
public function testShouldGetFile(): void
35+
{
36+
$this->assertEquals('Error.php', $this->errorHandled->getFile());
37+
}
38+
39+
public function testShouldGetMessage(): void
40+
{
41+
$this->assertEquals('Error message', $this->errorHandled->getMessage());
42+
}
43+
44+
public function testShouldGetLevel(): void
45+
{
46+
$this->assertEquals(E_ERROR, $this->errorHandled->getLevel());
47+
}
48+
49+
public function testShouldGetLine(): void
50+
{
51+
$this->assertEquals(8, $this->errorHandled->getLine());
52+
}
53+
54+
public function testShouldGetName(): void
55+
{
56+
$this->assertEquals('Error', $this->errorHandled->getName());
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 ReflectionClass;
15+
use PHPUnit\Framework\TestCase;
16+
use Josantonius\ErrorHandler\ErrorHandler;
17+
use Josantonius\ErrorHandler\Tests\ErrorHandler\Resources\Handler;
18+
use Josantonius\ErrorHandler\Tests\ErrorHandler\Resources\History;
19+
20+
class CheckForShutdownErrorsTest extends TestCase
21+
{
22+
private Handler $handler;
23+
24+
private ErrorHandler $errorHandler;
25+
26+
public function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
$this->handler = new Handler();
31+
32+
$this->errorHandler = new ErrorHandler();
33+
}
34+
35+
public function testShouldSendTheHandlerAShutdownError(): void
36+
{
37+
$this->errorHandler->register($this->handler->init(...));
38+
39+
History::clear();
40+
41+
$this->simulateShutdown(E_ERROR);
42+
43+
$this->assertCount(1, History::get());
44+
$this->assertEquals(E_ERROR, History::get(0)->errorHandled->getLevel());
45+
}
46+
47+
public function testShouldIgnoreAnythingOtherThanAShutdownError(): void
48+
{
49+
$this->errorHandler->register($this->handler->init(...));
50+
51+
History::clear();
52+
53+
$this->simulateShutdown(E_WARNING);
54+
55+
$this->assertEmpty(History::get());
56+
}
57+
58+
private function simulateShutdown(int $errorLevel): void
59+
{
60+
$reflection = new ReflectionClass($this->errorHandler);
61+
$reflection = $reflection->getMethod('checkForShutdownErrors');
62+
$reflection->setAccessible(true);
63+
$reflection->invoke($this->errorHandler, [
64+
'type' => $errorLevel,
65+
'message' => 'Error',
66+
'file' => '',
67+
'line' => 0
68+
]);
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 ConvertToExceptionsTest 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 testShouldSetConvertToExceptionsAllErrors(): void
35+
{
36+
$this->assertInstanceOf(ErrorHandler::class, $this->errorHandler->convertToExceptions());
37+
38+
$exceptionable = $this->getPrivateProperty($this->errorHandler, 'exceptionable');
39+
40+
$this->assertSame(array_keys($this->errorLevels), $exceptionable);
41+
}
42+
43+
public function testShouldSetConvertToExceptionsSomeErrors(): void
44+
{
45+
$this->assertInstanceOf(
46+
ErrorHandler::class,
47+
$this->errorHandler->convertToExceptions(E_WARNING, E_NOTICE)
48+
);
49+
50+
$exceptionable = $this->getPrivateProperty($this->errorHandler, 'exceptionable');
51+
52+
$this->assertSame([E_WARNING, E_NOTICE], $exceptionable);
53+
}
54+
55+
public function testShouldFailIfTheErrorLevelPassedToConvertToExceptionsIsWrong(): void
56+
{
57+
$this->expectException(WrongErrorLevelException::class);
58+
59+
$this->errorHandler->convertToExceptions(E_WARNING, 101200);
60+
}
61+
62+
private function getPrivateProperty(ErrorHandler $object, string $property): mixed
63+
{
64+
$reflection = new ReflectionClass($object);
65+
66+
$reflectionProperty = $reflection->getProperty($property);
67+
$reflectionProperty->setAccessible(true);
68+
return $reflectionProperty->getValue($object);
69+
}
70+
}

0 commit comments

Comments
 (0)