Skip to content

Commit

Permalink
En cours: tests fonctionnels complets
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrelemee committed Jan 6, 2025
1 parent df93642 commit cf3df44
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 98 deletions.
1 change: 0 additions & 1 deletion .github/workflows/deploy-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ jobs:
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
run: |
git rev-parse HEAD
clever link $CC_APP_ID
clever deploy --alias precontentieux-dev --force
1 change: 0 additions & 1 deletion .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ jobs:
CLEVER_SECRET: ${{ secrets.CLEVER_SECRET }}
CLEVER_TOKEN: ${{ secrets.CLEVER_TOKEN }}
run: |
echo $CC_APP_ID
clever link $CC_APP_ID
clever deploy --alias precontentieux-prod --force
22 changes: 11 additions & 11 deletions templates/bris_porte/tester_mon_eligibilite.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@
>
<fieldset
class="fr-fieldset"
aria-labelledby="checkboxes-inline-legend"
aria-labelledby="eligibilite-test-form-question-estVise"
>
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="checkboxes-inline-legend">
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="eligibilite-test-form-question-estVise">
Êtes-vous la personne visée par l'intervention des forces de l'ordre ?
</legend>
<div class="fr-fieldset__element fr-fieldset__element--inline" id="eligibilite-test-form-question-estVise">
<div class="fr-fieldset__element fr-fieldset__element--inline">
<div class="fr-radio-group">
<input
id="eligibilite-test-form-est-vise-oui"
Expand Down Expand Up @@ -170,9 +170,9 @@
>
<fieldset
class="fr-fieldset"
aria-labelledby="checkboxes-inline-legend"
aria-labelledby="eligibilite-test-form-question-estHebergeant"
>
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="checkboxes-inline-legend">
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="eligibilite-test-form-question-estHebergeant">
Est-ce que la personne recherchée réside ou est hébergée à votre adresse ?
</legend>
<div class="fr-fieldset__element fr-fieldset__element--inline">
Expand Down Expand Up @@ -220,9 +220,9 @@
>
<fieldset
class="fr-fieldset"
aria-labelledby="checkboxes-inline-legend"
aria-labelledby="eligibilite-test-form-question-estProprietaire"
>
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="checkboxes-inline-legend">
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="eligibilite-test-form-question-estProprietaire">
Quel est votre statut par rapport au logement ?
</legend>
<div class="fr-fieldset__element fr-fieldset__element--inline">
Expand Down Expand Up @@ -263,9 +263,9 @@
>
<fieldset
class="fr-fieldset"
aria-labelledby="checkboxes-inline-legend"
aria-labelledby="eligibilite-test-form-question-aContacteAssurance"
>
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="checkboxes-inline-legend">
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="eligibilite-test-form-question-aContacteAssurance">
Avez-vous pris contact avec votre assurance habitation et obtenu une attestation de non prise en charge du sinistre ?
</legend>
<div class="fr-fieldset__element fr-fieldset__element--inline">
Expand Down Expand Up @@ -306,9 +306,9 @@
>
<fieldset
class="fr-fieldset"
aria-labelledby="checkboxes-inline-legend"
aria-labelledby="eligibilite-test-form-question-aContacteBailleur"
>
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="checkboxes-inline-legend">
<legend class="fr-fieldset__legend--regular fr-fieldset__legend" id="eligibilite-test-form-question-aContacteBailleur">
Avez-vous pris contact avec votre bailleur et obtenu une attestation de non prise en charge des réparations ?
</legend>
<div class="fr-fieldset__element fr-fieldset__element--inline">
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
<p>
{% block appelAAction %}
<a class="fr-btn fr-p-3v" href="{{ url('bris_porte_tester_eligibilite') }}">
<span class="text-center full-width">Déposer votre demande d’indemnisation</span>
Déposer votre demande d’indemnisation
</a>
{% endblock appelAAction %}
</p>
Expand Down
100 changes: 99 additions & 1 deletion tests/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,113 @@

