Skip to main content

PHP

PHPUnit is a popular unit testing framework for PHP that is widely used in PHP application development. Its main function is to facilitate the writing and execution of automated unit tests to verify the behavior and quality of PHP code.

Features

Robust testing framework: PHPUnit provides a solid infrastructure for creating and running unit tests. It allows developers to effectively write tests to verify the operation of functions, methods and classes in their code.

Support for various functionalities: In addition to basic unit tests, PHPUnit supports a variety of test types, such as integration tests and functional tests. This makes it possible to evaluate different aspects of the software, from the internal logic of the functions to the interaction of complete components.

Test report generation: PHPUnit generates detailed test reports, making it easy to identify bugs and errors in the code. These reports help developers debug and correct problems more efficiently.

Code coverage tools: PHPUnit provides code coverage measurement tools that allow you to evaluate what percentage of the source code has been executed during testing. This helps to identify untested areas and improve code quality.

CI/CD support: PHPUnit can be integrated with a variety of CI/CD frameworks and tools, making it easy to incorporate automated testing into development workflows and ensuring that tests run consistently.

Extensibility: PHPUnit is highly extensible and allows developers to create their own extensions and customizations to meet their specific testing needs.

Integration with Gitlab

.gitlab-ci.yml
phpunit:
stage: test
image: harbor.opensecdevops.com/osdo/php-ci@sha256:c4d27b1286fb998148f7439533f83c2ca40f0358bbda6009a6a3f069e14086c0
dependencies: ['composer']
variables:
XDEBUG_MODE: coverage
script:
- ./vendor/bin/phpunit --coverage-text --colors=never --log-junit phpunit-report.xml --coverage-clover coverage-report.xml
artifacts:
paths:
- phpunit-report.xml
- coverage-report.xml
reports:
junit: phpunit-report.xml
expire_in: 30 min
  • image: A specific container image is used that contains the environment needed to run the PHPUnit tests. The image is retrieved from a container registry (in this case, Harbor) and identified by its sha256 hash. This ensures that a specific version of the image is used to ensure consistency in testing.

  • dependencies: Dependencies are established on other jobs in the pipeline, such as "composer". This ensures that these jobs are executed before this job can start, which may be necessary to install the dependencies required for testing.

  • variables: The environment variable "XDEBUG_MODE" is set to "coverage", indicating that Xdebug code coverage will be activated during testing. This is useful for measuring code coverage during unit testing.

  • script: In this section, you execute the commands necessary to run the PHPUnit tests. This includes running the PHPUnit binary located in the "vendor/bin" directory and generating code coverage reports and JUnit reports. The results are stored in specific files.

    • --coverage-text: This parameter instructs PHPUnit to generate a code coverage report in text format after running the tests. Provides a simple summary of code coverage in the console.

    • --colors=never: This parameter disables the coloring of the output on the console. It is useful when running tests in an environment that does not support color, such as an automation or continuous integration environment.

    • --log-junit file-xml: This parameter allows to specify an XML file in which the test results will be stored in JUnit format. The XML file is useful when you want to integrate the tests with CI/CD tools or other reporting tools.

    • --coverage-clover-xml-file: With this parameter, an XML file is specified where a code coverage report in Clover format will be saved. This report is useful for evaluating code coverage in detail.

  • artifacts: Defines the artifacts generated by this job that are to be retained after the end of the pipeline. In this case, two files are kept: "phpunit-report.xml" and "coverage-report.xml". In addition, a JUnit report is defined using the "phpunit-report.xml" file.