Setup Gitlab-ci with Symfony 4 project

Hi all!

I’m currently trying to setup a great continuous integration, but it’s my first time doing it.

I’m not really sure how to do it, so I’ll try to explain how I did it.

I have a file “.env.test” with this :

APP_ENV=test
APP_SECRET=asecretkey
MAILER_URL=null://localhost
DATABASE_URL=mysql://runner:password@127.0.0.1:3306/database

And a “.gitlab-ci.yml” file with this content :

image: php:7.2-cli
services:
    - mysql:latest
cache:
    paths:
    - vendor/

stages:
    - connect
    - test
    - deploy

variables:
    # Configure mysql service (https://hub.docker.com/_/mysql/)
    MYSQL_DATABASE: database
    MYSQL_USER: runner
    MYSQL_ROOT_PASSWORD: password
    MYSQL_PASSWORD: password

before_script:
    - apt-get update -yqq
    - apt-get install git -yqq zlib1g-dev
    # Install mysql driver
    - docker-php-ext-install pdo_mysql zip
    # Install composer
    - curl -sS https://getcomposer.org/installer | php
    # Install all project dependencies
    - php composer.phar install

test:
    stage: test
    script:
        - cp .env.test .env
        - php bin/console doctrine:database:create -e test
        - php bin/console make:migration -e test
        - php bin/console doctrine:fixture:load -no-interaction -e test
        - php composer.phar test

deploy:
    stage: deploy
    script:
        - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
        - eval $(ssh-agent -s)
        - ssh-add <(echo "$SSH_PRIVATE_KEY")
        - mkdir -p ~/.ssh
        - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

At the moment, in the “test” job, I have this error :

Error thrown while running command "doctrine:database:create -e test". 
Message: "An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused"
In AbstractMySQLDriver.php line 113:                                                                         
An exception occurred in driver: 
SQLSTATE[HY000] [2002] Connection refused                                                                          
In PDOConnection.php line 50:                                       
SQLSTATE[HY000] [2002] Connection refused                                        
In PDOConnection.php line 46:                                      
SQLSTATE[HY000] [2002] Connection refused   

I’m forced to create my database because I have functionnal test (that check if I have 200 HTTP answer). If there’s no database, it won’t be possible to have 200 answers, or maybe I’m wrong?

I’m kind of lost about this, is this possible for you to help me?
Have a good day!

For information, I’m forward now.
What did I changed?

APP_ENV=test
APP_SECRET=asecretkey
MAILER_URL=null://localhost
DATABASE_URL=mysql://runner:password@mysql/database

I used “mysql” instead of 127.0.0.1:3306

I also installed unzip and xdebug and changed the command for migration & fixture

test:
    stage: test
    script:
        - cp .env.test .env
        # Install & enable Xdebug for code coverage reports
        - pecl install xdebug
        - docker-php-ext-enable xdebug
        - php bin/console doctrine:migrations:migrate -e test
        - php bin/console doctrine:fixture:load --no-interaction -e test
        - php composer.phar test-gitlab

Now, when I launch the tests, I have a problem with the page where I call the database.
Here are the tests :

    /**
     * @dataProvider urlLoggoutProvider
     * @param $url
     */
    public function testPageIsUserNotLoggedSuccessful($url)
    {
        $client = self::createClient();
        $client->request('GET', $url);

        $this->assertTrue($client->getResponse()->isSuccessful());
    }

    public function urlLoggoutProvider()
    {
        yield ["/"];
        yield ["/news"];
        yield ["/unit"];
        yield ["/staff"];
        yield ["/calendar"];
        yield ["/downloadable_files"];
        yield ["/login"];
    }

And here’s the exception I have :

2018-08-06T08:12:48+00:00 [critical] Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: “An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused” at /builds/Calex92/Scouts/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 113

If someone can say what I still need to change?
Have a good day

Ok, for information, I had to create phpunit.xml.test (in a test env, I rename it in phpunit.xml) where I set the DATABASE_URL value into the same in the .env.test