Streamlining Yii3 Application Testing with Codeception and Docker

Introduction:

As developers, ensuring the robustness of our applications is vital. Testing is a crucial aspect of the development process, and in Yii3, we have the advantage of employing various tools to facilitate testing, including Codeception, and Docker. In this article, we’ll explore how to set up and streamline our testing process using these powerful tools.

Setting the Stage:

Running the PHP built-in server for Codeception.

The former can be achieved using the following command:

php -S 127.0.0.1:8080 -t public

We’ll be using Codeception version 5.0 for acceptance, functional, cli, and unit tests. Additionally, we’ll use the following modules:

"codeception/c3": "^2.8",
"codeception/codeception": "^5.0",
"codeception/module-asserts": "^3.0",
"codeception/module-cli": "^2.0",
"codeception/module-db": "^3.1",
"codeception/module-phpbrowser": "^3.0",
"symfony/process": "^6.3",

Note: That the symfony/process package is essential for executing environments, which allows us to easily run the PHP built-in server.

Directory Structure:

Before delving into the testing process, let’s review our project’s directory structure, which includes the following directories under the tests folder:

tests
└── _envs
└── _output
└── Acceptance
└── Functional
└── Cli
└── Unit

Environment Configuration for PHP Built-in Server:

To simplify the execution of the PHP built-in server, we’ll create a configuration file named
php-builtin.yml in directory _envs and add the necessary code to start the server automatically during testing:

extensions:
  enabled:
    - Codeception\Extension\RunProcess:
        0: php -d variables_order=EGPCS -S 127.0.0.1:8080 -t public
        sleep: 1

With this configuration, Codeception will automatically run the PHP built-in server, making it seamless to write our initial test flows.

Build and Docker Testing workflows:

Let’s create two testing workflows. One for Build testing using the PHP built-in server and another for Docker testing. For this, we’ll use GitHub Actions to orchestrate our testing process.

build.yml:

    - name: Run tests with codeception with code coverage.
      run: vendor/bin/codecept run --env php-builtin --coverage-xml

docker.yml

    - name: Run Codeception Tests
      run: |
        docker exec -e YII_ENV=tests yii-tools-demo vendor/bin/codecept run --env php-builtin

Conclusion:

By following the steps outlined in this article, we’ve successfully streamlined our Yii3 application testing process using Codeception, and Docker. With these powerful tools at our disposal, we can ensure our application’s stability and reliability throughout the development lifecycle.

Happy testing!

4 Likes