namespace App\Tests\Functional;

use Facebook\WebDriver\Exception\ElementClickInterceptedException;
use Facebook\WebDriver\WebDriverElement;
use Symfony\Component\Panther\Client as PantherClient;
use Symfony\Component\Panther\DomCrawler\Crawler as PantherCrawler;
use Symfony\Component\Panther\PantherTestCase;

abstract class AbstractFunctionalTestCase extends PantherTestCase
{
protected PantherClient $client;

public function devices(): array
{
return [
'mobile' => ['mobile', 428, 926],
'desktop' => ['desktop', 1200, 2000],
];
}
}

// Cas des input 'checkbox‘ ou 'radio':
protected function checkField(string $legend, string $label, bool $exactMatch = false): void
{
$field = $this->getFieldByLabel($label, $legend, $exactMatch);

if (null === $field->getElement(0)) {
throw new \LogicException("Aucun élément trouvé pour le champs de label '$legend' > '$label'");
}

if ('input' !== $field->getTagName()) {
throw new \LogicException("L'élément trouvé pour le champs de label '$legend' > '$label' a un type inattendu {$field->getTagName()} (attendu <input>)");
}

try {
$field->click();
} catch (ElementClickInterceptedException $e) {
// Dans le cas où le label englobe le champs, on se replie sur le clic sur le <label>
$this->client->getCrawler()->filter(sprintf('label[for="%s"]', $field->getAttribute('id')))->first()->click();
}
}

protected function setField(string $label, string $value, bool $exactMatch = false): void
{
$field = $this->getFieldByLabel($label, exactMatch: $exactMatch);

if (null === $field->getElement(0)) {
throw new \LogicException("Aucun élément trouvé pour le champs de label '$label'");
}

switch ($field->getTagName()) {
case 'textarea':
case 'input':
$field->getElement(0)?->clear();
$field->sendKeys($value);
break;
case 'select':
$options = $field->filter('option');
$option = $options->reduce(function (PantherCrawler $o) use ($value) {
return trim($o->getText()) === $value;
})->first();
$option->click();
break;
default:
throw new \LogicException("L'élément trouvé pour le champs de label '$label' a un type inattendu {$field->getTagName()} (attendu <input>, <select> ou <textarea>)");
}
}

protected function getFieldsByLabel(string $label, bool $exactMatch = false): PantherCrawler
{
// Chercher tous les champs (<input>, <select>, <textarea>)
$crawler = $this->client->getCrawler();

return $crawler
->filter('input,select,textarea')
->reduce(function (PantherCrawler $e) use ($label, $crawler, $exactMatch) {
return $crawler
->filter(sprintf('label[for="%s"]', $e->getAttribute('id')))
->reduce(function (WebDriverElement $l) use ($label, $exactMatch) {
return $exactMatch ? trim($l->getText()) === $label : str_contains(trim($l->getText()), $label);
})->count() > 0;
}
);
}

