Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add behat Testing #52

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions .github/workflows/behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
on:
pull_request:
workflow_dispatch:
push:
branches:
- master
- develop

name: Behat Test 👨‍🔧

jobs:
Behat-Test:
runs-on: ubuntu-latest
name: Behat Tests - PHP ${{ matrix.php }}
strategy:
fail-fast: false
matrix:
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
steps:
- name: Check out source code
uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '${{ matrix.php }}'
coverage: none
tools: composer
extensions: pcntl, curl, sqlite3, zip, dom, mbstring, json, xml

- name: Get Composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Set up Composer caching
uses: actions/cache@v4
env:
cache-name: cache-composer-dependencies
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-

- name: Update docker
run: |
sudo apt remove --purge nginx nginx-common docker docker-engine docker.io docker-ce containerd runc
curl -fsSL https://get.docker.com/ | sudo bash
sudo systemctl restart docker.service

- name: Install docker-compose
run: |
VERSION=$(curl --silent "https://api.github.com/repos/docker/compose/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/'
)
sudo curl -L "https://github.com/docker/compose/releases/download/$VERSION/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

- name: Install dependencies
run: |
cd "$GITHUB_WORKSPACE/.."
git clone https://github.com/EasyEngine/easyengine.git easyengine --depth=1
cd easyengine
rm -rf features
cp -R $GITHUB_WORKSPACE/features .
sed -i 's/\(easyengine\/.*\):\ \".*\"/\1:\ \"dev-develop\"/' composer.json
composer update --prefer-dist --no-progress --no-interaction --no-dev
php -dphar.readonly=0 utils/make-phar.php easyengine.phar
sudo cp easyengine.phar /usr/local/bin/ee
composer update --prefer-dist --no-progress --no-interaction --no-plugins

- name: Test
shell: 'script -q -e -c "bash {0}"'
run: |
set -e
cd "$GITHUB_WORKSPACE/../easyengine"
sudo -E ./vendor/bin/behat
env:
COMPOSE_INTERACTIVE_NO_CLI: 1
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"admin-tools enable",
"admin-tools disable"
]
},
"require-dev": {
"behat/behat": "^3.14"
}
}
8 changes: 8 additions & 0 deletions features/admintools.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Feature: EasyEngine Admin Tools

Scenario: Enable and disable admin tools with EasyEngine
Given I have created a WordPress site at "example.com"
When I run "ee admin-tools enable example.com"
Then I should be able to access "http://example.com/ee-admin/"
When I run "ee admin-tools disable example.com"
Then I should not be able to access "http://example.com/ee-admin/"
90 changes: 90 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;

class FeatureContext implements Context, SnippetAcceptingContext
{
private $command_output;
private static $site_created = false;

/**
* @Given I have created a WordPress site at :site
*/
public function iHaveCreatedAWordpressSiteAtExampleCom()
{
if (!self::$site_created) {
$site = "example.com";
$site_directory = "/opt/easyengine/sites/$site";

if (!file_exists($site_directory)) {
$command = "ee site create $site --type=php";
$this->command_output = shell_exec($command);

$site_url = "http://$site";
sleep(5);
$site_accessible = $this->isSiteAccessible($site_url);

if (!$site_accessible) {
throw new Exception("Failed to create WordPress site at $site. The site is not accessible.");
}
} else {
echo "WordPress site at $site already exists. Skipping site creation.";
}

self::$site_created = true;
}
}

/**
* Check if a site is accessible
*
* @param string $site_url The URL of the site to check
* @return bool True if the site is accessible, false otherwise
*/
private function isSiteAccessible($site_url)
{
$headers = get_headers($site_url);
return $headers && strpos($headers[0], '200') !== false;
}

/**
* @When I run "ee admin-tools enable :site"
*/
public function iRunEnableAdminTools($site)
{
$this->command_output = shell_exec("ee admin-tools enable $site");
}

/**
* @When I run "ee admin-tools disable :site"
*/
public function iRunDisableAdminTools($site)
{
$this->command_output = shell_exec("ee admin-tools disable $site");
}

/**
* @Then I should be able to access :url
*/
public function iShouldBeAbleToAccess($url)
{
sleep(5);
$headers = @get_headers($url);
if (!$headers || strpos($headers[0], '200') === false) {
throw new Exception("Failed to access $url. Expected 200 status code, but got: " . (is_array($headers) ? implode(', ', $headers) : 'No headers received'));
}
}

/**
* @Then I should not be able to access :url
*/
public function iShouldNotBeAbleToAccess($url)
{
sleep(5);
$headers = @get_headers($url);
if (!$headers || strpos($headers[0], '403') === false) {
throw new Exception("Failed to access $url. Expected 403 status code, but got: " . (is_array($headers) ? implode(', ', $headers) : 'No headers received'));
}
}
}