StatusTest::testGetGraphMemoryInfo()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 31
rs 8.8571
c 3
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace OpCacheGUITest\OpCache;
4
5
use OpCacheGUI\Format\Byte;
6
use OpCacheGUI\Format\Trimmer;
7
use OpCacheGUI\I18n\Translator;
8
use OpCacheGUI\OpCache\Status;
9
use PHPUnit\Framework\TestCase;
10
11
class StatusTest extends TestCase
12
{
13
    protected $statusData;
14
15
    protected function setUp()
16
    {
17
        date_default_timezone_set('UTC');
18
19
        $this->statusData = [
20
            'opcache_enabled'     => true,
21
            'cache_full'          => false,
22
            'restart_pending'     => false,
23
            'restart_in_progress' => false,
24
            'memory_usage'        => [
25
                'used_memory'               => '457392',
26
                'free_memory'               => '133573736',
27
                'wasted_memory'             => '186600',
28
                'current_wasted_percentage' => '0.1390278339386',
29
            ],
30
            'opcache_statistics'  => [
31
                'num_cached_scripts'   => 38,
32
                'num_cached_keys'      => 52,
33
                'max_cached_keys'      => 7963,
34
                'hits'                 => 1160,
35
                'misses'               => 59,
36
                'blacklist_misses'     => 0,
37
                'blacklist_miss_ratio' => 0,
38
                'opcache_hit_rate'     => 95.159967186218,
39
                'start_time'           => 1412775693,
40
                'last_restart_time'    => 1412790000,
41
                'oom_restarts'         => 0,
42
                'hash_restarts'        => 0,
43
                'manual_restarts'      => 0,
44
            ],
45
            'scripts'             => [
46
                [
47
                    'full_path'           => '/var/www/vhosts/OpcacheGUI/src/OpCacheGUI/Network/Request.php',
48
                    'hits'                => 1,
49
                    'memory_consumption'  => 6608,
50
                    'last_used'           => 'Thu Oct 09 16:08:35 2014',
51
                    'last_used_timestamp' => 1412863715,
52
                    'timestamp'           => 1412698853,
53
                ],
54
                [
55
                    'full_path'           => '/var/www/vhosts/OpcacheGUI/template/cached.phtml',
56
                    'hits'                => 4,
57
                    'memory_consumption'  => 3213,
58
                    'last_used'           => 'Thu Oct 09 16:06:35 2014',
59
                    'last_used_timestamp' => 1412863715,
60
                    'timestamp'           => 1412698853,
61
                ],
62
                [
63
                    'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Foo.php',
64
                    'hits'                => 19,
65
                    'memory_consumption'  => 204,
66
                    'last_used'           => 'Thu Oct 09 16:04:35 2014',
67
                    'last_used_timestamp' => 1412868715,
68
                    'timestamp'           => 1412798853,
69
                ],
70
                [
71
                    'full_path'           => '/var/www/vhosts/RedTube/template/humiliation/germany-vs-brazil.phtml',
72
                    'hits'                => 71,
73
                    'memory_consumption'  => 7001,
74
                    'last_used'           => 'Thu Jul 09 21:20:10 2014',
75
                    'last_used_timestamp' => 1412864715,
76
                    'timestamp'           => 1412798475,
77
                ],
78
                [
79
                    'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Psr/Autoloader.php',
80
                    'hits'                => 32,
81
                    'memory_consumption'  => 4678,
82
                    'last_used'           => 'Thu Oct 10 15:04:35 2014',
83
                    'last_used_timestamp' => 1412864715,
84
                    'timestamp'           => 1412798475,
85
                ],
86
                [
87
                    'full_path'           => '/var/www/vhosts/NoTimeStamp/src/Psr/Autoloader.php',
88
                    'hits'                => 12876,
89
                    'memory_consumption'  => 2048,
90
                    'last_used'           => 'Thu Oct 10 15:04:35 2014',
91
                    'last_used_timestamp' => 1412864715,
92
                ],
93
            ],
94
        ];
95
    }
96
97
    /**
98
     * @covers OpCacheGUI\OpCache\Status::__construct
99
     * @covers OpCacheGUI\OpCache\Status::getStatusInfo
100
     */
101
    public function testGetStatusInfo()
102
    {
103
        $status = new Status(
104
            $this->createMock(Byte::class),
105
            $this->createMock(Translator::class),
106
            $this->statusData
107
        );
108
109
        $data = [];
110
111
        foreach ($this->statusData as $key => $value) {
112
            if (!in_array($key, ['opcache_enabled', 'cache_full', 'restart_pending', 'restart_in_progress'], true)) {
113
                continue;
114
            }
115
116
            $data[$key] = $value;
117
        }
118
119
        $this->assertSame($data, $status->getStatusInfo());
120
    }
121
122
    /**
123
     * @covers OpCacheGUI\OpCache\Status::__construct
124
     * @covers OpCacheGUI\OpCache\Status::getMemoryInfo
125
     */
126
    public function testGetMemoryInfo()
127
    {
128
        $formatter = $this->createMock(Byte::class);
129
        $formatter->method('format')->will($this->onConsecutiveCalls('1KB', '2KB', '3KB'));
130
131
        $status = new Status($formatter, $this->createMock(Translator::class), $this->statusData);
132
133
        $data = $this->statusData;
134
135
        $data['memory_usage']['used_memory']   = '1KB';
136
        $data['memory_usage']['free_memory']   = '2KB';
137
        $data['memory_usage']['wasted_memory'] = '3KB';
138
        $data['memory_usage']['current_wasted_percentage'] = '0.14%';
139
140
        $this->assertSame($data['memory_usage'], $status->getMemoryInfo());
141
    }
142
143
    /**
144
     * @covers OpCacheGUI\OpCache\Status::__construct
145
     * @covers OpCacheGUI\OpCache\Status::getGraphMemoryInfo
146
     */
147
    public function testGetGraphMemoryInfo()
148
    {
149
        $translator = $this->createMock(Translator::class);
150
        $translator->method('translate')->will($this->onConsecutiveCalls('Used', 'Free', 'Wasted'));
151
152
        $status = new Status(
153
            $this->createMock(Byte::class),
154
            $translator,
155
            $this->statusData
156
        );
157
158
        $data = [
159
            [
160
                'value' => $this->statusData['memory_usage']['used_memory'],
161
                'color' => '#16a085',
162
                'label' => 'Used',
163
            ],
164
            [
165
                'value' => $this->statusData['memory_usage']['free_memory'],
166
                'color' => '#2ecc71',
167
                'label' => 'Free',
168
            ],
169
            [
170
                'value' => $this->statusData['memory_usage']['wasted_memory'],
171
                'color' => '#e74c3c',
172
                'label' => 'Wasted',
173
            ],
174
        ];
175
176
        $this->assertSame(json_encode($data), $status->getGraphMemoryInfo());
177
    }
178
179
    /**
180
     * @covers OpCacheGUI\OpCache\Status::__construct
181
     * @covers OpCacheGUI\OpCache\Status::getStatsInfo
182
     */
183
    public function testGetStatsInfoWithOpCacheDisabled()
184
    {
185
        $statusData = $this->statusData;
186
        $statusData['opcache_enabled'] = false;
187
188
        $status = new Status(
189
            $this->createMock(Byte::class),
190
            $this->createMock(Translator::class),
191
            $statusData
192
        );
193
194
        $data = [
195
            [
196
                'num_cached_scripts'   => 0,
197
                'num_cached_keys'      => 0,
198
                'max_cached_keys'      => 0,
199
                'hits'                 => 0,
200
                'misses'               => 0,
201
                'blacklist_misses'     => 0,
202
                'blacklist_miss_ratio' => 'n/a',
203
            ],
204
            [
205
                'opcache_hit_rate'     => 'n/a',
206
                'start_time'           => 'n/a',
207
                'last_restart_time'    => 'n/a',
208
                'oom_restarts'         => 'n/a',
209
                'hash_restarts'        => 'n/a',
210
                'manual_restarts'      => 'n/a',
211
            ]
212
        ];
213
214
        $this->assertSame($data, $status->getStatsInfo());
215
    }
216
217
    /**
218
     * @covers OpCacheGUI\OpCache\Status::__construct
219
     * @covers OpCacheGUI\OpCache\Status::getStatsInfo
220
     */
221
    public function testGetStatsInfoWithOpCacheEnabled()
222
    {
223
        $status = new Status(
224
            $this->createMock(Byte::class),
225
            $this->createMock(Translator::class),
226
            $this->statusData
227
        );
228
229
        $data = $this->statusData['opcache_statistics'];
230
231
        $data['blacklist_miss_ratio'] = 0.0;
232
        $data['opcache_hit_rate']     = '95.16%';
233
        $data['start_time']           = '13:41:33 08-10-2014';
234
        $data['last_restart_time']    = '17:40:00 08-10-2014';
235
236
        $formattedData = [
237
            array_slice($data, 0, 7, true),
238
            array_slice($data, 7, null, true),
239
        ];
240
241
        $this->assertSame($formattedData, $status->getStatsInfo());
242
    }
243
244
    /**
245
     * @covers OpCacheGUI\OpCache\Status::__construct
246
     * @covers OpCacheGUI\OpCache\Status::getStatsInfo
247
     */
248
    public function testGetStatsInfoWithOpCacheEnabledTakesIntoAccountTimeZones()
249
    {
250
        date_default_timezone_set('Europe/Amsterdam');
251
252
        $status = new Status(
253
            $this->createMock(Byte::class),
254
            $this->createMock(Translator::class),
255
            $this->statusData
256
        );
257
258
        $data = $this->statusData['opcache_statistics'];
259
260
        $data['blacklist_miss_ratio'] = 0.0;
261
        $data['opcache_hit_rate']     = '95.16%';
262
        $data['start_time']           = '15:41:33 08-10-2014';
263
        $data['last_restart_time']    = '19:40:00 08-10-2014';
264
265
        $formattedData = [
266
            array_slice($data, 0, 7, true),
267
            array_slice($data, 7, null, true),
268
        ];
269
270
        $this->assertSame($formattedData, $status->getStatsInfo());
271
    }
272
273
    /**
274
     * @covers OpCacheGUI\OpCache\Status::__construct
275
     * @covers OpCacheGUI\OpCache\Status::getStatsInfo
276
     */
277
    public function testGetStatsInfoWithOpCacheEnabledWithoutRestart()
278
    {
279
        $statusData = $this->statusData;
280
281
        $statusData['opcache_statistics']['last_restart_time'] = 0;
282
283
        $status = new Status(
284
            $this->createMock(Byte::class),
285
            $this->createMock(Translator::class),
286
            $statusData
287
        );
288
289
        $data = $this->statusData['opcache_statistics'];
290
291
        $data['blacklist_miss_ratio'] = 0.0;
292
        $data['opcache_hit_rate']     = '95.16%';
293
        $data['start_time']           = '13:41:33 08-10-2014';
294
        $data['last_restart_time']    = null;
295
296
        $formattedData = [
297
            array_slice($data, 0, 7, true),
298
            array_slice($data, 7, null, true),
299
        ];
300
301
        $this->assertSame($formattedData, $status->getStatsInfo());
302
    }
303
304
    /**
305
     * @covers OpCacheGUI\OpCache\Status::__construct
306
     * @covers OpCacheGUI\OpCache\Status::getGraphKeyStatsInfo
307
     */
308
    public function testGetGraphKeyStatsInfo()
309
    {
310
        $translator = $this->createMock(Translator::class);
311
        $translator->method('translate')->will($this->onConsecutiveCalls('Used', 'Free', 'Wasted'));
312
313
        $status = new Status($this->createMock(Byte::class), $translator, $this->statusData);
314
315
        $data = [
316
            [
317
                'value' => 38,
318
                'color' => '#16a085',
319
                'label' => 'Used',
320
            ],
321
            [
322
                'value' => 7911,
323
                'color' => '#2ecc71',
324
                'label' => 'Free',
325
            ],
326
            [
327
                'value' => 14,
328
                'color' => '#e74c3c',
329
                'label' => 'Wasted',
330
            ],
331
        ];
332
333
        $this->assertSame(json_encode($data), $status->getGraphKeyStatsInfo());
334
    }
335
336
    /**
337
     * @covers OpCacheGUI\OpCache\Status::__construct
338
     * @covers OpCacheGUI\OpCache\Status::getGraphHitStatsInfo
339
     */
340
    public function testGetGraphHitStatsInfo()
341
    {
342
        $translator = $this->createMock(Translator::class);
343
        $translator->method('translate')->will($this->onConsecutiveCalls('Hits', 'Misses', 'Blacklisted'));
344
345
        $status = new Status($this->createMock(Byte::class), $translator, $this->statusData);
346
347
        $data = [
348
            [
349
                'value' => 1160,
350
                'color' => '#2ecc71',
351
                'label' => 'Hits',
352
            ],
353
            [
354
                'value' => 59,
355
                'color' => '#e74c3c',
356
                'label' => 'Misses',
357
            ],
358
            [
359
                'value' => 0,
360
                'color' => '#16a085',
361
                'label' => 'Blacklisted',
362
            ],
363
        ];
364
365
        $this->assertSame(json_encode($data), $status->getGraphHitStatsInfo());
366
    }
367
368
    /**
369
     * @covers OpCacheGUI\OpCache\Status::__construct
370
     * @covers OpCacheGUI\OpCache\Status::getCachedScripts
371
     */
372
    public function testGetCachedScriptsEmpty()
373
    {
374
        $statusData = $this->statusData;
375
376
        unset($statusData['scripts']);
377
378
        $status = new Status(
379
            $this->createMock(Byte::class),
380
            $this->createMock(Translator::class),
381
            $statusData
382
        );
383
384
        $this->assertSame([], $status->getCachedScripts());
385
    }
386
387
    /**
388
     * @covers OpCacheGUI\OpCache\Status::__construct
389
     * @covers OpCacheGUI\OpCache\Status::getCachedScripts
390
     * @covers OpCacheGUI\OpCache\Status::sortCachedScripts
391
     */
392
    public function testGetCachedScriptsFilled()
393
    {
394
        $formatter = $this->createMock(Byte::class);
395
        $formatter->method('format')->will($this->onConsecutiveCalls('1KB', '2KB', '3KB', '4KB', '5KB', '6KB'));
396
397
        $status = new Status($formatter, $this->createMock(Translator::class), $this->statusData);
398
399
        $data = [
400
            [
401
                'full_path'           => '/var/www/vhosts/NoTimeStamp/src/Psr/Autoloader.php',
402
                'hits'                => 12876,
403
                'memory_consumption'  => '6KB',
404
                'last_used_timestamp' => '14:25:15 09-10-2014',
405
                'timestamp'           => 'N/A',
406
            ],
407
            [
408
                'full_path'           => '/var/www/vhosts/OpcacheGUI/src/OpCacheGUI/Network/Request.php',
409
                'hits'                => 1,
410
                'memory_consumption'  => '1KB',
411
                'last_used_timestamp' => '14:08:35 09-10-2014',
412
                'timestamp'           => '16:20:53 07-10-2014',
413
            ],
414
            [
415
                'full_path'           => '/var/www/vhosts/OpcacheGUI/template/cached.phtml',
416
                'hits'                => 4,
417
                'memory_consumption'  => '2KB',
418
                'last_used_timestamp' => '14:08:35 09-10-2014',
419
                'timestamp'           => '16:20:53 07-10-2014',
420
            ],
421
            [
422
                'full_path'           => '/var/www/vhosts/RedTube/template/humiliation/germany-vs-brazil.phtml',
423
                'hits'                => 71,
424
                'memory_consumption'  => '4KB',
425
                'last_used_timestamp' => '14:25:15 09-10-2014',
426
                'timestamp'           => '20:01:15 08-10-2014',
427
            ],
428
            [
429
                'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Foo.php',
430
                'hits'                => 19,
431
                'memory_consumption'  => '3KB',
432
                'last_used_timestamp' => '15:31:55 09-10-2014',
433
                'timestamp'           => '20:07:33 08-10-2014',
434
            ],
435
            [
436
                'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Psr/Autoloader.php',
437
                'hits'                => 32,
438
                'memory_consumption'  => '5KB',
439
                'last_used_timestamp' => '14:25:15 09-10-2014',
440
                'timestamp'           => '20:01:15 08-10-2014',
441
            ],
442
        ];
443
444
        $this->assertSame($data, $status->getCachedScripts());
445
    }
446
447
    /**
448
     * @covers OpCacheGUI\OpCache\Status::__construct
449
     * @covers OpCacheGUI\OpCache\Status::getCachedScripts
450
     * @covers OpCacheGUI\OpCache\Status::sortCachedScripts
451
     */
452
    public function testGetCachedScriptsFilledTakesIntoAccountTimeZones()
453
    {
454
        date_default_timezone_set('Europe/Amsterdam');
455
456
        $formatter = $this->createMock(Byte::class);
457
        $formatter->method('format')->will($this->onConsecutiveCalls('1KB', '2KB', '3KB', '4KB', '5KB', '6KB'));
458
459
        $status = new Status($formatter, $this->createMock(Translator::class), $this->statusData);
460
461
        $data = [
462
            [
463
                'full_path'           => '/var/www/vhosts/NoTimeStamp/src/Psr/Autoloader.php',
464
                'hits'                => 12876,
465
                'memory_consumption'  => '6KB',
466
                'last_used_timestamp' => '16:25:15 09-10-2014',
467
                'timestamp'           => 'N/A',
468
            ],
469
            [
470
                'full_path'           => '/var/www/vhosts/OpcacheGUI/src/OpCacheGUI/Network/Request.php',
471
                'hits'                => 1,
472
                'memory_consumption'  => '1KB',
473
                'last_used_timestamp' => '16:08:35 09-10-2014',
474
                'timestamp'           => '18:20:53 07-10-2014',
475
            ],
476
            [
477
                'full_path'           => '/var/www/vhosts/OpcacheGUI/template/cached.phtml',
478
                'hits'                => 4,
479
                'memory_consumption'  => '2KB',
480
                'last_used_timestamp' => '16:08:35 09-10-2014',
481
                'timestamp'           => '18:20:53 07-10-2014',
482
            ],
483
            [
484
                'full_path'           => '/var/www/vhosts/RedTube/template/humiliation/germany-vs-brazil.phtml',
485
                'hits'                => 71,
486
                'memory_consumption'  => '4KB',
487
                'last_used_timestamp' => '16:25:15 09-10-2014',
488
                'timestamp'           => '22:01:15 08-10-2014',
489
            ],
490
            [
491
                'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Foo.php',
492
                'hits'                => 19,
493
                'memory_consumption'  => '3KB',
494
                'last_used_timestamp' => '17:31:55 09-10-2014',
495
                'timestamp'           => '22:07:33 08-10-2014',
496
            ],
497
            [
498
                'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Psr/Autoloader.php',
499
                'hits'                => 32,
500
                'memory_consumption'  => '5KB',
501
                'last_used_timestamp' => '16:25:15 09-10-2014',
502
                'timestamp'           => '22:01:15 08-10-2014',
503
            ],
504
        ];
505
506
        $this->assertSame($data, $status->getCachedScripts());
507
    }
508
509
    /**
510
     * @covers OpCacheGUI\OpCache\Status::__construct
511
     * @covers OpCacheGUI\OpCache\Status::getCachedScripts
512
     */
513
    public function testGetCachedScriptsZeroedTimestamp()
514
    {
515
        $statusData = $this->statusData;
516
517
        $statusData['scripts'] = [
518
            [
519
                'full_path'           => '/var/www/vhosts/OpcacheGUI/src/OpCacheGUI/Network/Request.php',
520
                'hits'                => 1,
521
                'memory_consumption'  => 6608,
522
                'last_used'           => 'Thu Oct 09 16:08:35 2014',
523
                'last_used_timestamp' => 1412863715,
524
                'timestamp'           => 0,
525
            ],
526
        ];
527
528
        $status = new Status(
529
            $this->createMock(Byte::class),
530
            $this->createMock(Translator::class),
531
            $statusData
532
        );
533
534
        $this->assertSame([], $status->getCachedScripts());
535
    }
536
537
    /**
538
     * @covers OpCacheGUI\OpCache\Status::__construct
539
     * @covers OpCacheGUI\OpCache\Status::getCachedScriptsForOverview
540
     * @covers OpCacheGUI\OpCache\Status::getCachedScripts
541
     * @covers OpCacheGUI\OpCache\Status::sortCachedScripts
542
     */
543
    public function testGetCachedScriptsForOverviewFilled()
544
    {
545
        $formatter = $this->createMock(Byte::class);
546
        $formatter->method('format')->will($this->onConsecutiveCalls('1KB', '2KB', '3KB', '4KB', '5KB', '6KB'));
547
548
        $status = new Status($formatter, $this->createMock(Translator::class), $this->statusData);
549
550
        $data = [
551
            [
552
                'full_path'           => '/var/www/vhosts/NoTimeStamp/src/Psr/Autoloader.php',
553
                'hits'                => 12876,
554
                'memory_consumption'  => '6KB',
555
                'last_used_timestamp' => '14:25:15 09-10-2014',
556
                'timestamp'           => 'N/A',
557
            ],
558
            [
559
                'full_path'           => '/var/www/vhosts/OpcacheGUI/src/OpCacheGUI/Network/Request.php',
560
                'hits'                => 1,
561
                'memory_consumption'  => '1KB',
562
                'last_used_timestamp' => '14:08:35 09-10-2014',
563
                'timestamp'           => '16:20:53 07-10-2014',
564
            ],
565
            [
566
                'full_path'           => '/var/www/vhosts/OpcacheGUI/template/cached.phtml',
567
                'hits'                => 4,
568
                'memory_consumption'  => '2KB',
569
                'last_used_timestamp' => '14:08:35 09-10-2014',
570
                'timestamp'           => '16:20:53 07-10-2014',
571
            ],
572
            [
573
                'full_path'           => '/var/www/vhosts/RedTube/template/humiliation/germany-vs-brazil.phtml',
574
                'hits'                => 71,
575
                'memory_consumption'  => '4KB',
576
                'last_used_timestamp' => '14:25:15 09-10-2014',
577
                'timestamp'           => '20:01:15 08-10-2014',
578
            ],
579
            [
580
                'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Foo.php',
581
                'hits'                => 19,
582
                'memory_consumption'  => '3KB',
583
                'last_used_timestamp' => '15:31:55 09-10-2014',
584
                'timestamp'           => '20:07:33 08-10-2014',
585
            ],
586
            [
587
                'full_path'           => '/var/www/vhosts/SomeOtherProject/src/Psr/Autoloader.php',
588
                'hits'                => 32,
589
                'memory_consumption'  => '5KB',
590
                'last_used_timestamp' => '14:25:15 09-10-2014',
591
                'timestamp'           => '20:01:15 08-10-2014',
592
            ],
593
        ];
594
595
        $trimmer = $this->createMock(Trimmer::class);
596
        $trimmer->method('trim')->will($this->returnArgument(0));
597
598
        $this->assertSame($data, $status->getCachedScriptsForOverview($trimmer));
599
    }
600
601
}
602