Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

WIP - Translations in tests #53

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ rector: ## Run rector

tests: export APP_ENV=test
tests: ## Run the tests
$(EXEC) $(CONTAINER_NAME) $(TESTS)
@$(EXEC) --env SYMFONY_DEPRECATIONS_HELPER=disabled $(CONTAINER_NAME) $(TESTS)

tests-reset: export APP_ENV=test
tests-reset: ## Recreate database, launch migrations, load fixtures and execute tests
Expand Down
12 changes: 11 additions & 1 deletion tests/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ protected function login($email)

// get or create the user somehow (e.g. creating some users only
// for tests while loading the test fixtures)
$userRepository = static::getContainer()->get(UserRepository::class);
$userRepository = $this->getIdentifier(UserRepository::class);
$testUser = $userRepository->findOneByEmail($email);

$client->loginUser($testUser);

return $client;
}

protected function getIdentifier(string $containerIdentifier)
{
return static::getContainer()->get($containerIdentifier);
}

protected function trans(string $identifier, array $parameters = [], string $domain = 'messages')
{
return $this->getIdentifier('translator')->trans($identifier, $parameters, $domain);
}
}
23 changes: 20 additions & 3 deletions tests/Controller/TaskControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Tests\Controller;

use App\Entity\Task;
use App\Repository\TaskRepository;
use Symfony\Component\HttpFoundation\Response;

class TaskControllerTest extends BaseController
Expand Down Expand Up @@ -43,20 +45,32 @@ public function testCreateTaskPageWithFakeData(): void
$client = $this->login(BaseController::USER_EMAIL);
$client->request('GET', '/tasks/create');

$client->submitForm('Ajouter', [
self::assertSame('Créer un utilisateur', $this->trans('app.user.create'));
self::assertSame('Créer une catégorie', $this->trans('app.category.create'));
self::assertSame('Se déconnecter', $this->trans('app.logout'));
self::assertSame('Titre', $this->trans('app.title'));
self::assertSame('Contenu', $this->trans('app.content'));
self::assertSame('Catégorie', $this->trans('app.category'));
self::assertSame('Ajouter', $this->trans('app.add'));
self::assertSame('Retour à la liste des tâches', $this->trans('app.back.to.task.list'));

$client->submitForm($this->trans('app.add'), [
'task[title]' => '',
'task[content]' => '',
]);

self::assertResponseIsSuccessful();
self::assertSame('Vous devez saisir un titre.', $this->trans('app.task.title.not.blank', [], 'validators'));
self::assertSame('Vous devez saisir du contenu.', $this->trans('app.task.content.not.blank', [], 'validators'));
self::assertSame('Vous devez sélectionner une catégorie.', $this->trans('app.task.category.not.blank', [], 'validators'));
}

public function testCreateTaskPageWithGoodData(): void
{
$client = $this->login(BaseController::USER_EMAIL);
$client->request('GET', '/tasks/create');

$client->submitForm('Ajouter', [
$client->submitForm($this->trans('app.add'), [
'task[title]' => 'a new task',
'task[content]' => 'a new content',
'task[category]' => 1,
Expand All @@ -70,7 +84,7 @@ public function testEditTaskPageWithGoodData(): void
$client = $this->login(BaseController::USER_EMAIL);
$client->request('GET', '/tasks/1/edit');

$client->submitForm('Modifier', [
$client->submitForm($this->trans('app.edit'), [
'task[title]' => 'task updated',
'task[content]' => 'content updated',
]);
Expand All @@ -84,6 +98,9 @@ public function testToggleTask(): void
$client->request('GET', '/tasks/1/toggle');

self::assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
/** @var Task $task */
$task = $this->getIdentifier(TaskRepository::class)->find(1);
self::assertSame('La tâche '.$task->getTitle().' a bien été marquée comme à réaliser.', $this->trans('app.task.toggle.not.done', ['%taskTitle%' => $task->getTitle()]));
}

public function testDeleteTask(): void
Expand Down
6 changes: 6 additions & 0 deletions translations/messages+intl-icu.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="fr" datatype="plaintext" original="file.ext">
<body>
<!-- a start -->
<trans-unit id="app.add">
<source>app.add</source>
<target>Ajouter</target>
Expand All @@ -14,10 +15,15 @@
<source>app.admin</source>
<target>Administrateur</target>
</trans-unit>
<!-- a end -->

<!-- b start -->
<trans-unit id="app.back.to.task.list">
<source>app.back.to.task.list</source>
<target>Retour à la liste des tâches</target>
</trans-unit>
<!-- b end -->

<trans-unit id="app.cancel">
<source>app.cancel</source>
<target>Annuler</target>
Expand Down