protected function getFieldByLabel(string $label, ?string $legend = null, bool $exactMatch = false): PantherCrawler
{
$fields = $this->getFieldsByLabel($label);

if (null === $legend) {
return $fields->first();
}

$ff = $fields->reduce(function (PantherCrawler $e) use ($legend, $exactMatch) {
// On remonte jusqu'au plus proche fieldset englobant
return 1 === $e->ancestors()
->filter('fieldset')
->first()
->filter('legend')
->reduce(
// On cherche l'élément <legend> dans le fieldset dont le texte est $legend
function (WebDriverElement $l) use ($legend, $exactMatch) {
var_dump(trim($l->getText()));

return $exactMatch ? trim($l->getText()) === $legend : str_contains(trim($l->getText()), $legend);
}
)
->count();
});

return $ff->first();
}
}
42 changes: 5 additions & 37 deletions tests/Functional/Requerant/DepotBrisPorteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
use App\Tests\Functional\AbstractFunctionalTestCase;
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
use Doctrine\ORM\EntityManagerInterface;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriverPoint;
use GuzzleHttp\Client as HttpClient;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Panther\Client as PantherClient;
use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class DepotBrisPorteTest extends AbstractFunctionalTestCase
{
protected PantherClient $client;
protected EntityManagerInterface $em;
protected UserPasswordHasherInterface $passwordHasher;
protected HttpClient $mailerClient;
Expand Down Expand Up @@ -56,7 +53,7 @@ protected function setUp(): void

$this->em = self::getContainer()->get(EntityManagerInterface::class);
$this->passwordHasher = self::getContainer()->get(UserPasswordHasherInterface::class);
$this->screenShotDir = self::getContainer()->getParameter('kernel.project_dir').'/public/screenshots';
$this->screenShotDir = self::getContainer()->getParameter('kernel.project_dir').'/public/screenshots/requerant/depot-dossier/';
$finder = new Finder();
$filesystem = new Filesystem();
if ($filesystem->exists($this->screenShotDir)) {
Expand Down Expand Up @@ -110,35 +107,6 @@ protected function setUp(): void
StaticDriver::beginTransaction();
}

protected function getFieldByLabel(string $label, bool $exactMatch = false): ?WebDriverElement
{
$label = $this->client->getCrawler()->filter('label')
->reduce(function (WebDriverElement $e) use ($label, $exactMatch) {
if ($exactMatch) {
return trim($e->getText()) === $label;
}

return str_contains($e->getText(), $label);
})
->first() ?? null;

if ($label && $label->getAttribute('for')) {
$target = $this->client->getCrawler()->findElement(WebDriverBy::id($label->getAttribute('for'))) ?? null;

if (null === $target) {
throw new \LogicException("No form field found with id {$label->getAttribute('for')}");
}

if (!in_array($target->getTagName(), ['input', 'select', 'textarea'])) {
throw new \LogicException('Target element is not a form field (<input>, <select> or <textarea>)');
}

return $target;
}

return null;
}

protected function getButton(string $label): ?WebDriverElement
{
return $this->client->getCrawler()->filter('button')
Expand Down Expand Up @@ -188,10 +156,9 @@ public function testDepotDossierBrisPorte(string $device, int $width, int $heigh
$this->assertEquals(Response::HTTP_OK, $this->client->getInternalResponse()->getStatusCode());
$this->assertSelectorTextContains('main h1', 'Déclarer un bris de porte');

$input = $this->getFieldByLabel('Les 10 premiers chiffres de votre numéro de sécurité sociale');
$this->setField('Les 10 premiers chiffres de votre numéro de sécurité sociale', '2790656123');
$this->setField('Pays de naissance', 'France');

$input->clear();
$input->sendKeys('2790656123');
// Astuce pour s'assurer que la requête xhr de `PATCH` ait bien été déclenchée :
// TODO voir pour observer les `queuedChanges` par exemple https://github.com/php-webdriver/php-webdriver/wiki/How-to-work-with-AJAX-(jQuery,-Prototype,-Dojo)
sleep(1);
Expand All @@ -202,12 +169,13 @@ public function testDepotDossierBrisPorte(string $device, int $width, int $heigh

$this->client->takeScreenshot("$this->screenShotDir/$device/006-page-donnees-bris-de-porte.png");

// Il faut purger le cache Doctrine, afin de s'assurer que l'ORM rejoue une requête et récupère l'objet à jour
// Il faut purger le cache Doctrine, afin de s'assurer que l'ORM rejoue une requête et récupère l'objet à jour.
$this->em->clear();
$requerant = $this->em
->getRepository(Requerant::class)
->findOneBy(['email' => 'raquel.randt@courriel.fr']);
$this->assertNotNull($requerant);
$this->assertEquals('2790656123', $requerant->getPersonnePhysique()?->getNumeroSecuriteSociale());
$this->assertEquals('France', $requerant->getPersonnePhysique()?->getPaysNaissance()->getNom());
}
}
Loading

0 comments on commit cf3df44

Please sign in to comment.