From 565f99d2d7a10df12d73e38af68806f01b795d3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Girardot?= <cgi@atolcd.com>
Date: Tue, 28 Jul 2020 16:09:41 +0200
Subject: [PATCH] Build : Ajoute tests Cypress dans Jenkins

Change-Id: I705baef3e04ac9107756c2b34fcbbe2f62f0a3e8
---
 Jenkinsfile.review                            | 24 +++++++
 .../Controller/DefaultControllerTest.php      | 12 +++-
 .../TestCase/FixtureAwareWebTestCase.php      | 71 +++++++++++++++++++
 docker/lamp/Dockerfile                        | 28 ++++++--
 docker/lamp/files/create_mysql_users.sh       |  8 +++
 docker/lamp/files/run.sh                      |  6 ++
 6 files changed, 143 insertions(+), 6 deletions(-)
 create mode 100644 appli_sf/tests/FunctionalTests/TestCase/FixtureAwareWebTestCase.php

diff --git a/Jenkinsfile.review b/Jenkinsfile.review
index 1418416a..39e8d384 100644
--- a/Jenkinsfile.review
+++ b/Jenkinsfile.review
@@ -68,6 +68,9 @@ pipeline {
         stage("Unit tests") {
           steps {
             dir('appli_sf') {
+              sh 'bin/console doctrine:schema:drop --force'
+              sh 'bin/console doctrine:schema:update --force'
+              sh 'bin/console cache:clear'
               sh 'bin/phpunit tests/UnitTests'
             }
           }
@@ -75,10 +78,31 @@ pipeline {
         stage("Functional tests") {
           steps {
             dir('appli_sf') {
+              sh 'bin/console doctrine:schema:drop --force'
+              sh 'bin/console doctrine:schema:update --force'
+              sh 'bin/console cache:clear'
               sh 'bin/phpunit tests/FunctionalTests'
             }
           }
         }
+        stage("E2E tests") {
+          environment {
+            CYPRESS_BASE_URL = "http://localhost"
+          }
+          steps {
+            dir('appli_sf') {
+              sh 'bin/console doctrine:schema:drop --force'
+              sh 'bin/console doctrine:schema:update --force'
+              sh 'bin/console cache:clear'
+              sh 'bin/console import:individus imports/individus.csv --use-civil-name=1 --no-interaction --quiet'
+              sh 'bin/console user:add --username=admin --password=P@ssword12345 --email=test@deptnot.test --name=Administrateur --role=ROLE_ADMIN --no-interaction'
+              dir('tests/E2E') {
+                sh 'yarn'
+                sh ' cypress run'
+              }
+            }
+          }
+        }
       }
     }
   }
diff --git a/appli_sf/tests/FunctionalTests/Controller/DefaultControllerTest.php b/appli_sf/tests/FunctionalTests/Controller/DefaultControllerTest.php
index fd23e0a1..0ac1fe57 100644
--- a/appli_sf/tests/FunctionalTests/Controller/DefaultControllerTest.php
+++ b/appli_sf/tests/FunctionalTests/Controller/DefaultControllerTest.php
@@ -2,15 +2,19 @@
 
 namespace App\Tests\FunctionalTests\Controller;
 
+use App\DataFixtures\PersonCivilNameFixtures;
+use App\DataFixtures\UserFixtures;
 use App\Entity\User;
 use App\Helper\UserHelper;
 use App\Repository\UserRepository;
+use App\Tests\FunctionalTests\TestCase\FixtureAwareWebTestCase;
 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
 use Symfony\Component\BrowserKit\Cookie;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
+use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
 
