@@ -10,32 +10,82 @@ class GetCompatibleExceptionNameTest extends AbstractTraitShimTest
1010
1111 /** @noinspection PhpDocMissingThrowsInspection
1212 *
13- * @dataProvider provideExpectedExceptions
13+ * @dataProvider provideExpectedExceptionsWithoutContext
1414 *
1515 * @param string $exceptionName
1616 * @param string|array $expected
1717 */
18- final public function testShimShouldGetCompatibleExceptionNameWhenGivenExceptionName ($ exceptionName , $ expected )
18+ final public function testShimShouldGetCompatibleExceptionNameWhenGivenExceptionNameWithoutContext ($ exceptionName , $ expected )
1919 {
20- if (is_array ($ expected ) === true ) {
21- /* Grab version specific value */
22- $ key = PHP_MAJOR_VERSION ;
23- if (array_key_exists (PHP_MAJOR_VERSION . PHP_MINOR_VERSION , $ expected ) === true ) {
24- $ key = PHP_MAJOR_VERSION . PHP_MINOR_VERSION ;
25- }
26- $ expected = $ expected [$ key ];
20+ $ expected = $ this ->getExpectedExceptionFromData ($ expected );
21+
22+ $ mockTestCase = $ this ->getMockTestCase ();
23+
24+ $ shim = new GetCompatibleExceptionName ($ mockTestCase );
25+
26+ /** @noinspection PhpUnhandledExceptionInspection */
27+ $ actual = $ shim ->getCompatibleExceptionName ($ exceptionName );
28+
29+ $ this ->assertSame ($ expected , $ actual );
30+ }
31+
32+ /** @noinspection PhpDocMissingThrowsInspection
33+ *
34+ * @dataProvider provideExpectedExceptionsWithContext
35+ *
36+ * @param string $exceptionName
37+ * @param string|array $expected
38+ */
39+ final public function testShimShouldComplainWhenGivenExceptionNameWithContext ($ exceptionName , $ expected )
40+ {
41+ $ mockTestCase = $ this ->getMockTestCase ();
42+
43+ $ shim = new GetCompatibleExceptionName ($ mockTestCase );
44+
45+ $ currentVersion = PHP_MAJOR_VERSION . PHP_MINOR_VERSION ;
46+
47+ if (array_key_exists ($ currentVersion , $ expected ) === false ) {
48+ $ this ->markTestSkipped ('Context not needed for PHP ' .$ currentVersion );
2749 }
2850
29- if (class_exists ($ expected ) === false ) {
30- $ expected = str_replace ('_ ' , '\\' , $ expected );
51+ $ exception = '\\PHPUnit_Framework_Exception ' ;
52+
53+ if (class_exists ($ exception ) === false ) {
54+ $ exception = str_replace ('_ ' , '\\' , $ exception );
3155 }
3256
57+ $ regex = '/ ' . vsprintf (GetCompatibleExceptionName::ERROR_CONTEXT_NEEDED , array ('.* ' , '.* ' )) . '/ ' ;
58+ if (method_exists ($ this , 'expectExceptionMessageRegExp ' )) {
59+ /* PHPUnit ^5.2 | ^6.0 */
60+ $ this ->expectExceptionMessageRegExp ($ regex );
61+ $ this ->expectException ($ exception );
62+ } else {
63+ /* PHPUnit ^4.3 | =< 5.6 */
64+ $ this ->setExpectedExceptionRegExp ($ exception , $ regex );
65+ }
66+
67+ /** @noinspection PhpUnhandledExceptionInspection */
68+ $ actual = $ shim ->getCompatibleExceptionName ($ exceptionName );
69+ }
70+
71+ /** @noinspection PhpDocMissingThrowsInspection
72+ *
73+ * @dataProvider provideExpectedExceptionsWithContext
74+ *
75+ * @param string $exceptionName
76+ * @param string|array $expected
77+ * @param string $context
78+ */
79+ final public function testShimShouldGetCompatibleExceptionNameWhenGivenExceptionNameWithContext ($ exceptionName , $ expected , $ context )
80+ {
81+ $ expected = $ this ->getExpectedExceptionFromData ($ expected );
82+
3383 $ mockTestCase = $ this ->getMockTestCase ();
3484
3585 $ shim = new GetCompatibleExceptionName ($ mockTestCase );
3686
3787 /** @noinspection PhpUnhandledExceptionInspection */
38- $ actual = $ shim ->getCompatibleExceptionName ($ exceptionName );
88+ $ actual = $ shim ->getCompatibleExceptionName ($ exceptionName, $ context );
3989
4090 $ this ->assertSame ($ expected , $ actual );
4191 }
@@ -44,22 +94,68 @@ final public function testShimShouldGetCompatibleExceptionNameWhenGivenException
4494
4595 /////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
4696
47- public function provideExpectedExceptions ()
97+ public function provideExpectedExceptionsWithContext ()
98+ {
99+ return array (
100+ 'ArgumentCountError:with type-hint ' => array (
101+ '\\ArgumentCountError ' ,
102+ array (
103+ '5 ' => '\\PHPUnit_Framework_Error ' ,
104+ '7 ' => '\\ArgumentCountError ' ,
105+ '70 ' => '\\TypeError ' ,
106+ ),
107+ GetCompatibleExceptionName::ARGUMENT_COUNT_ERROR_WITH_TYPE_HINT ,
108+ ),
109+ 'ArgumentCountError:without type-hint ' => array (
110+ '\\ArgumentCountError ' ,
111+ array (
112+ '5 ' => '\\PHPUnit_Framework_Error ' ,
113+ '7 ' => '\\ArgumentCountError ' ,
114+ '70 ' => '\\PHPUnit_Framework_Error ' ,
115+ ),
116+ GetCompatibleExceptionName::ARGUMENT_COUNT_ERROR_WITHOUT_TYPE_HINT ,
117+ ),
118+ );
119+ }
120+
121+ public function provideExpectedExceptionsWithoutContext ()
48122 {
49123 return array (
50- 'ArgumentCountError ' => array ('\\ArgumentCountError ' , array (
51- '5 ' => '\\PHPUnit_Framework_Error ' ,
52- '7 ' => '\\ArgumentCountError ' ,
53- '70 ' => '\\TypeError ' ,
54- )),
55124 'ArithmeticError ' => array ('\\ArithmeticError ' , array (
56125 '5 ' => '\\PHPUnit_Framework_Error ' ,
57126 '7 ' => '\\ArithmeticError ' ,
58127 )),
59128 'DivisionByZeroError ' => array ('\\DivisionByZeroError ' , '\\PHPUnit_Framework_Error_Warning ' ),
60129 'Exception ' => array ('\\Exception ' , '\\Exception ' ),
130+ 'ParseError ' => array ('\\ParseError ' , '\\ParseError ' ),
61131 );
62132 }
63133
134+ /**
135+ * @param $expected
136+ *
137+ * @return mixed
138+ */
139+ private function getExpectedExceptionFromData ($ expected )
140+ {
141+ if (is_array ($ expected ) === true ) {
142+ /* Grab version specific value */
143+ $ key = PHP_MAJOR_VERSION ;
144+ if (array_key_exists (PHP_MAJOR_VERSION . PHP_MINOR_VERSION , $ expected ) === true ) {
145+ $ key = PHP_MAJOR_VERSION . PHP_MINOR_VERSION ;
146+ }
147+ $ expected = $ expected [$ key ];
148+ }
149+
150+ if (class_exists ($ expected ) === false ) {
151+ $ expected = str_replace ('_ ' , '\\' , $ expected );
152+
153+ if ($ expected === '\\PHPUnit \\Framework \\Error ' ) {
154+ $ expected = '\\PHPUnit \\Framework \\Error \\Error ' ;
155+ }
156+ }
157+
158+ return $ expected ;
159+ }
64160}
65161/*EOF*/
0 commit comments