Skip to content

Commit

Permalink
Removed Docker for PHP server.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Nov 4, 2024
1 parent 5038495 commit cbc0300
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 132 deletions.
74 changes: 37 additions & 37 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,59 @@ jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
php-versions: ['8.2', '8.3']

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}

- name: Set up Docker Compose
run: docker compose up -d --build
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: pcov
ini-values: pcov.directory=.

- name: Copy codebase into container
run: |
container_id="$(docker compose ps -q phpserver)"
docker cp -L . "${container_id}":/app/
- name: Install dependencies
run: composer install

- name: Install Composer dependencies
run: docker compose exec phpserver composer install --ansi --no-suggest
- name: Validate composer.json
run: |
composer --verbose validate
composer normalize --dry-run
- name: Lint code
run: docker compose exec phpserver composer lint
- name: Check coding standards
run: composer lint
continue-on-error: ${{ vars.CI_LINT_IGNORE_FAILURE == '1' }}

- name: Restart server with Xdebug enabled
run: XDEBUG_ENABLE=true docker compose up -d phpserver
- name: Run unit tests
run: composer test-unit
continue-on-error: ${{ vars.CI_TEST_UNIT_IGNORE_FAILURE == '1' }}

- name: Run tests with PHPUnit
run: docker compose exec -T -e XDEBUG_MODE=coverage phpserver vendor/bin/phpunit
continue-on-error: ${{ vars.CI_TEST_IGNORE_FAILURE == '1' }}

- name: Run tests with Behat
run: docker compose exec -T -e XDEBUG_MODE=coverage phpserver vendor/bin/behat
continue-on-error: ${{ vars.CI_TEST_IGNORE_FAILURE == '1' }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Process test logs and artifacts
run: |
mkdir -p /tmp/logs
if docker compose ps --services --filter "status=running" | grep -q phpserver && docker compose exec phpserver test -d /app/.logs; then
docker compose cp phpserver:/app/.logs/. "/tmp/logs/"
fi
- name: Set up Docker Compose
run: docker compose up -d

- name: Upload artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: artifacts
path: /tmp/logs
- name: Run BDD tests
run: composer test-bdd
continue-on-error: ${{ vars.CI_TEST_BDD_IGNORE_FAILURE == '1' }}

- name: Upload logs as artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: logs
path: /tmp/logs
name: ${{github.job}}-logs-${{ matrix.php-versions }}
path: .logs
include-hidden-files: true
if-no-files-found: error

Expand All @@ -81,8 +81,8 @@ jobs:
if: ${{ env.CODECOV_TOKEN != '' }}
with:
files: |
/tmp/logs/coverage/behat/cobertura.xml
/tmp/logs/coverage/phpunit/cobertura.xml
.logs/coverage/behat/cobertura.xml
.logs/coverage/phpunit/cobertura.xml
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
env:
Expand Down
25 changes: 7 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,38 +129,27 @@ You may optionally specify size of browser window in the screenshot step:
### Local development setup

```bash
docker compose up -d
docker compose exec phpserver composer install --ansi
composer install # Install dependencies.
docker compose up -d # Start Chrome container.
```

### Lint code

```bash
docker compose exec phpserver composer lint
composer lint
```

### Lint fix
### Fix code style

```bash
docker compose exec phpserver composer lint-fix
composer lint-fix
```

### Run tests

```bash
docker compose exec phpserver composer test
```

### Enable Xdebug

```bash
XDEBUG_ENABLE=true docker compose up -d phpserver
```

To disable, run

```bash
docker compose up -d phpserver
composer test-unit # Run unit tests.
composer test-bdd # Run BDD tests.
```

---
Expand Down
38 changes: 20 additions & 18 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
default:
autoload: [ "%paths.base%/tests/behat/bootstrap" ]
autoload: ["%paths.base%/tests/behat/bootstrap"]
suites:
default:
paths: [ "%paths.base%/tests/behat/features" ]
paths: ["%paths.base%/tests/behat/features"]
contexts:
- FeatureContext:
- screenshot_dir: '%paths.base%/.logs/screenshots'
javascript_base_url: 'http://host.docker.internal:8888'
- BehatCliContext
- DrevOps\BehatPhpServer\PhpServerContext:
-
docroot: "%paths.base%/tests/behat/features/fixtures"
host: "phpserver"
- DrevOps\BehatScreenshotExtension\Context\ScreenshotContext
- FeatureContext:
-
screenshot_dir: '%paths.base%/.logs/screenshots'

formatters:
pretty: true
# Disable JUnit formatter if memory leaks start to occur.
# See @https://github.com/Behat/Behat/pull/1423
junit:
output_path: '%paths.base%/.logs/test_results/behat'
- DrevOps\BehatPhpServer\PhpServerContext:
- docroot: "%paths.base%/tests/behat/features/fixtures"
host: "0.0.0.0"
port: 8888

extensions:
Behat\MinkExtension:
browserkit_http: ~
files_path: "%paths.base%/tests/behat/features/fixtures"
base_url: http://0.0.0.0:8888
browser_name: chrome
base_url: http://phpserver:8888
javascript_session: selenium2
selenium2:
wd_host: "http://chrome:4444/wd/hub"
wd_host: "http://localhost:4445/wd/hub"
capabilities:
browser: chrome
extra_capabilities:
Expand All @@ -43,10 +36,12 @@ default:
- '--disable-translate' # Disables the built-in translation feature, preventing Chrome from offering to translate pages.
- '--no-first-run' # Skips the initial setup screen that Chrome typically shows when running for the first time.
- '--test-type' # Disables certain security features and UI components that are unnecessary for automated testing, making Chrome more suitable for test environments.

DrevOps\BehatScreenshotExtension:
dir: '%paths.base%/.logs/screenshots'
fail: true
purge: true

DVDoug\Behat\CodeCoverage\Extension:
filter:
include:
Expand All @@ -60,3 +55,10 @@ default:
target: .logs/coverage/behat/.coverage-html
cobertura:
target: .logs/coverage/behat/cobertura.xml

formatters:
pretty: true
# Disable JUnit formatter if memory leaks start to occur.
# See @https://github.com/Behat/Behat/pull/1423
junit:
output_path: '%paths.base%/.logs/test_results/behat'
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@
"rector --clear-cache",
"phpcbf"
],
"test": [
"if [ \"${XDEBUG_MODE}\" = 'coverage' ]; then phpunit; else phpunit --no-coverage; fi",
"behat"
]
"reset": "rm -Rf vendor vendor-bin composer.lock",
"test-bdd": "behat",
"test-unit": "phpunit"
}
}
10 changes: 3 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
services:
phpserver:
image: uselagoon/php-8.2-cli:24.10.0
volumes:
- .:/app:delegated
environment:
# To enable xdebug: XDEBUG_ENABLE=true docker compose up -d phpserver
XDEBUG_ENABLE: ${XDEBUG_ENABLE:-}
chrome:
image: selenium/standalone-chromium:130.0
ports:
- 4445:4444
- 7901:7900
22 changes: 10 additions & 12 deletions tests/behat/bootstrap/BehatCliTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function __construct($parameters)
*/
public function goToPhpServerTestPage()
{
$this->getSession()->visit('http://phpserver:8888/screenshot.html');
$this->getSession()->visit('http://0.0.0.0:8888/screenshot.html');
}
/**
Expand Down Expand Up @@ -171,12 +171,11 @@ public function behatCliWriteBehatYml() {
default:
contexts:
- FeatureContextTest:
-
screenshot_dir: "%paths.base%/screenshots"
- screenshot_dir: "%paths.base%/screenshots"
- DrevOps\BehatPhpServer\PhpServerContext:
-
docroot: "%paths.base%/tests/behat/features/fixtures"
host: "phpserver"
- docroot: "%paths.base%/tests/behat/features/fixtures"
host: "0.0.0.0"
port: 8888
extensions:
Behat\MinkExtension:
browserkit_http: ~
Expand All @@ -202,18 +201,17 @@ public function behatCliWriteScreenshotContextBehatYml(PyStringNode $value) {
default:
contexts:
- FeatureContextTest:
-
screenshot_dir: "%paths.base%/screenshots"
- screenshot_dir: "%paths.base%/screenshots"
- DrevOps\BehatPhpServer\PhpServerContext:
-
docroot: "%paths.base%/tests/behat/features/fixtures"
host: "phpserver"
- docroot: "%paths.base%/tests/behat/features/fixtures"
host: "0.0.0.0"
port: 8888
- DrevOps\BehatScreenshotExtension\Context\ScreenshotContext
extensions:
Behat\MinkExtension:
browserkit_http: ~
selenium2: ~
base_url: http://nginx:8080
base_url: http://0.0.0.0:8888
EOL;

$content .= PHP_EOL . ' ' . trim((string) $value);
Expand Down
30 changes: 30 additions & 0 deletions tests/behat/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@
*/

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\MinkContext;
use Behat\MinkExtension\Context\RawMinkContext;

