13
13
14
14
use Symfony \Component \Debug \Exception \SilencedErrorContext ;
15
15
use Symfony \Component \HttpKernel \DataCollector \LoggerDataCollector ;
16
+ use Symfony \Component \VarDumper \Cloner \Data ;
16
17
17
18
class LoggerDataCollectorTest extends \PHPUnit_Framework_TestCase
18
19
{
20
+ private static $ data ;
21
+
19
22
/**
20
23
* @dataProvider getCollectTestData
21
24
*/
@@ -25,19 +28,17 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
25
28
$ logger ->expects ($ this ->once ())->method ('countErrors ' )->will ($ this ->returnValue ($ nb ));
26
29
$ logger ->expects ($ this ->exactly (2 ))->method ('getLogs ' )->will ($ this ->returnValue ($ logs ));
27
30
28
- $ c = new LoggerDataCollector ($ logger );
31
+ // disable cloning the context, to ease fixtures creation.
32
+ $ c = $ this ->getMockBuilder (LoggerDataCollector::class)
33
+ ->setMethods (array ('cloneVar ' ))
34
+ ->setConstructorArgs (array ($ logger ))
35
+ ->getMock ();
36
+ $ c ->expects ($ this ->any ())->method ('cloneVar ' )->willReturn (self ::$ data );
29
37
$ c ->lateCollect ();
30
38
31
- // Remove the trace from the real logs, to ease fixtures creation.
32
- $ logs = array_map (function ($ log ) {
33
- unset($ log ['context ' ]['trace ' ], $ log ['context ' ]['exception ' ]['trace ' ]);
34
-
35
- return $ log ;
36
- }, $ c ->getLogs ());
37
-
38
39
$ this ->assertEquals ('logger ' , $ c ->getName ());
39
40
$ this ->assertEquals ($ nb , $ c ->countErrors ());
40
- $ this ->assertEquals ($ expectedLogs , $ logs );
41
+ $ this ->assertEquals ($ expectedLogs , $ c -> getLogs () );
41
42
$ this ->assertEquals ($ expectedDeprecationCount , $ c ->countDeprecations ());
42
43
$ this ->assertEquals ($ expectedScreamCount , $ c ->countScreams ());
43
44
@@ -48,6 +49,10 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
48
49
49
50
public function getCollectTestData ()
50
51
{
52
+ if (null === self ::$ data ) {
53
+ self ::$ data = new Data (array ());
54
+ }
55
+
51
56
yield 'simple log ' => array (
52
57
1 ,
53
58
array (array ('message ' => 'foo ' , 'context ' => array (), 'priority ' => 100 , 'priorityName ' => 'DEBUG ' )),
@@ -56,18 +61,10 @@ public function getCollectTestData()
56
61
0 ,
57
62
);
58
63
59
- yield 'log with a resource ' => array (
60
- 1 ,
61
- array (array ('message ' => 'foo ' , 'context ' => array ('foo ' => fopen (__FILE__ , 'r ' )), 'priority ' => 100 , 'priorityName ' => 'DEBUG ' )),
62
- array (array ('message ' => 'foo ' , 'context ' => array ('foo ' => 'Resource(stream) ' ), 'priority ' => 100 , 'priorityName ' => 'DEBUG ' )),
63
- 0 ,
64
- 0 ,
65
- );
66
-
67
- yield 'log with an object ' => array (
64
+ yield 'log with a context ' => array (
68
65
1 ,
69
- array (array ('message ' => 'foo ' , 'context ' => array ('foo ' => new \ stdClass () ), 'priority ' => 100 , 'priorityName ' => 'DEBUG ' )),
70
- array (array ('message ' => 'foo ' , 'context ' => array ( ' foo ' => ' Object(stdClass) ' ) , 'priority ' => 100 , 'priorityName ' => 'DEBUG ' )),
66
+ array (array ('message ' => 'foo ' , 'context ' => array ('foo ' => ' bar ' ), 'priority ' => 100 , 'priorityName ' => 'DEBUG ' )),
67
+ array (array ('message ' => 'foo ' , 'context ' => self :: $ data , 'priority ' => 100 , 'priorityName ' => 'DEBUG ' )),
71
68
0 ,
72
69
0 ,
73
70
);
@@ -84,9 +81,9 @@ public function getCollectTestData()
84
81
array ('message ' => 'foo2 ' , 'context ' => array ('exception ' => new \ErrorException ('deprecated ' , 0 , E_USER_DEPRECATED )), 'priority ' => 100 , 'priorityName ' => 'DEBUG ' ),
85
82
),
86
83
array (
87
- array ('message ' => 'foo3 ' , 'context ' => array ( ' exception ' => array ( ' file ' => __FILE__ , ' line ' => 82 , ' class ' => \ErrorException::class, ' message ' => ' warning ' )) , 'priority ' => 100 , 'priorityName ' => 'DEBUG ' ),
88
- array ('message ' => 'foo ' , 'context ' => array ( ' type ' => ' E_DEPRECATED ' , 'file ' => __FILE__ , 'line ' => 83 , 'errorCount ' => 1 , 'scream ' => false ), ' priority ' => 100 , ' priorityName ' => ' DEBUG ' ),
89
- array ('message ' => 'foo2 ' , 'context ' => array ( ' type ' => ' E_USER_DEPRECATED ' , 'file ' => __FILE__ , 'line ' => 84 , 'errorCount ' => 1 , 'scream ' => false ), ' priority ' => 100 , ' priorityName ' => ' DEBUG ' ),
84
+ array ('message ' => 'foo3 ' , 'context ' => self :: $ data , 'priority ' => 100 , 'priorityName ' => 'DEBUG ' ),
85
+ array ('message ' => 'foo ' , 'context ' => self :: $ data , 'priority ' => 100 , 'priorityName ' => ' DEBUG ' , 'errorCount ' => 1 , 'scream ' => false ),
86
+ array ('message ' => 'foo2 ' , 'context ' => self :: $ data , 'priority ' => 100 , 'priorityName ' => ' DEBUG ' , 'errorCount ' => 1 , 'scream ' => false ),
90
87
),
91
88
2 ,
92
89
0 ,
@@ -100,8 +97,8 @@ public function getCollectTestData()
100
97
array ('message ' => 'foo3 ' , 'context ' => array ('exception ' => new SilencedErrorContext (E_USER_WARNING , __FILE__ , __LINE__ )), 'priority ' => 100 , 'priorityName ' => 'DEBUG ' ),
101
98
),
102
99
array (
103
- array ('message ' => 'foo3 ' , 'context ' => array ( ' exception ' => array ( ' file ' => __FILE__ , ' line ' => 99 , ' class ' => \ErrorException::class, ' message ' => ' warning ' )) , 'priority ' => 100 , 'priorityName ' => 'DEBUG ' ),
104
- array ('message ' => 'foo3 ' , 'context ' => array ( ' type ' => ' E_USER_WARNING ' , 'file ' => __FILE__ , 'line ' => 100 , 'errorCount ' => 1 , 'scream ' => true ), ' priority ' => 100 , ' priorityName ' => ' DEBUG ' ),
100
+ array ('message ' => 'foo3 ' , 'context ' => self :: $ data , 'priority ' => 100 , 'priorityName ' => 'DEBUG ' ),
101
+ array ('message ' => 'foo3 ' , 'context ' => self :: $ data , 'priority ' => 100 , 'priorityName ' => ' DEBUG ' , 'errorCount ' => 1 , 'scream ' => true ),
105
102
),
106
103
0 ,
107
104
1 ,
0 commit comments