|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpClient\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 16 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 17 | +use Symfony\Component\HttpClient\UriTemplateHttpClient; |
| 18 | + |
| 19 | +final class UriTemplateHttpClientTest extends TestCase |
| 20 | +{ |
| 21 | + public function testExpanderIsCalled() |
| 22 | + { |
| 23 | + $client = new UriTemplateHttpClient( |
| 24 | + new MockHttpClient(), |
| 25 | + function (string $url, array $vars): string { |
| 26 | + $this->assertSame('https://foo.tld/{version}/{resource}{?page}', $url); |
| 27 | + $this->assertSame([ |
| 28 | + 'version' => 'v2', |
| 29 | + 'resource' => 'users', |
| 30 | + 'page' => 33, |
| 31 | + ], $vars); |
| 32 | + |
| 33 | + return 'https://foo.tld/v2/users?page=33'; |
| 34 | + }, |
| 35 | + [ |
| 36 | + 'version' => 'v2', |
| 37 | + ], |
| 38 | + ); |
| 39 | + $this->assertSame('https://foo.tld/v2/users?page=33', $client->request('GET', 'https://foo.tld/{version}/{resource}{?page}', [ |
| 40 | + 'vars' => [ |
| 41 | + 'resource' => 'users', |
| 42 | + 'page' => 33, |
| 43 | + ], |
| 44 | + ])->getInfo('url')); |
| 45 | + } |
| 46 | + |
| 47 | + public function testWithOptionsAppendsVarsToDefaultVars() |
| 48 | + { |
| 49 | + $client = new UriTemplateHttpClient( |
| 50 | + new MockHttpClient(), |
| 51 | + function (string $url, array $vars): string { |
| 52 | + $this->assertSame('https://foo.tld/{bar}', $url); |
| 53 | + $this->assertSame([ |
| 54 | + 'bar' => 'ccc', |
| 55 | + ], $vars); |
| 56 | + |
| 57 | + return 'https://foo.tld/ccc'; |
| 58 | + }, |
| 59 | + ); |
| 60 | + $this->assertSame('https://foo.tld/{bar}', $client->request('GET', 'https://foo.tld/{bar}')->getInfo('url')); |
| 61 | + |
| 62 | + $client = $client->withOptions([ |
| 63 | + 'vars' => [ |
| 64 | + 'bar' => 'ccc', |
| 65 | + ], |
| 66 | + ]); |
| 67 | + $this->assertSame('https://foo.tld/ccc', $client->request('GET', 'https://foo.tld/{bar}')->getInfo('url')); |
| 68 | + } |
| 69 | + |
| 70 | + public function testExpanderIsNotCalledWithEmptyVars() |
| 71 | + { |
| 72 | + $this->expectNotToPerformAssertions(); |
| 73 | + |
| 74 | + $client = new UriTemplateHttpClient(new MockHttpClient(), $this->fail(...)); |
| 75 | + $client->request('GET', 'https://foo.tld/bar', [ |
| 76 | + 'vars' => [], |
| 77 | + ]); |
| 78 | + } |
| 79 | + |
| 80 | + public function testExpanderIsNotCalledWithNoVarsAtAll() |
| 81 | + { |
| 82 | + $this->expectNotToPerformAssertions(); |
| 83 | + |
| 84 | + $client = new UriTemplateHttpClient(new MockHttpClient(), $this->fail(...)); |
| 85 | + $client->request('GET', 'https://foo.tld/bar'); |
| 86 | + } |
| 87 | + |
| 88 | + public function testRequestWithNonArrayVarsOption() |
| 89 | + { |
| 90 | + $this->expectException(\InvalidArgumentException::class); |
| 91 | + $this->expectExceptionMessage('The "vars" option must be an array.'); |
| 92 | + |
| 93 | + (new UriTemplateHttpClient(new MockHttpClient()))->request('GET', 'https://foo.tld', [ |
| 94 | + 'vars' => 'should be an array', |
| 95 | + ]); |
| 96 | + } |
| 97 | + |
| 98 | + public function testWithOptionsWithNonArrayVarsOption() |
| 99 | + { |
| 100 | + $this->expectException(\InvalidArgumentException::class); |
| 101 | + $this->expectExceptionMessage('The "vars" option must be an array.'); |
| 102 | + |
| 103 | + (new UriTemplateHttpClient(new MockHttpClient()))->withOptions([ |
| 104 | + 'vars' => new \stdClass(), |
| 105 | + ]); |
| 106 | + } |
| 107 | + |
| 108 | + public function testVarsOptionIsNotPropagated() |
| 109 | + { |
| 110 | + $client = new UriTemplateHttpClient( |
| 111 | + new MockHttpClient(function (string $method, string $url, array $options): MockResponse { |
| 112 | + $this->assertArrayNotHasKey('vars', $options); |
| 113 | + |
| 114 | + return new MockResponse(); |
| 115 | + }), |
| 116 | + static fn (): string => 'ccc', |
| 117 | + ); |
| 118 | + |
| 119 | + $client->withOptions([ |
| 120 | + 'vars' => [ |
| 121 | + 'foo' => 'bar', |
| 122 | + ], |
| 123 | + ])->request('GET', 'https://foo.tld', [ |
| 124 | + 'vars' => [ |
| 125 | + 'foo2' => 'bar2', |
| 126 | + ], |
| 127 | + ]); |
| 128 | + } |
| 129 | +} |
0 commit comments