@@ -40,6 +40,12 @@ public function testStatusCode()
40
40
$ flattened = FlattenException::create (new \RuntimeException ());
41
41
$ this ->assertEquals ('500 ' , $ flattened ->getStatusCode ());
42
42
43
+ $ flattened = FlattenException::createFromThrowable (new \DivisionByZeroError (), 403 );
44
+ $ this ->assertEquals ('403 ' , $ flattened ->getStatusCode ());
45
+
46
+ $ flattened = FlattenException::createFromThrowable (new \DivisionByZeroError ());
47
+ $ this ->assertEquals ('500 ' , $ flattened ->getStatusCode ());
48
+
43
49
$ flattened = FlattenException::create (new NotFoundHttpException ());
44
50
$ this ->assertEquals ('404 ' , $ flattened ->getStatusCode ());
45
51
@@ -112,10 +118,10 @@ public function testHeadersForHttpException()
112
118
/**
113
119
* @dataProvider flattenDataProvider
114
120
*/
115
- public function testFlattenHttpException (\Exception $ exception )
121
+ public function testFlattenHttpException (\Throwable $ exception )
116
122
{
117
- $ flattened = FlattenException::create ($ exception );
118
- $ flattened2 = FlattenException::create ($ exception );
123
+ $ flattened = FlattenException::createFromThrowable ($ exception );
124
+ $ flattened2 = FlattenException::createFromThrowable ($ exception );
119
125
120
126
$ flattened ->setPrevious ($ flattened2 );
121
127
@@ -124,7 +130,7 @@ public function testFlattenHttpException(\Exception $exception)
124
130
$ this ->assertInstanceOf ($ flattened ->getClass (), $ exception , 'The class is set to the class of the original exception ' );
125
131
}
126
132
127
- public function testThrowable ()
133
+ public function testWrappedThrowable ()
128
134
{
129
135
$ exception = new FatalThrowableError (new \DivisionByZeroError ('Ouch ' , 42 ));
130
136
$ flattened = FlattenException::create ($ exception );
@@ -134,13 +140,23 @@ public function testThrowable()
134
140
$ this ->assertSame ('DivisionByZeroError ' , $ flattened ->getClass (), 'The class is set to the class of the original error ' );
135
141
}
136
142
143
+ public function testThrowable ()
144
+ {
145
+ $ error = new \DivisionByZeroError ('Ouch ' , 42 );
146
+ $ flattened = FlattenException::createFromThrowable ($ error );
147
+
148
+ $ this ->assertSame ('Ouch ' , $ flattened ->getMessage (), 'The message is copied from the original error. ' );
149
+ $ this ->assertSame (42 , $ flattened ->getCode (), 'The code is copied from the original error. ' );
150
+ $ this ->assertSame ('DivisionByZeroError ' , $ flattened ->getClass (), 'The class is set to the class of the original error ' );
151
+ }
152
+
137
153
/**
138
154
* @dataProvider flattenDataProvider
139
155
*/
140
- public function testPrevious (\Exception $ exception )
156
+ public function testPrevious (\Throwable $ exception )
141
157
{
142
- $ flattened = FlattenException::create ($ exception );
143
- $ flattened2 = FlattenException::create ($ exception );
158
+ $ flattened = FlattenException::createFromThrowable ($ exception );
159
+ $ flattened2 = FlattenException::createFromThrowable ($ exception );
144
160
145
161
$ flattened ->setPrevious ($ flattened2 );
146
162
@@ -163,33 +179,33 @@ public function testPreviousError()
163
179
/**
164
180
* @dataProvider flattenDataProvider
165
181
*/
166
- public function testLine (\Exception $ exception )
182
+ public function testLine (\Throwable $ exception )
167
183
{
168
- $ flattened = FlattenException::create ($ exception );
184
+ $ flattened = FlattenException::createFromThrowable ($ exception );
169
185
$ this ->assertSame ($ exception ->getLine (), $ flattened ->getLine ());
170
186
}
171
187
172
188
/**
173
189
* @dataProvider flattenDataProvider
174
190
*/
175
- public function testFile (\Exception $ exception )
191
+ public function testFile (\Throwable $ exception )
176
192
{
177
- $ flattened = FlattenException::create ($ exception );
193
+ $ flattened = FlattenException::createFromThrowable ($ exception );
178
194
$ this ->assertSame ($ exception ->getFile (), $ flattened ->getFile ());
179
195
}
180
196
181
197
/**
182
198
* @dataProvider flattenDataProvider
183
199
*/
184
- public function testToArray (\Exception $ exception )
200
+ public function testToArray (\Throwable $ exception, string $ expectedClass )
185
201
{
186
- $ flattened = FlattenException::create ($ exception );
202
+ $ flattened = FlattenException::createFromThrowable ($ exception );
187
203
$ flattened ->setTrace (array (), 'foo.php ' , 123 );
188
204
189
205
$ this ->assertEquals (array (
190
206
array (
191
207
'message ' => 'test ' ,
192
- 'class ' => ' Exception ' ,
208
+ 'class ' => $ expectedClass ,
193
209
'trace ' => array (array (
194
210
'namespace ' => '' , 'short_class ' => '' , 'class ' => '' , 'type ' => '' , 'function ' => '' , 'file ' => 'foo.php ' , 'line ' => 123 ,
195
211
'args ' => array (),
@@ -198,10 +214,24 @@ public function testToArray(\Exception $exception)
198
214
), $ flattened ->toArray ());
199
215
}
200
216
217
+ public function testCreate ()
218
+ {
219
+ $ exception = new NotFoundHttpException (
220
+ 'test ' ,
221
+ new \RuntimeException ('previous ' , 123 )
222
+ );
223
+
224
+ $ this ->assertSame (
225
+ FlattenException::createFromThrowable ($ exception )->toArray (),
226
+ FlattenException::create ($ exception )->toArray ()
227
+ );
228
+ }
229
+
201
230
public function flattenDataProvider ()
202
231
{
203
232
return array (
204
- array (new \Exception ('test ' , 123 )),
233
+ array (new \Exception ('test ' , 123 ), 'Exception ' ),
234
+ array (new \Error ('test ' , 123 ), 'Error ' ),
205
235
);
206
236
}
207
237
0 commit comments