1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2015 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Test\Exception;
use Psy\Exception\ErrorException;
use Psy\Exception\Exception;
class ErrorExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testInstance()
{
$e = new ErrorException();
$this->assertTrue($e instanceof Exception);
$this->assertTrue($e instanceof \ErrorException);
$this->assertTrue($e instanceof ErrorException);
}
public function testMessage()
{
$e = new ErrorException('foo');
$this->assertContains('foo', $e->getMessage());
$this->assertEquals('foo', $e->getRawMessage());
}
/**
* @dataProvider getLevels
*/
public function testErrorLevels($level, $type)
{
$e = new ErrorException('foo', 0, $level);
$this->assertContains('PHP ' . $type, $e->getMessage());
}
/**
* @dataProvider getLevels
*/
public function testThrowException($level, $type)
{
try {
ErrorException::throwException($level, '{whot}', '{file}', '13');
} catch (ErrorException $e) {
$this->assertContains('PHP ' . $type, $e->getMessage());
$this->assertContains('{whot}', $e->getMessage());
$this->assertContains('in {file}', $e->getMessage());
$this->assertContains('on line 13', $e->getMessage());
}
}
public function getLevels()
{
return array(
array(E_WARNING, 'warning'),
array(E_CORE_WARNING, 'warning'),
array(E_COMPILE_WARNING, 'warning'),
array(E_USER_WARNING, 'warning'),
array(E_STRICT, 'Strict error'),
array(0, 'error'),
);
}
/**
* @dataProvider getUserLevels
*/
public function testThrowExceptionAsErrorHandler($level, $type)
{
set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
try {
trigger_error('{whot}', $level);
} catch (ErrorException $e) {
$this->assertContains('PHP ' . $type, $e->getMessage());
$this->assertContains('{whot}', $e->getMessage());
}
restore_error_handler();
}
public function getUserLevels()
{
return array(
array(E_USER_ERROR, 'error'),
array(E_USER_WARNING, 'warning'),
array(E_USER_NOTICE, 'error'),
array(E_USER_DEPRECATED, 'error'),
);
}
public function testIgnoreExecutionLoopFilename()
{
$e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/ExecutionLoop/Loop.php');
$this->assertEmpty($e->getFile());
$e = new ErrorException('{{message}}', 0, 1, 'c:\fake\path\to\Psy\ExecutionLoop\Loop.php');
$this->assertEmpty($e->getFile());
$e = new ErrorException('{{message}}', 0, 1, '/fake/path/to/Psy/File.php');
$this->assertNotEmpty($e->getFile());
}
}