|
10 | 10 | namespace PrivatePackagist\ApiClient\HttpClient\Plugin;
|
11 | 11 |
|
12 | 12 | use GuzzleHttp\Psr7\Request;
|
| 13 | +use Http\Promise\FulfilledPromise; |
13 | 14 | use PHPUnit\Framework\TestCase;
|
14 | 15 |
|
15 | 16 | class PathPrependTest extends TestCase
|
16 | 17 | {
|
17 |
| - public function testPrefixRequestPath() |
| 18 | + /** @var PathPrepend */ |
| 19 | + private $plugin; |
| 20 | + private $next; |
| 21 | + private $first; |
| 22 | + |
| 23 | + protected function setUp() |
18 | 24 | {
|
19 |
| - $request = new Request('GET', '/packages/'); |
20 |
| - $expected = new Request('GET', '/api/packages/'); |
21 |
| - $plugin = new PathPrepend('/api'); |
22 |
| - $callback = $this->getMockBuilder(\stdClass::class) |
23 |
| - ->setMethods(['next']) |
24 |
| - ->getMock() |
25 |
| - ; |
26 |
| - $callback->expects($this->once()) |
27 |
| - ->method('next') |
28 |
| - ->with($expected) |
29 |
| - ; |
30 |
| - |
31 |
| - $plugin->handleRequest($request, [$callback, 'next'], function () { |
32 |
| - }); |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + $this->plugin = new PathPrepend('/api'); |
| 28 | + $this->next = function (Request $request) { |
| 29 | + return new FulfilledPromise($request); |
| 30 | + }; |
| 31 | + $this->first = function () { |
| 32 | + throw new \RuntimeException('Did not expect plugin to call first'); |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @dataProvider pathProvider |
| 38 | + */ |
| 39 | + public function testPrefixApiRequestPath($path, $expectedPath) |
| 40 | + { |
| 41 | + $request = new Request('GET', $path); |
| 42 | + $expected = new Request('GET', $expectedPath); |
| 43 | + |
| 44 | + $promise = $this->plugin->handleRequest($request, $this->next, $this->first); |
| 45 | + |
| 46 | + $this->assertEquals($expected, $promise->wait(true)); |
33 | 47 | }
|
34 | 48 |
|
35 |
| - public function testDontPrefixApiRequestPath() |
| 49 | + public function pathProvider() |
36 | 50 | {
|
37 |
| - $request = new Request('GET', '/api/packages/'); |
38 |
| - $expected = new Request('GET', '/api/packages/'); |
39 |
| - $plugin = new PathPrepend('/api'); |
40 |
| - $callback = $this->getMockBuilder(\stdClass::class) |
41 |
| - ->setMethods(['next']) |
42 |
| - ->getMock() |
43 |
| - ; |
44 |
| - $callback->expects($this->once()) |
45 |
| - ->method('next') |
46 |
| - ->with($expected) |
47 |
| - ; |
48 |
| - |
49 |
| - $plugin->handleRequest($request, [$callback, 'next'], function () { |
50 |
| - }); |
| 51 | + return [ |
| 52 | + ['/api/packages/', '/api/packages/'], |
| 53 | + ['/packages/', '/api/packages/'], |
| 54 | + ]; |
51 | 55 | }
|
52 | 56 | }
|
0 commit comments