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