. */ declare(strict_types=1); namespace App\Tests\Controller; use Doctrine\ORM\EntityManagerInterface; use Proxies\__CG__\App\Entity\UserSystem\User; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; /** * @group slow * @group DB */ class RedirectControllerTest extends WebTestCase { protected $em; protected $userRepo; protected $client; protected function setUp(): void { $this->client = static::createClient([], [ 'PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'test', ]); $this->client->disableReboot(); $this->client->catchExceptions(false); $this->em = self::$container->get(EntityManagerInterface::class); $this->userRepo = $this->em->getRepository(User::class); } public function urlMatchDataProvider(): array { return [ ['/', true], ['/part/2/info', true], ['/part/de/2', true], ['/part/en/', true], ['/de/', false], ['/de_DE/', false], ['/en/', false], ['/en_US/', false], ]; } /** * Test if a certain request to an url will be redirected. * * @dataProvider urlMatchDataProvider * @group slow */ public function testUrlMatch($url, $expect_redirect): void { //$client = static::createClient(); $this->client->request('GET', $url); $response = $this->client->getResponse(); if ($expect_redirect) { $this->assertSame(302, $response->getStatusCode()); } $this->assertSame($expect_redirect, $response->isRedirect()); } public function urlAddLocaleDataProvider(): array { return [ //User locale, original target, redirect target ['de', '/', '/de/'], ['de', '/part/3', '/de/part/3'], ['en', '/', '/en/'], ['en', '/category/new', '/en/category/new'], ['en_US', '/part/3', '/en_US/part/3'], //Without an explicit set value, the user should be redirect to english version [null, '/', '/en/'], ['en_US', '/part/3', '/en_US/part/3'], ]; } /** * Test if the user is redirected to the localized version of a page, based on his settings. * * @dataProvider urlAddLocaleDataProvider * @group slow * @depends testUrlMatch * * @param $user_locale * @param $input_path * @param $redirect_path */ public function testAddLocale($user_locale, $input_path, $redirect_path): void { //Redirect path is absolute $redirect_path = 'http://localhost'.$redirect_path; /** @var User $user */ $user = $this->userRepo->findOneBy(['name' => 'user']); //Set user locale $user->setLanguage($user_locale); $this->em->flush(); $this->client->followRedirects(false); $this->client->request('GET', $input_path); $this->assertSame($redirect_path, $this->client->getResponse()->headers->get('Location')); } }