1
- <?php
2
-
3
- namespace PHPPlatform \tests \Errors \Exceptions ;
4
-
5
- use PhpPlatform \Config \SettingsCache ;
6
- use PhpPlatform \Errors \ErrorHandler ;
7
- use PhpPlatform \Mock \Config \MockSettings ;
8
- use PhpPlatform \Errors \Exceptions \PlatformException ;
9
- use Composer \Autoload \ClassLoader ;
10
-
11
- class TestErrorHandler extends \PHPUnit_Framework_TestCase {
12
-
13
- static $ thisPackageName = 'php-platform/errors ' ;
14
- private $ logFile = "" ;
15
- private $ traceFile = "" ;
16
-
17
- protected function setUp (){
18
- SettingsCache::getInstance ()->reset ();
19
-
20
- $ this ->logFile = tempnam (sys_get_temp_dir (), "MOCK " );
21
- file_put_contents ($ this ->logFile , '' );
22
-
23
- $ this ->traceFile = tempnam (sys_get_temp_dir (), "MOCK " );
24
- file_put_contents ($ this ->traceFile , '' );
25
- }
26
-
27
- protected function tearDown (){
28
- if (is_file ($ this ->logFile )){
29
- unlink ($ this ->logFile );
30
- }
31
- if (is_file ($ this ->traceFile )){
32
- unlink ($ this ->traceFile );
33
- }
34
- }
35
-
36
- /**
37
- *
38
- * @dataProvider catchableExceptionsProvider
39
- *
40
- * @param callable $errorSource
41
- * @param boolean $producesException
42
- * @param string $error_log
43
- */
44
- public function testErrorHandlingForCatchableErrors ($ errorSource ,$ producesException ,$ error_log ){
45
-
46
- ErrorHandler::handleError ();
47
- MockSettings::setSettings (self ::$ thisPackageName , "logs.System " , $ this ->logFile );
48
-
49
- $ isException = false ;
50
- try {
51
- call_user_func ($ errorSource );
52
- }catch (PlatformException $ e ){
53
- $ isException = true ;
54
- }
55
- $ producesException ?parent ::assertTrue ($ isException ):parent ::assertTrue (!$ isException );
56
-
57
-
58
- $ log = file_get_contents ($ this ->logFile );
59
- parent ::assertContains ($ error_log , $ log );
60
-
61
- }
62
-
63
-
64
- public function catchableExceptionsProvider (){
65
- return array (
66
- array (function (){
67
- $ f = fopen ("nonexistingfile " , "r " );
68
- },false ,'[S][E_WARNING] fopen(nonexistingfile): failed to open stream: No such file or directory : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
69
- array (function (){
70
- $ arr = array ("username " =>"myUserName " );
71
- $ arr [username];
72
- },false ,'[S][E_NOTICE] Use of undefined constant username - assumed \'username \' : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
73
- array (function (){
74
- trigger_error ("Trigger Warning " ,E_USER_WARNING );
75
- },false ,'[S][E_USER_WARNING] Trigger Warning : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
76
- array (function (){
77
- trigger_error ("Trigger Deprecated " ,E_USER_DEPRECATED );
78
- },false ,'[S][E_USER_DEPRECATED] Trigger Deprecated : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
79
- array (function (){
80
- trigger_error ("Trigger Error " ,E_USER_ERROR );
81
- },true ,'[S][E_USER_ERROR] Trigger Error : PhpPlatform\Errors\Exceptions\System\SystemError ' ),
82
- array (function (){
83
- trigger_error ("Trigger Notice " );
84
- },false ,'[S][E_USER_NOTICE] Trigger Notice : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
85
- array (function (){
86
- $ recoverable = new \stdClass ();
87
- $ rec = (string )$ recoverable ;
88
- },true ,'[S][E_RECOVERABLE_ERROR] Object of class stdClass could not be converted to string : PhpPlatform\Errors\Exceptions\System\SystemError ' )
89
- );
90
- }
91
-
92
-
93
- /**
94
- * @requires PHP 5.4
95
- * @dataProvider systemExceptionsProvider
96
- */
97
- public function testErrorHandlingForSystemExceptions ($ erroneousSourceCode ,$ expectedError ,$ expectedOutput ){
98
-
99
- $ classLoaderReflection = new \ReflectionClass (new ClassLoader ());
100
- $ vendorDir = dirname (dirname ($ classLoaderReflection ->getFileName ()));
101
- $ autoloadScript = $ vendorDir .'/autoload.php ' ;
102
-
103
- $ phpFileContent = file_get_contents (__DIR__ .'/erroneous-template.inc ' );
104
- $ phpFileContent = str_replace ('$autoloadScript ' , $ autoloadScript , $ phpFileContent );
105
- $ phpFileContent = str_replace ('$thisPackageName ' , self ::$ thisPackageName , $ phpFileContent );
106
- $ phpFileContent = str_replace ('$logFile ' , $ this ->logFile , $ phpFileContent );
107
- $ phpFileContent = str_replace ('$erroneousSourceCode; ' , $ erroneousSourceCode , $ phpFileContent );
108
-
109
- $ phpFileWithErrors = tempnam (sys_get_temp_dir (), "MOCK " );
110
- file_put_contents ($ phpFileWithErrors , $ phpFileContent );
111
-
112
- $ outputFile = tempnam (sys_get_temp_dir (), "ERR " );
113
-
114
- $ output = array ();
115
- $ returnCode = 0 ;
116
- $ command = PHP_BINARY .' -n ' .$ phpFileWithErrors .' > ' .$ outputFile .' 2>&1 ' ;
117
- exec ($ command ,$ output ,$ returnCode );
118
-
119
- $ log = file_get_contents ($ this ->logFile );
120
- parent ::assertContains ($ expectedError , $ log );
121
-
122
- $ output = file_get_contents ($ outputFile );
123
-
124
- parent ::assertEquals ($ expectedOutput ,trim ($ output ));
125
- }
126
-
127
- public function systemExceptionsProvider (){
128
-
129
- $ parseErrorFile = tempnam (sys_get_temp_dir (), "PAR " );
130
- file_put_contents ($ parseErrorFile , "<?php \n noSemicolon() " );
131
-
132
- $ errorUpdateforPHP7 = '[S][E_ERROR] Call to undefined function undefinedFunction() : PhpPlatform\Errors\Exceptions\System\SystemError ' ;
133
- if (strpos (phpversion (), "7. " ) === 0 ){
134
- $ errorUpdateforPHP7 = 'Call to undefined function undefinedFunction() ' ;
135
- }
136
-
137
- return array (
138
- array ('undefinedFunction(); ' ,$ errorUpdateforPHP7 ,'' ),
139
- array ("include_once ' $ parseErrorFile'; " ,'[S][E_PARSE] syntax error, unexpected end of file : PhpPlatform\Errors\Exceptions\System\SystemError ' ,'' )
140
- );
141
- }
142
-
143
-
144
-
145
-
146
-
1
+ <?php
2
+
3
+ namespace PHPPlatform \tests \Errors \Exceptions ;
4
+
5
+ use PhpPlatform \Config \SettingsCache ;
6
+ use PhpPlatform \Errors \ErrorHandler ;
7
+ use PhpPlatform \Mock \Config \MockSettings ;
8
+ use PhpPlatform \Errors \Exceptions \PlatformException ;
9
+ use Composer \Autoload \ClassLoader ;
10
+
11
+ class TestErrorHandler extends \PHPUnit_Framework_TestCase {
12
+
13
+ static $ thisPackageName = 'php-platform/errors ' ;
14
+ private $ logFile = "" ;
15
+ private $ traceFile = "" ;
16
+
17
+ protected function setUp (){
18
+ SettingsCache::getInstance ()->reset ();
19
+
20
+ $ this ->logFile = tempnam (sys_get_temp_dir (), "MOCK " );
21
+ file_put_contents ($ this ->logFile , '' );
22
+
23
+ $ this ->traceFile = tempnam (sys_get_temp_dir (), "MOCK " );
24
+ file_put_contents ($ this ->traceFile , '' );
25
+ }
26
+
27
+ protected function tearDown (){
28
+ if (is_file ($ this ->logFile )){
29
+ unlink ($ this ->logFile );
30
+ }
31
+ if (is_file ($ this ->traceFile )){
32
+ unlink ($ this ->traceFile );
33
+ }
34
+ }
35
+
36
+ /**
37
+ *
38
+ * @dataProvider catchableExceptionsProvider
39
+ *
40
+ * @param callable $errorSource
41
+ * @param boolean $producesException
42
+ * @param string $error_log
43
+ */
44
+ public function testErrorHandlingForCatchableErrors ($ errorSource ,$ producesException ,$ error_log ){
45
+
46
+ ErrorHandler::handleError ();
47
+ MockSettings::setSettings (self ::$ thisPackageName , "logs.System " , $ this ->logFile );
48
+
49
+ $ isException = false ;
50
+ try {
51
+ call_user_func ($ errorSource );
52
+ }catch (PlatformException $ e ){
53
+ $ isException = true ;
54
+ }
55
+ $ producesException ?parent ::assertTrue ($ isException ):parent ::assertTrue (!$ isException );
56
+
57
+
58
+ $ log = file_get_contents ($ this ->logFile );
59
+ parent ::assertContains ($ error_log , $ log );
60
+
61
+ }
62
+
63
+
64
+ public function catchableExceptionsProvider (){
65
+ return array (
66
+ array (function (){
67
+ $ f = fopen ("nonexistingfile " , "r " );
68
+ },false ,'[S][E_WARNING] fopen(nonexistingfile): failed to open stream: No such file or directory : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
69
+ array (function (){
70
+ $ arr = array ("username " =>"myUserName " );
71
+ if ($ i == 1 ){
72
+ echo "success " ;
73
+ }
74
+ },false ,'[S][E_NOTICE] Undefined variable: i : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
75
+ array (function (){
76
+ trigger_error ("Trigger Warning " ,E_USER_WARNING );
77
+ },false ,'[S][E_USER_WARNING] Trigger Warning : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
78
+ array (function (){
79
+ trigger_error ("Trigger Deprecated " ,E_USER_DEPRECATED );
80
+ },false ,'[S][E_USER_DEPRECATED] Trigger Deprecated : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
81
+ array (function (){
82
+ trigger_error ("Trigger Error " ,E_USER_ERROR );
83
+ },true ,'[S][E_USER_ERROR] Trigger Error : PhpPlatform\Errors\Exceptions\System\SystemError ' ),
84
+ array (function (){
85
+ trigger_error ("Trigger Notice " );
86
+ },false ,'[S][E_USER_NOTICE] Trigger Notice : PhpPlatform\Errors\Exceptions\System\SystemWarning ' ),
87
+ array (function (){
88
+ $ recoverable = new \stdClass ();
89
+ $ rec = (string )$ recoverable ;
90
+ },true ,'[S][E_RECOVERABLE_ERROR] Object of class stdClass could not be converted to string : PhpPlatform\Errors\Exceptions\System\SystemError ' )
91
+ );
92
+ }
93
+
94
+
95
+ /**
96
+ * @requires PHP 5.4
97
+ * @dataProvider systemExceptionsProvider
98
+ */
99
+ public function testErrorHandlingForSystemExceptions ($ erroneousSourceCode ,$ expectedError ,$ expectedOutput ){
100
+
101
+ $ classLoaderReflection = new \ReflectionClass (new ClassLoader ());
102
+ $ vendorDir = dirname (dirname ($ classLoaderReflection ->getFileName ()));
103
+ $ autoloadScript = $ vendorDir .'/autoload.php ' ;
104
+
105
+ $ phpFileContent = file_get_contents (__DIR__ .'/erroneous-template.inc ' );
106
+ $ phpFileContent = str_replace ('$autoloadScript ' , $ autoloadScript , $ phpFileContent );
107
+ $ phpFileContent = str_replace ('$thisPackageName ' , self ::$ thisPackageName , $ phpFileContent );
108
+ $ phpFileContent = str_replace ('$logFile ' , $ this ->logFile , $ phpFileContent );
109
+ $ phpFileContent = str_replace ('$erroneousSourceCode; ' , $ erroneousSourceCode , $ phpFileContent );
110
+
111
+ $ phpFileWithErrors = tempnam (sys_get_temp_dir (), "MOCK " );
112
+ file_put_contents ($ phpFileWithErrors , $ phpFileContent );
113
+
114
+ $ outputFile = tempnam (sys_get_temp_dir (), "ERR " );
115
+
116
+ $ output = array ();
117
+ $ returnCode = 0 ;
118
+ $ command = PHP_BINARY .' -n ' .$ phpFileWithErrors .' > ' .$ outputFile .' 2>&1 ' ;
119
+ exec ($ command ,$ output ,$ returnCode );
120
+
121
+ $ log = file_get_contents ($ this ->logFile );
122
+ parent ::assertContains ($ expectedError , $ log );
123
+
124
+ $ output = file_get_contents ($ outputFile );
125
+
126
+ parent ::assertEquals ($ expectedOutput ,trim ($ output ));
127
+ }
128
+
129
+ public function systemExceptionsProvider (){
130
+
131
+ $ parseErrorFile = tempnam (sys_get_temp_dir (), "PAR " );
132
+ file_put_contents ($ parseErrorFile , "<?php \n noSemicolon() " );
133
+
134
+ $ errorUpdateforPHP7 = '[S][E_ERROR] Call to undefined function undefinedFunction() : PhpPlatform\Errors\Exceptions\System\SystemError ' ;
135
+ if (strpos (phpversion (), "7. " ) === 0 ){
136
+ $ errorUpdateforPHP7 = 'Call to undefined function undefinedFunction() ' ;
137
+ }
138
+
139
+ return array (
140
+ array ('undefinedFunction(); ' ,$ errorUpdateforPHP7 ,'' ),
141
+ array ("include_once ' $ parseErrorFile'; " ,'[S][E_PARSE] syntax error, unexpected end of file : PhpPlatform\Errors\Exceptions\System\SystemError ' ,'' )
142
+ );
143
+ }
144
+
145
+
146
+
147
+
148
+
147
149
}
0 commit comments