Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 687d5ea

Browse files
committed
Bucket package covered with tests
1 parent e548b35 commit 687d5ea

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed

src/Bucket/HTTPRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public function __construct($section, RequestInterface $request, $success, HTTPM
2323

2424
/**
2525
* @param RequestInterface $request
26-
* @param HTTPMetricAlterCallback $callback
26+
* @param HTTPMetricAlterCallback|null $callback
2727
*
2828
* @return MetricOperation|mixed
2929
*/
30-
public function buildMetricOperation(RequestInterface $request, HTTPMetricAlterCallback $callback)
30+
public function buildMetricOperation(RequestInterface $request, HTTPMetricAlterCallback $callback = null)
3131
{
32-
$operation = new MetricOperation(strtolower($request->getMethod()));
32+
$operation = new MetricOperation([strtolower($request->getMethod())]);
3333
if ($request->getUri()->getPath() !== '/') {
3434
$partsFilled = 1;
3535
foreach (explode('/', $request->getUri()->getPath()) as $fragment) {

tests/Bucket/HTTPRequestTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Bucket;
4+
5+
6+
use PHPUnit\Framework\TestCase;
7+
8+
class MetricOperationTest extends TestCase
9+
{
10+
/**
11+
* @dataProvider operations
12+
*
13+
* @param array $result
14+
* @param array $initial
15+
*/
16+
public function testMetricOperation(array $result, array $initial)
17+
{
18+
$this->assertEquals($result, (new MetricOperation($initial))->toArray());
19+
}
20+
21+
public function operations()
22+
{
23+
return [
24+
[
25+
['-', '-', '-'],
26+
[],
27+
],
28+
[
29+
['foo', '-', '-'],
30+
['foo'],
31+
],
32+
[
33+
['foo', 'bar', '-'],
34+
['foo', 'bar'],
35+
],
36+
[
37+
['foo', 'bar', 'baz'],
38+
['foo', 'bar', 'baz'],
39+
],
40+
[
41+
['foo', 'bar', 'baz'],
42+
['foo', 'bar', 'baz', 'qux'],
43+
],
44+
[
45+
['foo', 'bar', 'baz'],
46+
['one' => 'foo', 'two' => 'bar', 'three' => 'baz', 'four' => 'qux'],
47+
],
48+
];
49+
}
50+
}

0 commit comments

Comments
 (0)