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