octobercms / october

Self-hosted CMS platform based on the Laravel PHP Framework.
https://octobercms.com/
Other
11.01k stars 2.21k forks source link

UnitTests with GitlabCI how to? #1764

Closed lgt closed 8 years ago

lgt commented 8 years ago

I try to run UnitTests using GitlabCI with runners, I have configured a runner using a docker image and services but not all the tests are passing. Actually all database tests are failing in my case

for Example:


25) SimpleTreeModelTest::testGetAllChildren
ErrorException: Trying to get property of non-object

/tests/unit/plugins/database/SimpleTreeModelTest.php:10

...

here I have my .gitlab-ci.yml

image: alexeymasolov/docker-octobercms-nginx-ssh

services:
  - mysql:latest

# Before Script
before_script:
  - composer self-update
  - composer install --prefer-source > /dev/null
  - cd plugins/vendor/my_plugin1 && composer install --prefer-source > /dev/null && cd pwd
  - cd plugins/vendor/my_plugin2 && composer install --prefer-source > /dev/null && cd pwd
  - cp .env.testing .env

stages:
  - build
  - test
  - deploy

# Variables
variables:
  MYSQL_ROOT_PASSWORD: root
  MYSQL_USER: user
  MYSQL_PASSWORD: homestead
  MYSQL_DATABASE: october_test

test 1.1:
  image: alexeymasolov/docker-octobercms-nginx-ssh
  services:
        - mysql:latest
  script:
    - echo "SELECT 'OK';" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"
    - dir
    - php -v
    - echo "Install OctoberCMS"
    - mkdir storage/cms/cache
    - mkdir storage/cms/combiner
    - mkdir storage/cms/assets
    - chmod -R 777 storage/
    - php artisan october:up
    - echo "Running PHPUnit Tests"
    - php vendor/bin/phpunit --colors --debug  --coverage-text
    - echo "Running PHPUnit Tests on Plugin"
    - cd plugins/vendor/my_plugin1
    - php vendor/bin/phpunit --colors --debug  --coverage-text

Under config/testingadded database.php which on run is getting populated but when comes about testing fails

huglester commented 8 years ago

I think in the before script, you should create the database for testing, like:

before_script:
    - cp .env.testing .env
    - source .env
    - composer install
    - export MAIL_HOST=$MAILHOG__MAILHOG_PORT_1025_TCP_ADDR
    - mysql -hmysql -uroot -e 'drop database if exists `october_test `;'
    - mysql -hmysql -uroot -e 'create database `october_test `;'

of course you would need to migrate the databases.. probaby phpunit would do that if DatabaseMigration trait is used, or add:

- php artisan migrate --force

hope that helps!

P.S. default setup of mysql uses username root without password, iif I remember correct!

lgt commented 8 years ago

thanks a lot! I'm about to try but I get an error in .env file

source .env
.env: line 7: DB_DRIVER: command not found

can you share please your .env file?

huglester commented 8 years ago

as you can see these commands:

 - cp .env.testing .env
 - source .env

I have separate config file for CI, so after it is being deployed to docker, it copies over .env.testing as .env file, so laravel could read databse credentials, etc.

But seeing at the error I think you have errors in you .env file, so you paste :)

lgt commented 8 years ago
APP_ENV=testing
APP_DEBUG=true
APP_KEY=SomeRandomString

DB_HOST=mysql
DB_DRIVER = mysql
DB_DATABASE=october_tests
DB_USERNAME=user
DB_PASSWORD=homestead

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
huglester commented 8 years ago

DB_DRIVER = mysql remove the spaces

lgt commented 8 years ago

same errors after changes

Starting test 'BelongsToModelTest::testSetRelationValue'.
E
Starting test 'BelongsToModelTest::testGetRelationValue'.
E
Starting test 'BelongsToModelTest::testDeferredBinding'.
E
Starting test 'DeferredBindingTest::testNegatedBinding'.
E
huglester commented 8 years ago

gitlabci, has default user to root and no pass by default in mysql instance.

try this:

DB_HOST=mysql DB_DATABASE=october_test DB_USERNAME=root DB_PASSWORD=

make sure in before script database ARE THE SAME

lgt commented 8 years ago

as I mentioned all data is getting populated here is the dump

$ php artisan october:up
Migrating application and plugins...
Migration table created successfully.
System
 - Migrated: 2013_10_01_000001_Db_Deferred_Bindings
 - Migrated: 2013_10_01_000002_Db_System_Files
 - Migrated: 2013_10_01_000003_Db_System_Plugin_Versions
 - Migrated: 2013_10_01_000004_Db_System_Plugin_History
 - Migrated: 2013_10_01_000005_Db_System_Settings
 - Migrated: 2013_10_01_000006_Db_System_Parameters
 - Migrated: 2013_10_01_000007_Db_System_Add_Disabled_Flag
 - Migrated: 2013_10_01_000008_Db_System_Mail_Templates
 - Migrated: 2013_10_01_000009_Db_System_Mail_Layouts
 - Migrated: 2014_10_01_000010_Db_Jobs
 - Migrated: 2014_10_01_000011_Db_System_Event_Logs
 - Migrated: 2014_10_01_000012_Db_System_Request_Logs
 - Migrated: 2014_10_01_000013_Db_System_Sessions
 - Migrated: 2015_10_01_000014_Db_System_Mail_Layout_Rename
 - Migrated: 2015_10_01_000015_Db_System_Add_Frozen_Flag
 - Migrated: 2015_10_01_000016_Db_Cache
 - Migrated: 2015_10_01_000017_Db_System_Revisions
 - Migrated: 2015_10_01_000018_Db_FailedJobs
Backend
 - Migrated: 2013_10_01_000001_Db_Backend_Users
 - Migrated: 2013_10_01_000002_Db_Backend_User_Groups
 - Migrated: 2013_10_01_000003_Db_Backend_Users_Groups
 - Migrated: 2013_10_01_000004_Db_Backend_User_Throttle
 - Migrated: 2014_01_04_000005_Db_Backend_User_Preferences
 - Migrated: 2014_10_01_000006_Db_Backend_Access_Log
 - Migrated: 2014_10_01_000007_Db_Backend_Add_Description_Field
 - Migrated: 2015_10_01_000008_Db_Backend_Add_Superuser_Flag
Cms
 - Migrated: 2014_10_01_000001_Db_Cms_Theme_Data
Database.Tester
 - v1.0.1:  First version of Tester
 - v1.0.2:  Create tables
October.Tester
 - v1.0.1:  Added some upgrade file and some seeding
 - v1.0.2:  Create blog post comments table
 - v1.0.3:  Bug fix update that uses no scripts
 - v1.0.4:  Another fix
 - v1.0.5:  Create blog settings table
October.NoUpdates
 - Nothing to update.
October.Sample
 - Nothing to update.
Seeded System 
Seeded Backend 

but database tests are not passing

FAILURES!
Tests: 216, Assertions: 1489, Errors: 33.

Code Coverage Report:          
  2016-02-10 09:25:41          

 Summary:                      
  Classes: 18.58% (110/592)    
  Methods: 30.71% (1735/5649)  
  Lines:   61.86% (30661/49563)