/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Support/SupportUriTest.php
367 строк
12 KB
Lucas Michot
[13.x] Enable additional Rector rules (#60854)
22 июл 2026, 17:01
Не верифицирован
22 июл 2026, 17:01
b623055
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Support; use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Support\Stringable; use Illuminate\Support\Uri; use PHPUnit\Framework\TestCase; class SupportUriTest extends TestCase { public function test_can_build_special_urls() { Uri::setUrlGeneratorResolver(fn () => new CustomUrlGeneratorResolver); $this->assertSame('https://laravel.com/to', Uri::to('')->value()); $this->assertSame('https://laravel.com/route', Uri::route('')->value()); $this->assertSame('https://laravel.com/signed-route', Uri::signedRoute('')->value()); $this->assertSame('https://laravel.com/signed-route', Uri::temporarySignedRoute('', '')->value()); $this->assertSame('https://laravel.com/action', Uri::action('')->value()); } public function test_basic_uri_interactions() { $uri = Uri::of($originalUri = 'https://laravel.com/docs/installation'); $this->assertSame('https', $uri->scheme()); $this->assertNull($uri->user()); $this->assertNull($uri->password()); $this->assertSame('laravel.com', $uri->host()); $this->assertNull($uri->port()); $this->assertSame('docs/installation', $uri->path()); $this->assertSame([], $uri->query()->toArray()); $this->assertSame('', (string) $uri->query()); $this->assertSame('', $uri->query()->decode()); $this->assertNull($uri->fragment()); $this->assertEquals($originalUri, (string) $uri); $uri = Uri::of('https://taylor:password@laravel.com/docs/installation?version=1#hello'); $this->assertSame('taylor', $uri->user()); $this->assertSame('password', $uri->password()); $this->assertSame('hello', $uri->fragment()); $this->assertEquals(['version' => 1], $uri->query()->all()); $this->assertEquals(1, $uri->query()->integer('version')); $this->assertSame('taylor:password@laravel.com', $uri->authority()); } public function test_is_empty_and_is_not_empty() { $this->assertTrue(Uri::of('')->isEmpty()); $this->assertFalse(Uri::of('')->isNotEmpty()); $this->assertFalse(Uri::of('https://laravel.com')->isEmpty()); $this->assertTrue(Uri::of('https://laravel.com')->isNotEmpty()); } public function test_without_fragment() { $uri = Uri::of('https://laravel.com/docs/installation#introduction'); $this->assertSame('introduction', $uri->fragment()); $withoutFragment = $uri->withoutFragment(); $this->assertNull($withoutFragment->fragment()); $this->assertSame('https://laravel.com/docs/installation', $withoutFragment->value()); // Original URI should be unchanged (immutability). $this->assertSame('introduction', $uri->fragment()); } public function test_without_fragment_on_uri_without_fragment() { $uri = Uri::of('https://laravel.com/docs'); $withoutFragment = $uri->withoutFragment(); $this->assertNull($withoutFragment->fragment()); $this->assertSame('https://laravel.com/docs', $withoutFragment->value()); } public function test_complicated_query_string_parsing() { $uri = Uri::of('https://example.com/users?key_1=value&key_2[sub_field]=value&key_3[]=value&key_4[9]=value&key_5[][][foo][9]=bar&key.6=value&flag_value'); $this->assertEquals([ 'key_1' => 'value', 'key_2' => [ 'sub_field' => 'value', ], 'key_3' => [ 'value', ], 'key_4' => [ 9 => 'value', ], 'key_5' => [ [ [ 'foo' => [ 9 => 'bar', ], ], ], ], 'key.6' => 'value', 'flag_value' => '', ], $uri->query()->all()); $this->assertSame('key_1=value&key_2[sub_field]=value&key_3[]=value&key_4[9]=value&key_5[][][foo][9]=bar&key.6=value&flag_value', $uri->query()->decode()); } public function test_uri_building() { $uri = Uri::of(); $uri = $uri->withHost('laravel.com') ->withScheme('https') ->withUser('taylor', 'password') ->withPath('/docs/installation') ->withPort(80) ->withQuery(['version' => 1]) ->withFragment('hello'); $expected = 'https://taylor:password@laravel.com:80/docs/installation?version=1#hello'; $this->assertEquals($expected, (string) $uri); $this->assertEquals($expected, $uri->value()); $this->assertEquals($expected, $uri->toString()); } public function test_to_stringable() { $uri = Uri::of('https://laravel.com/docs/installation?version=1#hello'); $stringable = $uri->toStringable(); $this->assertInstanceOf(Stringable::class, $stringable); $this->assertSame('https://laravel.com/docs/installation?version=1#hello', (string) $stringable); $this->assertSame($uri->value(), (string) $stringable); } public function test_complicated_query_string_manipulation() { $uri = Uri::of('https://laravel.com'); $uri = $uri->withQuery([ 'name' => 'Taylor', 'age' => 38, 'role' => [ 'title' => 'Developer', 'focus' => 'PHP', ], 'tags' => [ 'person', 'employee', ], 'flag' => '', ])->withoutQuery(['name']); $this->assertSame('age=38&role[title]=Developer&role[focus]=PHP&tags[0]=person&tags[1]=employee&flag=', $uri->query()->decode()); $this->assertSame('name=Taylor', $uri->replaceQuery(['name' => 'Taylor'])->query()->decode()); // Push onto multi-value and missing items... $uri = Uri::of('https://laravel.com?tags[]=foo'); $this->assertEquals(['tags' => ['foo', 'bar']], $uri->pushOntoQuery('tags', 'bar')->query()->all()); $this->assertEquals(['tags' => ['foo', 'bar', 'baz']], $uri->pushOntoQuery('tags', ['bar', 'baz'])->query()->all()); $this->assertEquals(['tags' => ['foo'], 'names' => ['Taylor']], $uri->pushOntoQuery('names', 'Taylor')->query()->all()); // Push onto single value item... $uri = Uri::of('https://laravel.com?tag=foo'); $this->assertEquals(['tag' => ['foo', 'bar']], $uri->pushOntoQuery('tag', 'bar')->query()->all()); } public function test_query_strings_with_dots_can_be_replaced_or_merged_consistently() { $uri = Uri::of('https://dot.test/?foo.bar=baz'); $this->assertSame('foo.bar=baz&foo[bar]=zab', $uri->withQuery(['foo.bar' => 'zab'])->query()->decode()); $this->assertSame('foo[bar]=zab', $uri->replaceQuery(['foo.bar' => 'zab'])->query()->decode()); } public function test_decoding_the_entire_uri() { $uri = Uri::of('https://laravel.com/docs/11.x/installation')->withQuery(['tags' => ['first', 'second']]); $this->assertSame('https://laravel.com/docs/11.x/installation?tags[0]=first&tags[1]=second', $uri->decode()); } public function test_decoding_the_entire_uri_preserves_the_fragment() { $uri = Uri::of('https://laravel.com/docs/11.x/routing?q=laravel%20docs#route-model-binding'); $this->assertSame('https://laravel.com/docs/11.x/routing?q=laravel docs#route-model-binding', $uri->decode()); } public function test_with_query_if_missing() { // Test adding new parameters while preserving existing ones $uri = Uri::of('https://laravel.com?existing=value'); $uri = $uri->withQueryIfMissing([ 'new' => 'parameter', 'existing' => 'new_value', ]); $this->assertSame('existing=value&new=parameter', $uri->query()->decode()); // Test adding complex nested arrays to empty query string $uri = Uri::of('https://laravel.com'); $uri = $uri->withQueryIfMissing([ 'name' => 'Taylor', 'role' => [ 'title' => 'Developer', 'focus' => 'PHP', ], 'tags' => [ 'person', 'employee', ], ]); $this->assertSame('name=Taylor&role[title]=Developer&role[focus]=PHP&tags[0]=person&tags[1]=employee', $uri->query()->decode()); // Test partial array merging and preserving indexed arrays $uri = Uri::of('https://laravel.com?name=Taylor&tags[0]=person'); $uri = $uri->withQueryIfMissing([ 'name' => 'Changed', 'age' => 38, 'tags' => ['should', 'not', 'change'], ]); $this->assertSame('name=Taylor&tags[0]=person&age=38', $uri->query()->decode()); $this->assertEquals(['name' => 'Taylor', 'tags' => ['person'], 'age' => 38], $uri->query()->all()); $uri = Uri::of('https://laravel.com?user[name]=Taylor'); $uri = $uri->withQueryIfMissing([ 'user' => [ 'name' => 'Should Not Change', 'age' => 38, ], 'settings' => [ 'theme' => 'dark', ], ]); $this->assertEquals([ 'user' => [ 'name' => 'Taylor', ], 'settings' => [ 'theme' => 'dark', ], ], $uri->query()->all()); } public function test_with_query_prevents_empty_query_string() { $uri = Uri::of('https://laravel.com'); $this->assertSame('https://laravel.com', (string) $uri); $this->assertSame('https://laravel.com', (string) $uri->withQuery([])); } public function test_path_segments() { $uri = Uri::of('https://laravel.com'); $this->assertSame([], $uri->pathSegments()->toArray()); $uri = Uri::of('https://laravel.com/one/two/three'); $this->assertEquals(['one', 'two', 'three'], $uri->pathSegments()->toArray()); $this->assertSame('one', $uri->pathSegments()->first()); $uri = Uri::of('https://laravel.com/one/two/three?foo=bar'); $this->assertCount(3, $uri->pathSegments()); $uri = Uri::of('https://laravel.com/one/two/three/?foo=bar'); $this->assertCount(3, $uri->pathSegments()); $uri = Uri::of('https://laravel.com/one/two/three/#foo=bar'); $this->assertCount(3, $uri->pathSegments()); } public function test_macroable() { Uri::macro('myMacro', function () { return $this->withPath('foobar'); }); $uri = new Uri('https://laravel.com/'); $this->assertSame('https://laravel.com/foobar', (string) $uri->myMacro()); } } class CustomUrlGeneratorResolver implements UrlGenerator { public function current() { return 'https://laravel.com/current'; } public function previous($fallback = false) { return 'https://laravel.com/previous'; } public function to($path, $extra = [], $secure = null) { return 'https://laravel.com/to'; } public function secure($path, $parameters = []) { return 'https://laravel.com/secure'; } public function asset($path, $secure = null) { return 'https://laravel.com/asset'; } public function route($name, $parameters = [], $absolute = true) { return 'https://laravel.com/route'; } public function signedRoute($name, $parameters = [], $expiration = null, $absolute = true) { return 'https://laravel.com/signed-route'; } public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true) { return 'https://laravel.com/temporary-signed-route'; } public function query($path, $query = [], $extra = [], $secure = null) { return 'https://laravel.com/query'; } public function action($action, $parameters = [], $absolute = true) { return 'https://laravel.com/action'; } public function getRootControllerNamespace() { return 'App\\Http\\Controllers'; } public function setRootControllerNamespace($rootNamespace) { return $this; } }