|
| 1 | +<?php |
| 2 | +namespace HelloFresh\Stats\HTTPMetricAlterCallback; |
| 3 | + |
| 4 | +use HelloFresh\Stats\Bucket; |
| 5 | +use HelloFresh\Stats\Bucket\MetricOperation; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class HasIDAtSecondLevelTest extends TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @dataProvider defaultSectionTestsProvider |
| 12 | + * |
| 13 | + * @param MetricOperation $inputMetric |
| 14 | + * @param MetricOperation $result |
| 15 | + * @param array $map |
| 16 | + */ |
| 17 | + public function testDefaultSectionTests(MetricOperation $inputMetric, MetricOperation $result, array $map) |
| 18 | + { |
| 19 | + $uri = $this->getMockBuilder('\Psr\Http\Message\UriInterface')->getMock(); |
| 20 | + |
| 21 | + $uri->expects($this->atLeastOnce()) |
| 22 | + ->method('getPath') |
| 23 | + ->will($this->returnValue(sprintf('/%s/%s', $inputMetric[1], $inputMetric[2]))); |
| 24 | + |
| 25 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Http\Message\RequestInterface $request */ |
| 26 | + $request = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); |
| 27 | + |
| 28 | + $request->expects($this->atLeastOnce()) |
| 29 | + ->method('getUri') |
| 30 | + ->will($this->returnValue($uri)); |
| 31 | + |
| 32 | + $callback = new HasIDAtSecondLevel($map); |
| 33 | + $this->assertEquals($result->toArray(), $callback($inputMetric, $request)->toArray()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testRegisterSectionTest() |
| 37 | + { |
| 38 | + $inputMetric = new MetricOperation(['get', 'users', 'edit']); |
| 39 | + |
| 40 | + $uri = $this->getMockBuilder('\Psr\Http\Message\UriInterface')->getMock(); |
| 41 | + |
| 42 | + $uri->expects($this->atLeastOnce()) |
| 43 | + ->method('getPath') |
| 44 | + ->will($this->returnValue(sprintf('/%s/%s', $inputMetric[1], $inputMetric[2]))); |
| 45 | + |
| 46 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Http\Message\RequestInterface $request */ |
| 47 | + $request = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); |
| 48 | + |
| 49 | + $request->expects($this->atLeastOnce()) |
| 50 | + ->method('getUri') |
| 51 | + ->will($this->returnValue($uri)); |
| 52 | + |
| 53 | + $callback = new HasIDAtSecondLevel(['users' => 'edit']); |
| 54 | + $this->assertEquals($inputMetric->toArray(), $callback($inputMetric, $request)->toArray()); |
| 55 | + |
| 56 | + $callback->registerSectionTest('edit', function ($pathSection) { |
| 57 | + return $pathSection == 'edit'; |
| 58 | + }); |
| 59 | + $this->assertEquals( |
| 60 | + (new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]))->toArray(), |
| 61 | + $callback($inputMetric, $request)->toArray() |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dataProvider createFromStringMapProvider |
| 67 | + * @expectedException \InvalidArgumentException |
| 68 | + * @expectedExceptionMessage Invalid sections format |
| 69 | + * |
| 70 | + * @param string $map |
| 71 | + */ |
| 72 | + public function testCreateFromStringMap_InvalidFormat($map) |
| 73 | + { |
| 74 | + HasIDAtSecondLevel::createFromStringMap($map); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @expectedException \InvalidArgumentException |
| 79 | + * @expectedExceptionMessage Unknown section test callback name: foo |
| 80 | + */ |
| 81 | + public function testCreateFromStringMap_UnknownSectionTest() |
| 82 | + { |
| 83 | + HasIDAtSecondLevel::createFromStringMap('users:foo'); |
| 84 | + } |
| 85 | + |
| 86 | + public function testCreateFromStringMap() |
| 87 | + { |
| 88 | + $inputMetric1 = new MetricOperation(['get', 'users', '1']); |
| 89 | + $inputMetric2 = new MetricOperation(['get', 'users', 'foo']); |
| 90 | + |
| 91 | + $uri = $this->getMockBuilder('\Psr\Http\Message\UriInterface')->getMock(); |
| 92 | + |
| 93 | + $uri->expects($this->at(0)) |
| 94 | + ->method('getPath') |
| 95 | + ->will($this->returnValue(sprintf('/%s/%s', $inputMetric1[1], $inputMetric1[2]))); |
| 96 | + $uri->expects($this->at(1)) |
| 97 | + ->method('getPath') |
| 98 | + ->will($this->returnValue(sprintf('/%s/%s', $inputMetric2[1], $inputMetric2[2]))); |
| 99 | + |
| 100 | + /** @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Http\Message\RequestInterface $request */ |
| 101 | + $request = $this->getMockBuilder('\Psr\Http\Message\RequestInterface')->getMock(); |
| 102 | + |
| 103 | + $request->expects($this->atLeastOnce()) |
| 104 | + ->method('getUri') |
| 105 | + ->will($this->returnValue($uri)); |
| 106 | + |
| 107 | + $callback = HasIDAtSecondLevel::createFromStringMap('users:foo', ['foo' => function ($pathSection) { |
| 108 | + return $pathSection == 'foo'; |
| 109 | + }]); |
| 110 | + |
| 111 | + $this->assertEquals( |
| 112 | + $inputMetric1->toArray(), |
| 113 | + $callback($inputMetric1, $request)->toArray() |
| 114 | + ); |
| 115 | + $this->assertEquals( |
| 116 | + (new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]))->toArray(), |
| 117 | + $callback($inputMetric2, $request)->toArray() |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + public function defaultSectionTestsProvider() |
| 122 | + { |
| 123 | + return [ |
| 124 | + // GET /users/1 |
| 125 | + [ |
| 126 | + new MetricOperation(['get', 'users', '1']), |
| 127 | + new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]), |
| 128 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_TRUE], |
| 129 | + ], |
| 130 | + [ |
| 131 | + new MetricOperation(['get', 'users', '1']), |
| 132 | + new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]), |
| 133 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_IS_NUMERIC], |
| 134 | + ], |
| 135 | + [ |
| 136 | + new MetricOperation(['get', 'users', '1']), |
| 137 | + new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]), |
| 138 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_IS_NOT_EMPTY], |
| 139 | + ], |
| 140 | + // GET /users/edit |
| 141 | + [ |
| 142 | + new MetricOperation(['get', 'users', 'edit']), |
| 143 | + new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]), |
| 144 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_TRUE], |
| 145 | + ], |
| 146 | + [ |
| 147 | + new MetricOperation(['get', 'users', 'edit']), |
| 148 | + new MetricOperation(['get', 'users', 'edit']), |
| 149 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_IS_NUMERIC], |
| 150 | + ], |
| 151 | + [ |
| 152 | + new MetricOperation(['get', 'users', 'edit']), |
| 153 | + new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]), |
| 154 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_IS_NOT_EMPTY], |
| 155 | + ], |
| 156 | + // GET /users |
| 157 | + [ |
| 158 | + new MetricOperation(['get', 'users']), |
| 159 | + new MetricOperation(['get', 'users', Bucket::METRIC_ID_PLACEHOLDER]), |
| 160 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_TRUE], |
| 161 | + ], |
| 162 | + [ |
| 163 | + new MetricOperation(['get', 'users']), |
| 164 | + new MetricOperation(['get', 'users', Bucket::METRIC_EMPTY_PLACEHOLDER]), |
| 165 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_IS_NUMERIC], |
| 166 | + ], |
| 167 | + [ |
| 168 | + new MetricOperation(['get', 'users']), |
| 169 | + new MetricOperation(['get', 'users', Bucket::METRIC_EMPTY_PLACEHOLDER]), |
| 170 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_IS_NOT_EMPTY], |
| 171 | + ], |
| 172 | + // does not match |
| 173 | + [ |
| 174 | + new MetricOperation(['get', 'clients']), |
| 175 | + new MetricOperation(['get', 'clients', Bucket::METRIC_EMPTY_PLACEHOLDER]), |
| 176 | + ['users' => HasIDAtSecondLevel::SECTION_TEST_TRUE], |
| 177 | + ], |
| 178 | + ]; |
| 179 | + } |
| 180 | + |
| 181 | + public function createFromStringMapProvider() |
| 182 | + { |
| 183 | + return [ |
| 184 | + ['foo'], |
| 185 | + ['foo:bar:baz'], |
| 186 | + ["foo\n"], |
| 187 | + ["foo:bar\nbaz"], |
| 188 | + ]; |
| 189 | + } |
| 190 | +} |
0 commit comments