/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements Context {

/**
* Base URL for JavaScript scenarios.
*
* @var string
*/
protected string $javascriptBaseUrl;

use ScreenshotTrait;

/**
Expand All @@ -23,6 +33,26 @@ class FeatureContext extends MinkContext implements Context {
*/
public function __construct(array $parameters) {
$this->screenshotInitParams($parameters);

$this->javascriptBaseUrl = $parameters['javascript_base_url'] ?: 'http://host.docker.internal:8888';
}

/**
* Update base URL for JavaScript scenarios.
*
* @BeforeScenario
*/
public function beforeScenarioUpdateBaseUrl(BeforeScenarioScope $scope): void {
if ($scope->getScenario()->hasTag('javascript')) {
$environment = $scope->getEnvironment();
if ($environment instanceof InitializedContextEnvironment) {
foreach ($environment->getContexts() as $context) {
if ($context instanceof RawMinkContext) {
$context->setMinkParameter('base_url', $this->javascriptBaseUrl);
}
}
}
}
}

}
10 changes: 5 additions & 5 deletions tests/behat/features/behatcli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Feature: Behat CLI context
*/
public function goToPhpServerTestPage()
{
$this->getSession()->visit('http://phpserver:8888/screenshot.html');
$this->getSession()->visit('http://0.0.0.0:8888/screenshot.html');
}
/**
Expand All @@ -41,14 +41,14 @@ Feature: Behat CLI context
contexts:
- FeatureContextTest
- DrevOps\BehatPhpServer\PhpServerContext:
-
docroot: "%paths.base%/tests/behat/features/fixtures"
host: "phpserver"
- docroot: "%paths.base%/tests/behat/features/fixtures"
host: "0.0.0.0"
port: 8888
extensions:
Behat\MinkExtension:
browserkit_http: ~
selenium2: ~
base_url: http://nginx:8080
base_url: http://0.0.0.0:8888
"""
And a file named "tests/behat/features/fixtures/screenshot.html" with:
"""
Expand Down
Loading

0 comments on commit cbc0300

Please sign in to comment.