1414 */
1515namespace DebugKit \Test \TestCase ;
1616
17+ use Cake \Error \PhpError ;
1718use Cake \Event \Event ;
1819use Cake \Event \EventManager ;
1920use Cake \TestSuite \TestCase ;
@@ -37,28 +38,44 @@ public function testSetDeprecationHandler()
3738 $ service = new ToolbarService (new EventManager (), []);
3839 $ plugin = new Plugin ();
3940 $ plugin ->setDeprecationHandler ($ service );
40- $ event = new Event ('' );
4141 $ panel = new DeprecationsPanel ();
4242
43- //Without setting the $stackFrame
44- deprecationWarning ('setDeprecationHandler ' );
45- //Setting the $stackFrame
46- deprecationWarning ('setDeprecationHandler_2 ' , 2 );
47- //Raw error
48- $ line = __LINE__ + 1 ;
49- trigger_error ('raw_error ' , E_USER_DEPRECATED );
43+ $ error = new PhpError (E_USER_WARNING , 'ignored ' , __FILE__ , __LINE__ , []);
44+ $ event = new Event ('Error.beforeRender ' , null , ['error ' => $ error ]);
45+ EventManager::instance ()->dispatch ($ event );
46+
47+ // No file/line in message.
48+ $ error = new PhpError (E_USER_DEPRECATED , 'going away ' , __FILE__ , __LINE__ , []);
49+ $ event = new Event ('Error.beforeRender ' , null , ['error ' => $ error ]);
50+ EventManager::instance ()->dispatch ($ event );
51+
52+ // Formatted like deprecationWarning()
53+ $ message = <<<TEXT
54+ Something deprecated happened.
55+ Don't use that thing.
56+ src/Plugin.php, line: 51
57+ You can disable all deprecation warnings by setting `Error.errorLevel` to `E_ALL & ~E_USER_DEPRECATED`.
58+ TEXT ;
59+ $ error = new PhpError (E_USER_DEPRECATED , $ message , __FILE__ , __LINE__ , []);
60+ $ event = new Event ('Error.beforeRender ' , null , ['error ' => $ error ]);
61+ EventManager::instance ()->dispatch ($ event );
5062
5163 $ panel ->shutdown ($ event );
52- $ data = $ panel ->data ()['plugins ' ]['DebugKit ' ];
64+ $ data = $ panel ->data ();
65+
66+ $ this ->assertArrayHasKey ('plugins ' , $ data );
67+ $ this ->assertArrayHasKey ('DebugKit ' , $ data ['plugins ' ]);
68+ $ this ->assertCount (1 , $ data ['plugins ' ]['DebugKit ' ]);
5369
54- $ this ->assertCount (3 , $ data );
70+ $ first = $ data ['plugins ' ]['DebugKit ' ][0 ];
71+ $ this ->assertEquals ($ first ['message ' ], 'going away ' );
72+ $ this ->assertEquals ($ first ['file ' ], __FILE__ );
73+ $ this ->assertEquals ($ first ['line ' ], 48 );
5574
56- //test first two deprecationWarning()
57- foreach ([$ data [0 ], $ data [1 ]] as $ value ) {
58- $ this ->assertStringContainsString ($ value ['file ' ], $ value ['message ' ]);
59- $ this ->assertStringContainsString ("line: {$ value ['line ' ]}" , $ value ['message ' ]);
60- }
61- //test raw error
62- $ this ->assertSame ($ line , $ data [2 ]['line ' ]);
75+ $ this ->assertArrayHasKey ('other ' , $ data );
76+ $ parsed = $ data ['other ' ][0 ];
77+ $ this ->assertEquals ($ parsed ['message ' ], $ message );
78+ $ this ->assertEquals ($ parsed ['file ' ], 'src/Plugin.php ' );
79+ $ this ->assertEquals ($ parsed ['line ' ], 51 );
6380 }
6481}
0 commit comments