Wednesday, January 5, 2022

[FIXED] Gitlab CI with CakePHP 3

Issue

I'm using CakePHP and want to benefit from CI using Gitlab. I have the config for .gitlab-ci.yml with this in config/app.php:

/**
     * The test connection is used during the test suite.
     */
    'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => env('MYSQL_HOST', 'localhost'),
        //'port' => 'non_standard_port_number',
        'username' => env('MYSQL_USER', 'root'),
        'password' => env('MYSQL_ROOT_PASSWORD', 'mysql_strong_password'),
        'database' => env('MYSQL_DATABASE', 'test'),
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        //'url' => env('DATABASE_TEST_URL', null),
    ],

But unfortunately, my tests are failing permanently.

.Exception: Unable to insert fixtures for "App\Test\TestCase\Controller\CategoriesControllerTest" test case. SQLSTATE[HY000] [2002] No such file or directory in [/builds/koffisani/gabways-web/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureManager.php, line 358] ERROR: Job failed: exit code 1

I need a little help to progress.

Update :

Gitlab CI config :

image: kaffineaddict/gitlabcakephp3

# Cache the vendor folder
cache:
  paths:
  - vendor/


before_script:
# Install git to clone your repository
- apt-get update -yqq
- apt-get install git libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq
- docker-php-ext-install mbstring mcrypt pdo_mysql curl json intl gd xml zip bz2 opcache

# Install composer
- curl -sS https://getcomposer.org/installer | php

# Install all project dependencies
- composer install

services:
- mysql

variables:
  # Configure mysql service (https://hub.docker.com/_/mysql/)
  # We will need to use these in the app.php test datasource
  MYSQL_DATABASE: test_myapp
  MYSQL_ROOT_PASSWORD: secret

# Run the phpunit tests
All:
 script:
  - cp config/app.default.php app.php
  - vendor/bin/phpunit

Solution

I've finally figured it out.

My .gitlab-ci.yml file contains this block :

variables:
# Configure mysql service (https://hub.docker.com/_/mysql/)
# We will need to use these in the app.php test datasource
   MYSQL_DATABASE: test
   MYSQL_ROOT_PASSWORD: mysql_strong_password
   MYSQL_HOST: mysql

And the app.php data structure section is as follows :

'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'mysql',
        //'port' => 'non_standard_port_number',
        'username' => 'root',
        'password' => 'mysql_strong_password',
        'database' => 'test',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
        'log' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        'url' => env('DATABASE_TEST_URL', null),
    ],

And things went well. It may help someone.



Answered By - sk001

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.