GitHub Actions can run PHP lint, Composer validation and PHPUnit on every change.
What It Does
GitHub Actions can run PHP lint, Composer validation and PHPUnit on every change. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
A local Composer project with a compatible PHPUnit version and isolated fixtures or stubs.
The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.
Example 1: Basic Behavior
Set up PHP, install locked dependencies, and run PHPUnit on pushes and pull requests.
name: php-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
coverage: none
- run: composer install --no-interaction --prefer-dist
- run: vendor/bin/phpunit --colors=never
This is a YAML configuration example; use it in its appropriate file or shell, not in the PHP source editor.
Example 2: Another Case
Fail CI early when package metadata or vulnerable locked dependencies need action.
composer validate --strict
composer audit --locked
This is a command-line example; use it in its appropriate file or shell, not in the PHP source editor.
Practical Example: Putting It to Use
Small isolated assertions can supplement PHPUnit suites, but use a real test runner in CI.
<?php
function subtotal(int $count, int $cents): int { return $count * $cents; }
if (subtotal(3, 250) !== 750) throw new RuntimeException("Test failure");
echo "Passed";
Important Behavior and Edge Cases
A CI job must use tested versions and secrets with minimal privileges; passing style does not imply passing tests.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| Automating Tests in GitHub Actions | GitHub Actions can run PHP lint, Composer validation and PHPUnit on every change. |
| Related approach | Use a workflow YAML file under .github/workflows/. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for Automating Tests in GitHub Actions | A CI job must use tested versions and secrets with minimal privileges; passing style does not imply passing tests. |
| Assuming a related mechanism has the same behavior | Use a workflow YAML file under .github/workflows/. |
Key Takeaways
- GitHub Actions can run PHP lint, Composer validation and PHPUnit on every change.
- A CI job must use tested versions and secrets with minimal privileges; passing style does not imply passing tests.
- Use a workflow YAML file under .github/workflows/.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.