12
12
namespace Symfony \Component \Cache \Tests \DataCollector ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Cache \Adapter \NullAdapter ;
15
16
use Symfony \Component \Cache \Adapter \TraceableAdapter ;
16
17
use Symfony \Component \Cache \DataCollector \CacheDataCollector ;
17
18
use Symfony \Component \HttpFoundation \Request ;
@@ -55,16 +56,16 @@ public function testHitedEventDataCollector()
55
56
$ traceableAdapterEvent ->name = 'hasItem ' ;
56
57
$ traceableAdapterEvent ->start = 0 ;
57
58
$ traceableAdapterEvent ->end = 0 ;
58
- $ traceableAdapterEvent ->hits = 1 ;
59
+ $ traceableAdapterEvent ->hits = 0 ;
59
60
$ traceableAdapterEvent ->misses = 0 ;
60
61
$ traceableAdapterEvent ->result = ['foo ' => false ];
61
62
62
63
$ statistics = $ this ->getCacheDataCollectorStatisticsFromEvents ([$ traceableAdapterEvent ]);
63
64
64
65
$ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['calls ' ], 1 , 'calls ' );
65
66
$ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['reads ' ], 1 , 'reads ' );
66
- $ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['hits ' ], 1 , 'hits ' );
67
- $ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['misses ' ], 0 , 'misses ' );
67
+ $ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['hits ' ], 0 , 'hits ' );
68
+ $ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['misses ' ], 1 , 'misses ' );
68
69
$ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['writes ' ], 0 , 'writes ' );
69
70
}
70
71
@@ -84,6 +85,25 @@ public function testSavedEventDataCollector()
84
85
$ this ->assertEquals ($ statistics [self ::INSTANCE_NAME ]['writes ' ], 1 , 'writes ' );
85
86
}
86
87
88
+ public function testCollectBeforeEnd ()
89
+ {
90
+ $ adapter = new TraceableAdapter (new NullAdapter ());
91
+
92
+ $ collector = new CacheDataCollector ();
93
+ $ collector ->addInstance (self ::INSTANCE_NAME , $ adapter );
94
+
95
+ $ adapter ->get ('foo ' , function () use ($ collector ) {
96
+ $ collector ->collect (new Request (), new Response ());
97
+
98
+ return 123 ;
99
+ });
100
+
101
+ $ stats = $ collector ->getStatistics ();
102
+ $ this ->assertGreaterThan (0 , $ stats [self ::INSTANCE_NAME ]['time ' ]);
103
+ $ this ->assertEquals ($ stats [self ::INSTANCE_NAME ]['hits ' ], 0 , 'hits ' );
104
+ $ this ->assertEquals ($ stats [self ::INSTANCE_NAME ]['misses ' ], 1 , 'misses ' );
105
+ }
106
+
87
107
private function getCacheDataCollectorStatisticsFromEvents (array $ traceableAdapterEvents )
88
108
{
89
109
$ traceableAdapterMock = $ this ->createMock (TraceableAdapter::class);
0 commit comments