Fix some tests

This commit is contained in:
2023-12-14 21:18:00 +01:00
parent d5476df14c
commit 2dd1b7c943
11 changed files with 92 additions and 127 deletions

View File

@@ -13,6 +13,7 @@ namespace Chill\MainBundle\Tests\Controller;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
@@ -24,23 +25,14 @@ use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
*/
final class UserControllerTest extends WebTestCase
{
private \Symfony\Bundle\FrameworkBundle\KernelBrowser $client;
use PrepareClientTrait;
private array $toDelete = [];
protected function setUp(): void
{
$this->client = self::createClient([], [
'PHP_AUTH_USER' => 'admin',
'PHP_AUTH_PW' => 'password',
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR',
]);
}
protected function tearDown(): void
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$em = self::getContainer()->get(EntityManagerInterface::class);
foreach ($this->toDelete as [$class, $id]) {
$obj = $em->getRepository($class)->find($id);
@@ -75,23 +67,26 @@ final class UserControllerTest extends WebTestCase
public function testList()
{
$client = $this->getClientAuthenticatedAsAdmin();
// get the list
$crawler = $this->client->request('GET', '/fr/admin/main/user');
$this->assertEquals(
200,
$this->client->getResponse()->getStatusCode(),
'Unexpected HTTP status code for GET /admin/main/user'
);
$client->request('GET', '/fr/admin/main/user');
self::assertResponseIsSuccessful();
}
public function testNew()
{
$crawler = $this->client->request('GET', '/fr/admin/main/user/new');
$client = $this->getClientAuthenticated('admin');
$crawler = $client->request('GET', '/fr/admin/main/user/new');
self::assertResponseIsSuccessful();
$username = 'Test_user'.uniqid();
$password = 'Password1234!';
// Fill in the form and submit it
$form = $crawler->selectButton('Créer & fermer')->form([
'chill_mainbundle_user[username]' => $username,
'chill_mainbundle_user[plainPassword][first]' => $password,
@@ -100,12 +95,12 @@ final class UserControllerTest extends WebTestCase
'chill_mainbundle_user[label]' => $username,
]);
$this->client->submit($form);
$crawler = $this->client->followRedirect();
$client->submit($form);
$crawler = $client->followRedirect();
// Check data in the show view
$this->assertStringContainsString(
'Test_user',
$username,
$crawler->text(),
'page contains the name of the user'
);
@@ -119,15 +114,18 @@ final class UserControllerTest extends WebTestCase
*/
public function testUpdate(int $userId, string $username)
{
$crawler = $this->client->request('GET', "/fr/admin/main/user/{$userId}/edit");
$client = $this->getClientAuthenticatedAsAdmin();
$crawler = $client->request('GET', "/fr/admin/main/user/{$userId}/edit");
self::assertResponseIsSuccessful();
$username = 'Foo bar '.uniqid();
$form = $crawler->selectButton('Enregistrer & fermer')->form([
'chill_mainbundle_user[username]' => $username,
]);
$this->client->submit($form);
$crawler = $this->client->followRedirect();
$client->submit($form);
$client->followRedirect();
// Check the element contains an attribute with value equals "Foo"
$this->assertResponseIsSuccessful();
}
@@ -137,7 +135,8 @@ final class UserControllerTest extends WebTestCase
*/
public function testUpdatePassword(int $userId, mixed $username)
{
$crawler = $this->client->request('GET', "/fr/admin/user/{$userId}/edit_password");
$client = $this->getClientAuthenticatedAsAdmin();
$crawler = $client->request('GET', "/fr/admin/user/{$userId}/edit_password");
$newPassword = '1234Password!';
$form = $crawler->selectButton('Changer le mot de passe')->form([
@@ -145,10 +144,10 @@ final class UserControllerTest extends WebTestCase
'chill_mainbundle_user_password[new_password][second]' => $newPassword,
]);
$this->client->submit($form);
$client->submit($form);
$this->assertTrue(
$this->client->getResponse()->isRedirect(),
$client->getResponse()->isRedirect(),
'the response is a redirection'
);