1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Mike van Riel <[email protected]> |
9
|
|
|
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
11
|
|
|
* @link http://phpdoc.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Parser\Middleware; |
15
|
|
|
|
16
|
|
|
use phpDocumentor\Parser\Middleware\StopwatchMiddleware; |
17
|
|
|
use phpDocumentor\Reflection\File\LocalFile; |
18
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand; |
19
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Psr\Log\LogLevel; |
23
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
24
|
|
|
use Symfony\Component\Stopwatch\StopwatchEvent; |
25
|
|
|
use Symfony\Component\Stopwatch\StopwatchPeriod; |
26
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @coversDefaultClass \phpDocumentor\Parser\Middleware\StopwatchMiddleware |
30
|
|
|
* @covers ::<private> |
31
|
|
|
* @covers ::__construct |
32
|
|
|
*/ |
33
|
|
|
final class StopwatchMiddlewareTest extends TestCase |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @covers ::execute |
37
|
|
|
*/ |
38
|
|
|
public function testThatMemoryUsageIsLogged() |
39
|
|
|
{ |
40
|
|
|
$commandFile = new LocalFile(__FILE__); |
41
|
|
|
$command = new CreateCommand($commandFile, new ProjectFactoryStrategies([])); |
42
|
|
|
|
43
|
|
|
$logger = $this->prophesize(LoggerInterface::class); |
44
|
|
|
$logger |
45
|
|
|
->log(LogLevel::DEBUG, '>> Memory after processing of file: 1.14 megabytes (+1,172 kilobytes)', []) |
46
|
|
|
->shouldBeCalled(); |
47
|
|
|
$logger |
48
|
|
|
->log(LogLevel::DEBUG, '>> Memory after processing of file: 1.24 megabytes (+98 kilobytes)', []) |
49
|
|
|
->shouldBeCalled(); |
50
|
|
|
|
51
|
|
|
$stopwatch = $this->prophesize(Stopwatch::class); |
52
|
|
|
$stopwatch->lap('parser.parse')->willReturn( |
53
|
|
|
$this->givenAStopwatchEventWithMemoryTotal(1200000), |
54
|
|
|
$this->givenAStopwatchEventWithMemoryTotal(1300000) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$middleware = new StopwatchMiddleware($stopwatch->reveal(), $logger->reveal()); |
58
|
|
|
|
59
|
|
|
// triggering twice should result in two stopwatch events where the second shows the diff between the first |
60
|
|
|
// and second |
61
|
|
|
$middleware->execute($command, function() { return 'result'; }); |
62
|
|
|
$result = $middleware->execute($command, function() { return 'result'; }); |
63
|
|
|
|
64
|
|
|
$this->assertSame('result', $result); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function givenAStopwatchEventWithMemoryTotal(int $memory): StopwatchEvent |
68
|
|
|
{ |
69
|
|
|
$period = $this->prophesize(StopwatchPeriod::class); |
70
|
|
|
$period->getMemory()->willReturn($memory); |
71
|
|
|
|
72
|
|
|
$event = $this->prophesize(StopwatchEvent::class); |
73
|
|
|
$event->getPeriods()->willReturn([$period->reveal()]); |
74
|
|
|
|
75
|
|
|
return $event->reveal(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|