-class DefaultControllerTest extends WebTestCase
+class DefaultControllerTest extends FixtureAwareWebTestCase
 {
     /**
      * @var \Doctrine\ORM\EntityManager
@@ -33,6 +37,12 @@ class DefaultControllerTest extends WebTestCase
         $this->entityManager = $kernel->getContainer()
             ->get('doctrine')
             ->getManager();
+        $encoder = self::$container->get(UserPasswordEncoderInterface::class);
+
+        // Chargement des fixtures
+        $this->addFixture(new UserFixtures($encoder));
+        $this->addFixture(new PersonCivilNameFixtures());
+        $this->executeFixtures();
     }
 
     /**
diff --git a/appli_sf/tests/FunctionalTests/TestCase/FixtureAwareWebTestCase.php b/appli_sf/tests/FunctionalTests/TestCase/FixtureAwareWebTestCase.php
new file mode 100644
index 00000000..27505826
--- /dev/null
+++ b/appli_sf/tests/FunctionalTests/TestCase/FixtureAwareWebTestCase.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace App\Tests\FunctionalTests\TestCase;
+
+use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
+use Doctrine\Common\DataFixtures\FixtureInterface;
+use Doctrine\Common\DataFixtures\Purger\ORMPurger;
+use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+
+abstract class FixtureAwareWebTestCase extends WebTestCase
+{
+    /**
+     * @var ORMExecutor
+     */
+    private $fixtureExecutor;
+
+    /**
+     * @var ContainerAwareLoader
+     */
+    private $fixtureLoader;
+
+    public function setUp()
+    {
+        self::bootKernel();
+    }
+
+    /**
+     * Adds a new fixture to be loaded.
+     *
+     * @param FixtureInterface $fixture
+     */
+    protected function addFixture(FixtureInterface $fixture)
+    {
+        $this->getFixtureLoader()->addFixture($fixture);
+    }
+
+    /**
+     * Executes all the fixtures that have been loaded so far.
+     */
+    protected function executeFixtures()
+    {
+        $this->getFixtureExecutor()->execute($this->getFixtureLoader()->getFixtures());
+    }
+
+    /**
+     * @return ORMExecutor
+     */
+    private function getFixtureExecutor()
+    {
+        if (!$this->fixtureExecutor) {
+            /** @var \Doctrine\ORM\EntityManager $entityManager */
+            $entityManager = self::$kernel->getContainer()->get('doctrine')->getManager();
+            $this->fixtureExecutor = new ORMExecutor($entityManager, new ORMPurger($entityManager));
+        }
+
+        return $this->fixtureExecutor;
+    }
+
+    /**
+     * @return ContainerAwareLoader
+     */
+    private function getFixtureLoader()
+    {
+        if (!$this->fixtureLoader) {
+            $this->fixtureLoader = new ContainerAwareLoader(self::$kernel->getContainer());
+        }
+
+        return $this->fixtureLoader;
+    }
+}
diff --git a/docker/lamp/Dockerfile b/docker/lamp/Dockerfile
index bda84452..7dd46a7d 100644
--- a/docker/lamp/Dockerfile
+++ b/docker/lamp/Dockerfile
@@ -16,7 +16,7 @@ RUN groupmod -g ${BOOT2DOCKER_GID} staff
 ENV DEBIAN_FRONTEND noninteractive
 RUN set -eux; \
     apt-get update; \
-    apt-get install -y \
+    apt-get install -y --no-install-recommends \
         supervisor \
         wget \
         apache2 \
@@ -31,14 +31,29 @@ RUN set -eux; \
         php-zip \
         libapache2-mod-php \
         curl \
-        gnupg2 \
+        gnupg \
+        ca-certificates \
+        libgtk2.0-0 \
+        libgtk-3-0 \
+        libnotify-dev \
+        libgconf-2-4 \
+        libnss3 \
+        libxss1 \
+        libasound2 \
+        libxtst6 \
+        xauth \
+        xvfb \
         jq; \
         echo "ServerName localhost" >> /etc/apache2/apache2.conf;
 
 # Install yarn
-RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
-    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
-    apt-get update && apt-get install -y yarn
+ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn
+RUN set -eux;\
+    apt-get update && apt-get install -y gnupg; \
+    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -; \
+    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list; \
+    apt-get update; \
+    apt-get install -y --no-install-recommends nodejs yarn;
 
 # Add image configuration and scripts
 ADD files/start-apache2.sh /start-apache2.sh
@@ -71,6 +86,9 @@ ENV PHP_UPLOAD_MAX_FILESIZE 10M
 ENV PHP_POST_MAX_SIZE 10M
 ENV PHP_VERSION 7.2
 
+# Install Cypress
+RUN yarn global add --pure-lockfile cypress@4.11.0;
+
 WORKDIR /app
 
 CMD ["/run.sh"]
diff --git a/docker/lamp/files/create_mysql_users.sh b/docker/lamp/files/create_mysql_users.sh
index b2022c7e..f6a08c18 100755
--- a/docker/lamp/files/create_mysql_users.sh
+++ b/docker/lamp/files/create_mysql_users.sh
@@ -3,11 +3,19 @@
 /usr/bin/mysqld_safe > /dev/null 2>&1 &
 
 RET=1
+COUNT_ATTEMPT=0
+NB_MAX_ATTEMPT=15
 while [[ RET -ne 0 ]]; do
     echo "=> Waiting for confirmation of MySQL service startup"
     sleep 5
     mysql -uroot -e "status" > /dev/null 2>&1
     RET=$?
+    (( COUNT_ATTEMPT=COUNT_ATTEMPT+1 ))
+    echo "=> Tentative $COUNT_ATTEMPT sur $NB_MAX_ATTEMPT"
+
+    if [[ COUNT_ATTEMPT -ge NB_MAX_ATTEMPT ]]; then
+        exit 5;
+    fi
 done
 
 mysql -uroot -e "CREATE USER 'notaires'@'localhost' IDENTIFIED BY 'password'"
diff --git a/docker/lamp/files/run.sh b/docker/lamp/files/run.sh
index e6681ad5..f884ceea 100755
--- a/docker/lamp/files/run.sh
+++ b/docker/lamp/files/run.sh
@@ -82,6 +82,12 @@ if [[ ! -d $VOLUME_HOME/mysql ]]; then
 
     echo "=> Done!"
     /create_mysql_users.sh
+    RET=$?
+
+    if [[ RET -ne 0 ]]; then
+        echo "=> Échec du démarrage de la BDD"
+        exit $?
+    fi
 else
     echo "=> Using an existing volume of MySQL"
 fi
-- 
GitLab