Skip to content

Commit 53fcc7c

Browse files
committed
Add automated testing for PHPQA Docker images and update dependencies
- Introduced test scripts for validating published and local PHPQA images. - Updated Symfony Panther dependency to version ^2.3 for compatibility. - Fixed permissions for the /tools directory in Dockerfile to resolve cache extraction issues. - Enhanced README with testing instructions and known issues.
1 parent c663535 commit 53fcc7c

File tree

9 files changed

+476
-2
lines changed

9 files changed

+476
-2
lines changed

.castor/phpqa.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use function Castor\io;
2222
use function Castor\run;
2323

24-
guard_min_version('v0.23.0');
24+
guard_min_version('v1.0.0');
2525

2626
/**
2727
* Get project configuration from .phpqa-config.php or use defaults

.github/workflows/docker.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,19 @@ jobs:
167167
$IMAGE:$VERSION-amd64 \
168168
$IMAGE:$VERSION-arm64
169169
fi
170+
171+
test:
172+
needs: [manifest, prepare]
173+
runs-on: ubuntu-latest
174+
strategy:
175+
fail-fast: false
176+
matrix:
177+
php: [8.2, 8.3, 8.4, 8.5]
178+
continue-on-error: ${{ matrix.php == '8.5' }}
179+
steps:
180+
- uses: actions/checkout@v4
181+
182+
- name: Test PHPQA image
183+
run: |
184+
chmod +x tests/test-image.sh
185+
./tests/test-image.sh ${{ matrix.php }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
# Temporary files
99
tmp-phpqa/
1010
*.backup
11+
12+
# Test fixtures and temporary test files
13+
tests/fixtures/*.tgz
14+
tests/fixtures/*.tar.gz

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ RUN composer global bin phpunit require \
7979
digitalrevolution/phpunit-extensions \
8080
symfony/browser-kit:"^6.4|^7.0|^8.0" \
8181
symfony/css-selector:"^6.4|^7.0|^8.0" \
82-
symfony/panther:"^2.0" \
82+
symfony/panther:"^2.3" \
8383
zenstruck/foundry:"^2.8" \
8484
--no-scripts --no-interaction --no-suggest \
8585
&& composer global clear-cache
@@ -112,5 +112,9 @@ RUN if [ "$WITH_CHROMIUM" = "true" ]; then \
112112
echo 'export PANTHER_FIREFOX_ARGUMENTS="-headless"' >> /etc/profile.d/panther.sh; \
113113
fi
114114

115+
# Fix permissions for /tools directory to allow cache operations
116+
RUN chown -R 1001:1001 /tools \
117+
&& chmod -R 755 /tools
118+
115119
# Reset permissions to default non-root user (1001 as per your workflow)
116120
USER 1001

SUMMARY.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ use function Castor\io;
3939
$phpqaFile = __DIR__ . '/.castor-cache/phpqa.php';
4040
$phpqaUrl = 'https://raw.githubusercontent.com/Spomky-Labs/phpqa/main/.castor/phpqa.php';
4141

42+
$cacheDir = __DIR__ . '/.castor-cache';
43+
if (!is_dir($cacheDir) && !mkdir($cacheDir, 0755, true) && !is_dir($cacheDir)) {
44+
throw new \RuntimeException(sprintf('Directory "%s" was not created', $cacheDir));
45+
}
46+
4247
if (!file_exists($phpqaFile)) {
4348
io()->note('Downloading PHPQA tasks...');
4449
file_put_contents($phpqaFile, file_get_contents($phpqaUrl));
@@ -328,3 +333,48 @@ You now have:
328333
✅ Comprehensive documentation
329334

330335
**Time saved: 80-90% reduction in QA configuration maintenance**
336+
337+
## 🔧 Docker Image Improvements (Dec 2024)
338+
339+
### Issues Fixed
340+
341+
**Issue 1: Cache Extraction Permissions**
342+
- **Problem**: tar failed with "Cannot utime: Operation not permitted" when GitHub Actions tried to restore caches
343+
- **Root Cause**: `/tools` directory owned by root, but container runs as user 1001
344+
- **Fix**: Added proper ownership in Dockerfile:116-117
345+
346+
**Issue 2: Symfony Panther/BrowserKit Incompatibility**
347+
- **Problem**: Fatal error with Panther < 2.3 and BrowserKit 8.0
348+
- **Root Cause**: Old Panther versions incompatible with strict typing in BrowserKit 8.0
349+
- **Fix**: Updated Panther constraint to `^2.3` in Dockerfile:82
350+
351+
### Testing Infrastructure
352+
353+
New automated tests prevent regressions:
354+
355+
1. **`tests/test-image.sh`** - Validates published images (10 comprehensive tests)
356+
2. **`tests/test-local-build.sh`** - Tests local builds before publishing
357+
3. **GitHub Actions CI** - Automatic testing after each image build
358+
4. **`tests/README.md`** - Complete testing documentation
359+
360+
**Test Coverage:**
361+
- Essential tools verification (tar, git, composer, etc.)
362+
- PHP version validation
363+
- Directory permissions (1001:1001)
364+
- Cache write permissions
365+
- Symfony Panther/BrowserKit compatibility
366+
- Tar functionality with timestamp operations
367+
- PHP extensions availability
368+
369+
### Running Tests
370+
371+
```bash
372+
# Test published image
373+
./tests/test-image.sh 8.4
374+
375+
# Test local build
376+
./tests/test-local-build.sh 8.4
377+
378+
# Test all versions
379+
for v in 8.2 8.3 8.4; do ./tests/test-image.sh $v; done
380+
```

tests/README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# PHPQA Image Tests
2+
3+
This directory contains automated tests to validate the PHPQA Docker images.
4+
5+
## Test Scripts
6+
7+
### `test-image.sh` - Test Published Images
8+
9+
Tests a published PHPQA image from the GitHub Container Registry.
10+
11+
**Usage:**
12+
```bash
13+
./tests/test-image.sh [PHP_VERSION]
14+
```
15+
16+
**Examples:**
17+
```bash
18+
# Test PHP 8.4 image (default)
19+
./tests/test-image.sh
20+
21+
# Test PHP 8.3 image
22+
./tests/test-image.sh 8.3
23+
24+
# Test all versions
25+
for version in 8.2 8.3 8.4; do
26+
./tests/test-image.sh $version
27+
done
28+
```
29+
30+
### `test-local-build.sh` - Test Local Build
31+
32+
Builds and tests a local PHPQA image before publishing.
33+
34+
**Usage:**
35+
```bash
36+
./tests/test-local-build.sh [PHP_VERSION]
37+
```
38+
39+
**Examples:**
40+
```bash
41+
# Build and test PHP 8.4 image
42+
./tests/test-local-build.sh 8.4
43+
44+
# Build and test PHP 8.3 image
45+
./tests/test-local-build.sh 8.3
46+
```
47+
48+
## Test Coverage
49+
50+
Both test scripts verify:
51+
52+
1. **Essential Tools**: Ensures tar, git, curl, wget, composer, php, and castor are installed
53+
2. **PHP Version**: Verifies the correct PHP version is installed
54+
3. **Directory Permissions**: Checks that `/tools` directory has correct ownership (1001:1001)
55+
4. **Cache Permissions**: Validates write permissions for `/tools/.composer/cache`
56+
5. **Symfony Panther Version**: Ensures Panther 2.3+ is installed (compatible with BrowserKit 8.0)
57+
6. **BrowserKit Compatibility**: Tests that Panther and BrowserKit can work together
58+
7. **Tar Functionality**: Simulates cache extraction scenarios
59+
8. **PHP Extensions**: Verifies essential PHP extensions are loaded
60+
9. **Timestamp Operations**: Tests tar's ability to handle file timestamps (utime)
61+
62+
## Known Issues & Fixes
63+
64+
### Issue 1: Tar Cache Extraction Failure (Fixed)
65+
66+
**Problem:**
67+
```
68+
/usr/bin/tar: ../../../tools/.composer/cache: Cannot utime: Operation not permitted
69+
```
70+
71+
**Root Cause:**
72+
The `/tools` directory was owned by root, but the container runs as user 1001, preventing tar from setting file timestamps when extracting GitHub Actions caches.
73+
74+
**Fix:**
75+
Added permissions fix in Dockerfile (line 116-117):
76+
```dockerfile
77+
RUN chown -R 1001:1001 /tools \
78+
&& chmod -R 755 /tools
79+
```
80+
81+
### Issue 2: Symfony Panther/BrowserKit Incompatibility (Fixed)
82+
83+
**Problem:**
84+
```
85+
Fatal error: Declaration of Symfony\Component\Panther\Client::doRequest($request)
86+
must be compatible with Symfony\Component\BrowserKit\AbstractBrowser::doRequest(object $request): object
87+
```
88+
89+
**Root Cause:**
90+
Panther versions < 2.3 are incompatible with BrowserKit 8.0 due to strict type declarations.
91+
92+
**Fix:**
93+
Updated Panther constraint in Dockerfile (line 82):
94+
```dockerfile
95+
symfony/panther:"^2.3" # Changed from "^2.0"
96+
```
97+
98+
## CI/CD Integration
99+
100+
The tests are automatically run in GitHub Actions after each image build:
101+
102+
- **Workflow:** `.github/workflows/docker.yml`
103+
- **Job:** `test`
104+
- **Trigger:** After the `manifest` job completes
105+
- **Matrices:** Tests all PHP versions (8.2, 8.3, 8.4, 8.5)
106+
107+
## Local Development Workflow
108+
109+
1. Make changes to the Dockerfile
110+
2. Build and test locally:
111+
```bash
112+
./tests/test-local-build.sh 8.4
113+
```
114+
3. If tests pass, commit and push
115+
4. GitHub Actions will build and test all versions automatically
116+
117+
## Adding New Tests
118+
119+
To add new tests, edit `test-image.sh` and `test-local-build.sh`:
120+
121+
1. Add a new test section with clear numbering
122+
2. Use descriptive echo statements
123+
3. Verify the test fails appropriately when conditions aren't met
124+
4. Ensure the test exits with code 1 on failure
125+
5. Update this README with the new test description
126+
127+
## Troubleshooting
128+
129+
### Test fails with "Image not found"
130+
131+
The image hasn't been published yet. Use `test-local-build.sh` instead.
132+
133+
### Test fails with "Permission denied"
134+
135+
Make the script executable:
136+
```bash
137+
chmod +x tests/test-image.sh tests/test-local-build.sh
138+
```
139+
140+
### Test fails with tar errors
141+
142+
Check the `/tools` directory permissions in the image:
143+
```bash
144+
docker run --rm ghcr.io/spomky-labs/phpqa:8.4 stat -c '%u:%g %a' /tools
145+
```
146+
147+
Should output: `1001:1001 755`

tests/fixtures/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)