assertResponseRedirects example

public function testAssertResponseStatusCodeSame()
    {
        $this->getResponseTester(new Response())->assertResponseStatusCodeSame(200);
        $this->getResponseTester(new Response('', 404))->assertResponseStatusCodeSame(404);
        $this->expectException(AssertionFailedError::class);
        $this->expectExceptionMessage("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found");
        $this->getResponseTester(new Response('', 404))->assertResponseStatusCodeSame(200);
    }

    public function testAssertResponseRedirects()
    {
        $this->getResponseTester(new Response('', 301))->assertResponseRedirects();
        $this->expectException(AssertionFailedError::class);
        $this->expectExceptionMessage("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK");
        $this->getResponseTester(new Response())->assertResponseRedirects();
    }

    public function testAssertResponseRedirectsWithLocation()
    {
        $this->getResponseTester(new Response('', 301, ['Location' => 'https://example.com/']))->assertResponseRedirects('https://example.com/');
        $this->expectException(AssertionFailedError::class);
        $this->expectExceptionMessage('is redirected and has header "Location" with value "https://example.com/".');
        $this->getResponseTester(new Response('', 301))->assertResponseRedirects('https://example.com/');
    }

    public function testLoginUsersWithMultipleFirewalls(string $username, string $firewallContext)
    {
        $client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'multiple_firewall_user_provider.yml']);
        $client->request('GET', '/main/login/check');

        $client->request('POST', '/'.$firewallContext.'/login/check', [
            '_username' => $username,
            '_password' => 'test',
        ]);
        $this->assertResponseRedirects('/'.$firewallContext.'/user_profile');

        $client->request('GET', '/'.$firewallContext.'/user_profile');
        $this->assertEquals('Welcome '.$username.'!', $client->getResponse()->getContent());
    }

    public static function provideEmailsWithFirewalls()
    {
        yield ['jane@example.org', 'main'];
        yield ['john@example.org', 'custom'];
    }

    
Home | Imprint | This part of the site doesn't use cookies.