|
4 | 4 |
|
5 | 5 |
|
6 | 6 | use HelloFresh\Stats; |
| 7 | +use HelloFresh\Stats\Bucket; |
| 8 | +use HelloFresh\Stats\Bucket\MetricOperation; |
| 9 | +use HelloFresh\Stats\Bucket\Plain; |
| 10 | +use HelloFresh\Stats\Incrementer; |
| 11 | +use HelloFresh\Stats\State; |
7 | 12 | use PHPUnit\Framework\TestCase; |
8 | 13 |
|
9 | 14 | class LogTest extends TestCase |
10 | 15 | { |
| 16 | + public function setUp() |
| 17 | + { |
| 18 | + mt_srand(time()); |
| 19 | + } |
| 20 | + |
11 | 21 | public function testInstances() |
12 | 22 | { |
13 | 23 | /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */ |
14 | 24 | $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); |
15 | 25 |
|
16 | 26 | $statsClient = new Log($logger); |
17 | 27 | $this->assertInstanceOf(Stats\Timer\Log::class, $statsClient->buildTimer()); |
| 28 | + |
| 29 | + $reflection = new \ReflectionClass($statsClient); |
| 30 | + |
| 31 | + $methodIncrementer = $reflection->getMethod('getIncrementer'); |
| 32 | + $methodIncrementer->setAccessible(true); |
| 33 | + $this->assertInstanceOf(Incrementer\Log::class, $methodIncrementer->invoke($statsClient)); |
| 34 | + |
| 35 | + $methodState = $reflection->getMethod('getState'); |
| 36 | + $methodState->setAccessible(true); |
| 37 | + $this->assertInstanceOf(State\Log::class, $methodState->invoke($statsClient)); |
| 38 | + } |
| 39 | + |
| 40 | + public function testTrackRequest() |
| 41 | + { |
| 42 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */ |
| 43 | + $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); |
| 44 | + |
| 45 | + $logger->expects($this->at(0)) |
| 46 | + ->method('debug') |
| 47 | + ->willReturnCallback(function ($message, array $context) { |
| 48 | + $this->assertEquals('Stats timer finished', $message); |
| 49 | + $this->assertArrayHasKey('metric', $context); |
| 50 | + $this->assertEquals('request.get.hello.world', $context['metric']); |
| 51 | + $this->assertArrayHasKey('elapsed', $context); |
| 52 | + }); |
| 53 | + $logger->expects($this->at(1)) |
| 54 | + ->method('debug') |
| 55 | + ->with('Stats counter incremented', ['metric' => 'request.get.hello.world', 'n' => 1]); |
| 56 | + $logger->expects($this->at(2)) |
| 57 | + ->method('debug') |
| 58 | + ->with('Stats counter incremented', ['metric' => 'request-ok.get.hello.world', 'n' => 1]); |
| 59 | + $logger->expects($this->at(3)) |
| 60 | + ->method('debug') |
| 61 | + ->with('Stats counter incremented', ['metric' => 'total.request', 'n' => 1]); |
| 62 | + $logger->expects($this->at(4)) |
| 63 | + ->method('debug') |
| 64 | + ->with('Stats counter incremented', ['metric' => 'total.request-ok', 'n' => 1]); |
| 65 | + |
| 66 | + $uri = $this->getMockBuilder('\Psr\Http\Message\UriInterface')->getMock(); |
| 67 | + |
| 68 | + $uri->expects($this->atLeastOnce()) |
| 69 | + ->method('getPath') |
| 70 | + ->will($this->returnValue('/hello/world')); |
| 71 | + |
| 72 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Http\Message\RequestInterface $request */ |
| 73 | + $request = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); |
| 74 | + |
| 75 | + $request->expects($this->once()) |
| 76 | + ->method('getMethod') |
| 77 | + ->will($this->returnValue('GET')); |
| 78 | + $request->expects($this->atLeastOnce()) |
| 79 | + ->method('getUri') |
| 80 | + ->will($this->returnValue($uri)); |
| 81 | + |
| 82 | + $statsClient = new Log($logger); |
| 83 | + |
| 84 | + $timer = $statsClient->buildTimer(); |
| 85 | + $statsClient->trackRequest($request, $timer, true); |
| 86 | + } |
| 87 | + |
| 88 | + public function testTrackOperation() |
| 89 | + { |
| 90 | + $n = mt_rand(); |
| 91 | + $section = uniqid('section', true); |
| 92 | + $sectionMetric = Plain::sanitizeMetricName($section); |
| 93 | + |
| 94 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */ |
| 95 | + $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); |
| 96 | + |
| 97 | + $logger->expects($this->at(0)) |
| 98 | + ->method('debug') |
| 99 | + ->willReturnCallback(function ($message, array $context) use ($sectionMetric) { |
| 100 | + $this->assertEquals('Stats timer finished', $message); |
| 101 | + $this->assertArrayHasKey('metric', $context); |
| 102 | + $this->assertEquals($sectionMetric . '-fail.foo.bar.baz', $context['metric']); |
| 103 | + $this->assertArrayHasKey('elapsed', $context); |
| 104 | + }); |
| 105 | + $logger->expects($this->at(1)) |
| 106 | + ->method('debug') |
| 107 | + ->with('Stats counter incremented', ['metric' => $sectionMetric . '.foo.bar.baz', 'n' => $n]); |
| 108 | + $logger->expects($this->at(2)) |
| 109 | + ->method('debug') |
| 110 | + ->with('Stats counter incremented', ['metric' => $sectionMetric . '-fail.foo.bar.baz', 'n' => $n]); |
| 111 | + $logger->expects($this->at(3)) |
| 112 | + ->method('debug') |
| 113 | + ->with('Stats counter incremented', ['metric' => 'total.' . $sectionMetric, 'n' => $n]); |
| 114 | + $logger->expects($this->at(4)) |
| 115 | + ->method('debug') |
| 116 | + ->with('Stats counter incremented', ['metric' => 'total.' . $sectionMetric . '-fail', 'n' => $n]); |
| 117 | + |
| 118 | + $statsClient = new Log($logger); |
| 119 | + |
| 120 | + $timer = $statsClient->buildTimer(); |
| 121 | + $statsClient->trackOperation($section, new MetricOperation(['foo', 'bar', 'baz']), false, $timer, $n); |
| 122 | + } |
| 123 | + |
| 124 | + public function testTrackState() |
| 125 | + { |
| 126 | + $state = mt_rand(); |
| 127 | + $section = uniqid('section', true); |
| 128 | + $sectionMetric = Plain::sanitizeMetricName($section); |
| 129 | + |
| 130 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */ |
| 131 | + $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); |
| 132 | + |
| 133 | + $logger->expects($this->once()) |
| 134 | + ->method('debug') |
| 135 | + ->with('Stats state set', ['metric' => $sectionMetric . '.foo.bar.baz', 'state' => $state]); |
| 136 | + |
| 137 | + $statsClient = new Log($logger); |
| 138 | + $statsClient->trackState($section, new MetricOperation(['foo', 'bar', 'baz']), $state); |
| 139 | + } |
| 140 | + |
| 141 | + public function testHTTPRequestSection() |
| 142 | + { |
| 143 | + $section = uniqid('section', true); |
| 144 | + |
| 145 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */ |
| 146 | + $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); |
| 147 | + |
| 148 | + $statsClient = new Log($logger); |
| 149 | + |
| 150 | + $reflection = new \ReflectionClass($statsClient); |
| 151 | + $reflectionProperty = $reflection->getProperty('httpRequestSection'); |
| 152 | + $reflectionProperty->setAccessible(true); |
| 153 | + |
| 154 | + $this->assertEquals(Bucket::DEFAULT_HTTP_REQUEST_SECTION, $reflectionProperty->getValue($statsClient)); |
| 155 | + |
| 156 | + $statsClient->setHTTPRequestSection($section); |
| 157 | + $this->assertEquals($section, $reflectionProperty->getValue($statsClient)); |
| 158 | + |
| 159 | + $statsClient->resetHTTPRequestSection(); |
| 160 | + $this->assertEquals(Bucket::DEFAULT_HTTP_REQUEST_SECTION, $reflectionProperty->getValue($statsClient)); |
| 161 | + } |
| 162 | + |
| 163 | + public function testTrackRequest_HTTPMetricAlterCallback() |
| 164 | + { |
| 165 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface $logger */ |
| 166 | + $logger = $this->getMockBuilder('\Psr\Log\LoggerInterface')->getMock(); |
| 167 | + |
| 168 | + $logger->expects($this->at(0)) |
| 169 | + ->method('debug') |
| 170 | + ->willReturnCallback(function ($message, array $context) { |
| 171 | + $this->assertEquals('Stats timer finished', $message); |
| 172 | + $this->assertArrayHasKey('metric', $context); |
| 173 | + $this->assertEquals('request.callback.altered.metric', $context['metric']); |
| 174 | + $this->assertArrayHasKey('elapsed', $context); |
| 175 | + }); |
| 176 | + $logger->expects($this->at(1)) |
| 177 | + ->method('debug') |
| 178 | + ->with('Stats counter incremented', ['metric' => 'request.callback.altered.metric', 'n' => 1]); |
| 179 | + $logger->expects($this->at(2)) |
| 180 | + ->method('debug') |
| 181 | + ->with('Stats counter incremented', ['metric' => 'request-ok.callback.altered.metric', 'n' => 1]); |
| 182 | + $logger->expects($this->at(3)) |
| 183 | + ->method('debug') |
| 184 | + ->with('Stats counter incremented', ['metric' => 'total.request', 'n' => 1]); |
| 185 | + $logger->expects($this->at(4)) |
| 186 | + ->method('debug') |
| 187 | + ->with('Stats counter incremented', ['metric' => 'total.request-ok', 'n' => 1]); |
| 188 | + |
| 189 | + $uri = $this->getMockBuilder('\Psr\Http\Message\UriInterface')->getMock(); |
| 190 | + |
| 191 | + $uri->expects($this->atLeastOnce()) |
| 192 | + ->method('getPath') |
| 193 | + ->will($this->returnValue('/hello/world')); |
| 194 | + |
| 195 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Http\Message\RequestInterface $request */ |
| 196 | + $request = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); |
| 197 | + |
| 198 | + $request->expects($this->once()) |
| 199 | + ->method('getMethod') |
| 200 | + ->will($this->returnValue('GET')); |
| 201 | + $request->expects($this->atLeastOnce()) |
| 202 | + ->method('getUri') |
| 203 | + ->will($this->returnValue($uri)); |
| 204 | + |
| 205 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\HelloFresh\Stats\HTTPMetricAlterCallback $alterCallback */ |
| 206 | + $alterCallback = $this->getMockBuilder('\HelloFresh\Stats\HTTPMetricAlterCallback')->getMock(); |
| 207 | + |
| 208 | + $alterCallback->expects($this->once()) |
| 209 | + ->method('__invoke') |
| 210 | + ->willReturn(new MetricOperation(['callback', 'altered', 'metric'])); |
| 211 | + |
| 212 | + $statsClient = new Log($logger); |
| 213 | + $statsClient->setHTTPMetricAlterCallback($alterCallback); |
| 214 | + $this->assertEquals($alterCallback, $statsClient->getHTTPMetricAlterCallback()); |
| 215 | + |
| 216 | + $timer = $statsClient->buildTimer(); |
| 217 | + $statsClient->trackRequest($request, $timer, true); |
18 | 218 | } |
19 | 219 | } |
0 commit comments