|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace HelloFresh\Stats\Bucket; |
| 4 | + |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class HTTPRequestTest extends TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @dataProvider metrics |
| 12 | + * |
| 13 | + * @param string $method |
| 14 | + * @param string $path |
| 15 | + * @param string $section |
| 16 | + * @param string $metric |
| 17 | + */ |
| 18 | + public function testHTTPRequestTest($method, $path, $section, $metric) |
| 19 | + { |
| 20 | + $uri = $this->getMockBuilder('\Psr\Http\Message\UriInterface')->getMock(); |
| 21 | + |
| 22 | + $uri->expects($this->atLeastOnce()) |
| 23 | + ->method('getPath') |
| 24 | + ->will($this->returnValue($path)); |
| 25 | + |
| 26 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Http\Message\RequestInterface $request */ |
| 27 | + $request = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); |
| 28 | + |
| 29 | + $request->expects($this->once()) |
| 30 | + ->method('getMethod') |
| 31 | + ->will($this->returnValue($method)); |
| 32 | + $request->expects($this->atLeastOnce()) |
| 33 | + ->method('getUri') |
| 34 | + ->will($this->returnValue($uri)); |
| 35 | + |
| 36 | + $bucket = new HTTPRequest($section, $request, true, null); |
| 37 | + $this->assertEquals($metric, $bucket->metric()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testHTTPMetricAlterCallback() |
| 41 | + { |
| 42 | + $uri = $this->getMockBuilder('\Psr\Http\Message\UriInterface')->getMock(); |
| 43 | + |
| 44 | + $uri->expects($this->atLeastOnce()) |
| 45 | + ->method('getPath') |
| 46 | + ->will($this->returnValue('/hello/world')); |
| 47 | + |
| 48 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Http\Message\RequestInterface $request */ |
| 49 | + $request = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); |
| 50 | + |
| 51 | + $request->expects($this->once()) |
| 52 | + ->method('getMethod') |
| 53 | + ->will($this->returnValue('GET')); |
| 54 | + $request->expects($this->atLeastOnce()) |
| 55 | + ->method('getUri') |
| 56 | + ->will($this->returnValue($uri)); |
| 57 | + |
| 58 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\HelloFresh\Stats\HTTPMetricAlterCallback $callback */ |
| 59 | + $callback = $this->getMockBuilder('\HelloFresh\Stats\HTTPMetricAlterCallback')->getMock(); |
| 60 | + |
| 61 | + $callback->expects($this->once()) |
| 62 | + ->method('__invoke') |
| 63 | + ->will($this->returnValue(new MetricOperation(['new', 'metric', 'here']))); |
| 64 | + |
| 65 | + $bucket = new HTTPRequest('baz', $request, true, $callback); |
| 66 | + $this->assertEquals('baz.new.metric.here', $bucket->metric()); |
| 67 | + } |
| 68 | + |
| 69 | + public function metrics() |
| 70 | + { |
| 71 | + return [ |
| 72 | + ['POST', '/', 'foo', 'foo.post.-.-'], |
| 73 | + ['GET', '/hello', 'bar', 'bar.get.hello.-'], |
| 74 | + ['GET', '/hello/', 'baz', 'baz.get.hello.-'], |
| 75 | + ['GET', '/hello/world', 'baz', 'baz.get.hello.world'], |
| 76 | + ]; |
| 77 | + } |
| 78 | +} |
0 commit comments