|
1
|
|
|
<?php |
|
2
|
|
|
namespace FwlibTest\Test\Benchmark; |
|
3
|
|
|
|
|
4
|
|
|
use Fwlib\Test\Benchmark\Benchmark; |
|
5
|
|
|
use Fwlib\Test\Benchmark\Group; |
|
6
|
|
|
use Fwlib\Test\Benchmark\Marker; |
|
7
|
|
|
use Fwlib\Test\Benchmark\RendererInterface; |
|
8
|
|
|
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase; |
|
9
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @copyright Copyright 2015 Fwolf |
|
13
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
|
14
|
|
|
*/ |
|
15
|
|
|
class BenchmarkTest extends PHPUnitTestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param string[] $methods |
|
19
|
|
|
* @return MockObject|Benchmark |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function buildMock(array $methods = null) |
|
22
|
|
|
{ |
|
23
|
|
|
$mock = $this->getMock( |
|
24
|
|
|
Benchmark::class, |
|
25
|
|
|
$methods |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
return $mock; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
View Code Duplication |
public function testAutoStop() |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$benchmark = $this->buildMock(['getCurrentGroup', 'stop']); |
|
35
|
|
|
|
|
36
|
|
|
$group = (new Group(0))->setBeginTime(42); |
|
37
|
|
|
$benchmark->expects($this->once()) |
|
|
|
|
|
|
38
|
|
|
->method('getCurrentGroup') |
|
39
|
|
|
->willReturn($group); |
|
40
|
|
|
|
|
41
|
|
|
$benchmark->expects($this->once()) |
|
42
|
|
|
->method('stop'); |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$this->reflectionCall($benchmark, 'autoStop'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public function testDisplay() |
|
50
|
|
|
{ |
|
51
|
|
|
$benchmark = $this->buildMock(['getOutput']); |
|
52
|
|
|
$benchmark->expects($this->once()) |
|
|
|
|
|
|
53
|
|
|
->method('getOutput') |
|
54
|
|
|
->willReturn('dummy output'); |
|
55
|
|
|
|
|
56
|
|
|
$this->expectOutputString('dummy output'); |
|
57
|
|
|
$benchmark->display(); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
public function testGetCurrentGroup() |
|
62
|
|
|
{ |
|
63
|
|
|
$group = new Group(3); |
|
64
|
|
|
$benchmark = $this->buildMock(); |
|
65
|
|
|
$this->reflectionSet($benchmark, 'groups', [3 => $group]); |
|
66
|
|
|
|
|
67
|
|
|
$this->reflectionSet($benchmark, 'groupId', 1); |
|
68
|
|
|
$this->assertNull($this->reflectionCall($benchmark, 'getCurrentGroup')); |
|
69
|
|
|
|
|
70
|
|
|
$this->reflectionSet($benchmark, 'groupId', 3); |
|
71
|
|
|
$this->assertInstanceOf( |
|
72
|
|
|
Group::class, |
|
73
|
|
|
$this->reflectionCall($benchmark, 'getCurrentGroup') |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
public function testGetOutput() |
|
79
|
|
|
{ |
|
80
|
|
|
$benchmark = $this->buildMock(['autoStop']); |
|
81
|
|
|
$benchmark->expects($this->once()) |
|
|
|
|
|
|
82
|
|
|
->method('autoStop'); |
|
83
|
|
|
|
|
84
|
|
|
$benchmark->getOutput(); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
public function testGetTime() |
|
89
|
|
|
{ |
|
90
|
|
|
$benchmark = $this->buildMock(); |
|
91
|
|
|
|
|
92
|
|
|
$time = $this->reflectionCall($benchmark, 'getTime'); |
|
93
|
|
|
// Convert float to string with strval() will loose precision |
|
94
|
|
|
$timeStr = sprintf('%f', $time); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertRegExp('/\d+\.\d{3,}/', $timeStr); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
public function testMark() |
|
101
|
|
|
{ |
|
102
|
|
|
$benchmark = $this->buildMock(); |
|
103
|
|
|
|
|
104
|
|
|
$benchmark->start(); |
|
|
|
|
|
|
105
|
|
|
$benchmark->mark('m1'); |
|
|
|
|
|
|
106
|
|
|
$benchmark->mark('', 'red'); |
|
107
|
|
|
|
|
108
|
|
|
$markerId = $this->reflectionGet($benchmark, 'markerId'); |
|
109
|
|
|
$this->assertEquals(2, $markerId); |
|
110
|
|
|
|
|
111
|
|
|
// When stop, marker id is reset |
|
112
|
|
|
$benchmark->stop(); |
|
|
|
|
|
|
113
|
|
|
$markerId = $this->reflectionGet($benchmark, 'markerId'); |
|
114
|
|
|
$this->assertEquals(0, $markerId); |
|
115
|
|
|
|
|
116
|
|
|
/** @var Marker[] $markers */ |
|
117
|
|
|
$markers = $this->reflectionGet($benchmark, 'markers')[0]; |
|
118
|
|
|
$marker0 = $markers[0]; |
|
119
|
|
|
$marker1 = $markers[1]; |
|
120
|
|
|
$this->assertEquals('m1', $marker0->getDescription()); |
|
121
|
|
|
$this->assertNotEmpty($marker1->getDescription()); |
|
122
|
|
|
$this->assertEquals('red', $marker1->getColor()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
public function testSetGetRenderer() |
|
127
|
|
|
{ |
|
128
|
|
|
$benchmark = $this->buildMock(); |
|
129
|
|
|
|
|
130
|
|
|
/** @var MockObject|RendererInterface $renderer */ |
|
131
|
|
|
$renderer = $this->getMockBuilder(RendererInterface::class) |
|
132
|
|
|
->getMockForAbstractClass(); |
|
133
|
|
|
|
|
134
|
|
|
$benchmark->setRenderer($renderer); |
|
|
|
|
|
|
135
|
|
|
$newRenderer = $this->reflectionCall($benchmark, 'getRenderer'); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertInstanceOf(RendererInterface::class, $newRenderer); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.