request('GET', $url); $this->assertSame( Response::HTTP_OK, $client->getResponse()->getStatusCode(), sprintf('The %s public URL loads correctly.', $url) ); } /** * A good practice for tests is to not use the service container, to make * them more robust. However, in this example we must access to the container * to get the entity manager and make a database query. The reason is that * blog post fixtures are randomly generated and there's no guarantee that * a given blog post slug will be available. */ public function testPublicBlogPost() { $client = static::createClient(); // the service container is always available via the test client $blogPost = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1); $client->request('GET', sprintf('/en/blog/posts/%s', $blogPost->getSlug())); $this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); } /** * The application contains a lot of secure URLs which shouldn't be * publicly accessible. This tests ensures that whenever a user tries to * access one of those pages, a redirection to the login form is performed. * * @dataProvider getSecureUrls */ public function testSecureUrls(string $url) { $client = static::createClient(); $client->request('GET', $url); $response = $client->getResponse(); $this->assertSame(Response::HTTP_FOUND, $response->getStatusCode()); $this->assertSame( 'http://localhost/en/login', $response->getTargetUrl(), sprintf('The %s secure URL redirects to the login form.', $url) ); } public function getPublicUrls() { yield ['/']; yield ['/en/blog/']; yield ['/en/login']; } public function getSecureUrls() { yield ['/en/admin/post/']; yield ['/en/admin/post/new']; yield ['/en/admin/post/1']; yield ['/en/admin/post/1/edit']; } }