ホーム > Laravel > How to Automatically Test Laravel with CircleCI
Laravel

How to Automatically Test Laravel with CircleCI

Thank you for your continued support.
This article contains advertisements that help fund our operations.

⇨ Click here for the table of contents for the Laravel article

I wrote an article about testing Laravel with CircleCI.

This article does not cover "automated deployment".

It is intended for those who are having trouble with "testing".

How to Test a Laravel Project with CircleCI

The method we will implement this time is to create a test sqlite (database) on CircleCI and execute the tests there.

Linking GitHub Repository on CircleCI Management Screen

Please log in and select the repository.

It is recommended to log in with GitHub as it will be linked to GitHub.

⇨CircleCI Login Page

Creating .env.testing

Copy the .env file that you use locally and create .env.example.

Even though I do not set API keys that should not be shown locally,

  • Please remove any important information.

  • Comment out the database settings as follows (to be set in the subsequent sections):

# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_PORT=8889
# DB_DATABASE=
# DB_USERNAME=root
# DB_PASSWORD=root

Add to config/database.php

Add items for testing.

'sqlite_testing' => [
    'driver' => 'sqlite',
    'url' => env('DATABASE_URL'),
    'database' => env('DB_DATABASE', storage_path('testing.sqlite')),
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

It can be confusing at:

'database' => env('DB_DATABASE', storage_path('testing.sqlite')),

This sets up the storage/testing.sqlite file as the SQLite DB.

This means that the file needs to be generated in storage/testing.sqlite.

Edit phpunit.xml

<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite_testing"/>
<server name="DB_DATABASE" value="storage/testing.sqlite"/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>

The important part is:

<server name="DB_CONNECTION" value="sqlite_testing"/>

This sets up the configuration created earlier to be used in phpunit.

Edit .circleci/config.yml

# PHP CircleCI 2.0 configuration file
# See: https://circleci.com/docs/2.0/language-php/
version: 2
  1. php7.4
  2. Cache vendor
  3. Create a test DB and run migrations with PHPUnit

This is a minimal configuration.

Summary

That's it.

This is the description of testing, so try adding the description of automatic deployment afterwards.

I hope this is helpful for someone.

For feedback or complaints, please contact me via TwitterDM.

That's all!

Popular Articles

Deploying a PHP7.4 + Laravel6 Project to AWS EC2

Implementing Breadcrumbs in Laravel with laravel-breadcrumbs

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!