|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Testing; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Routing\Registrar; |
| 6 | +use Illuminate\Http\RedirectResponse; |
| 7 | +use Illuminate\Routing\UrlGenerator; |
| 8 | +use Illuminate\Support\Facades\Facade; |
| 9 | +use Orchestra\Testbench\TestCase; |
| 10 | + |
| 11 | +class AssertRedirectToRouteTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Illuminate\Contracts\Routing\Registrar |
| 15 | + */ |
| 16 | + private $router; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Illuminate\Routing\UrlGenerator |
| 20 | + */ |
| 21 | + private $urlGenerator; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + $this->router = $this->app->make(Registrar::class); |
| 28 | + |
| 29 | + $this->router |
| 30 | + ->get('named-route') |
| 31 | + ->name('named-route'); |
| 32 | + |
| 33 | + $this->router |
| 34 | + ->get('named-route-with-param/{param}') |
| 35 | + ->name('named-route-with-param'); |
| 36 | + |
| 37 | + $this->urlGenerator = $this->app->make(UrlGenerator::class); |
| 38 | + } |
| 39 | + |
| 40 | + public function testAssertRedirectToRouteWithRouteName() |
| 41 | + { |
| 42 | + $this->router->get('test-route', function () { |
| 43 | + return new RedirectResponse($this->urlGenerator->route('named-route')); |
| 44 | + }); |
| 45 | + |
| 46 | + $this->get('test-route') |
| 47 | + ->assertRedirectToRoute('named-route'); |
| 48 | + } |
| 49 | + |
| 50 | + public function testAssertRedirectToRouteWithRouteNameAndParams() |
| 51 | + { |
| 52 | + $this->router->get('test-route', function () { |
| 53 | + return new RedirectResponse($this->urlGenerator->route('named-route-with-param', 'hello')); |
| 54 | + }); |
| 55 | + |
| 56 | + $this->router->get('test-route-with-extra-param', function () { |
| 57 | + return new RedirectResponse($this->urlGenerator->route('named-route-with-param', [ |
| 58 | + 'param' => 'foo', |
| 59 | + 'extra' => 'another', |
| 60 | + ])); |
| 61 | + }); |
| 62 | + |
| 63 | + $this->get('test-route') |
| 64 | + ->assertRedirectToRoute('named-route-with-param', 'hello'); |
| 65 | + |
| 66 | + $this->get('test-route-with-extra-param') |
| 67 | + ->assertRedirectToRoute('named-route-with-param', [ |
| 68 | + 'param' => 'foo', |
| 69 | + 'extra' => 'another', |
| 70 | + ]); |
| 71 | + } |
| 72 | + |
| 73 | + protected function tearDown(): void |
| 74 | + { |
| 75 | + parent::tearDown(); |
| 76 | + |
| 77 | + Facade::setFacadeApplication(null); |
| 78 | + } |
| 79 | +} |
0 commit comments