From 7aa5af3ce51ec5bcf2b69b8d0dffd6156856e15f Mon Sep 17 00:00:00 2001 From: Immanuel Raj Date: Mon, 3 Jun 2024 09:38:01 +0000 Subject: [PATCH 1/4] composer: Add behat dev dependency Signed-off-by: Immanuel Raj --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 51d545a..879f5e5 100644 --- a/composer.json +++ b/composer.json @@ -23,5 +23,8 @@ "admin-tools enable", "admin-tools disable" ] + }, + "require-dev": { + "behat/behat": "^3.14" } } From 8e65aca4b3705725406ba9ac3311e2616164a42d Mon Sep 17 00:00:00 2001 From: Immanuel Raj Date: Mon, 3 Jun 2024 10:24:41 +0000 Subject: [PATCH 2/4] Add Behat Testing Signed-off-by: Immanuel Raj --- features/admintools.feature | 9 +++ features/bootstrap/FeatureContext.php | 106 ++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 features/admintools.feature create mode 100644 features/bootstrap/FeatureContext.php diff --git a/features/admintools.feature b/features/admintools.feature new file mode 100644 index 0000000..5cd392d --- /dev/null +++ b/features/admintools.feature @@ -0,0 +1,9 @@ +Feature: EasyEngine Admin Tools + + Scenario: Enable and disable admin tools with EasyEngine + Given I have installed EasyEngine if not installed + And 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/" \ No newline at end of file diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php new file mode 100644 index 0000000..b72ce78 --- /dev/null +++ b/features/bootstrap/FeatureContext.php @@ -0,0 +1,106 @@ +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')); + } + } +} \ No newline at end of file From f4744dde917ecca5fe71486c33c6204fa827cbf5 Mon Sep 17 00:00:00 2001 From: Immanuel Raj Date: Thu, 6 Jun 2024 16:36:07 +0530 Subject: [PATCH 3/4] Add action for behat testing Signed-off-by: Immanuel Raj --- .github/workflows/behat.yml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .github/workflows/behat.yml diff --git a/.github/workflows/behat.yml b/.github/workflows/behat.yml new file mode 100644 index 0000000..f42b1f0 --- /dev/null +++ b/.github/workflows/behat.yml @@ -0,0 +1,37 @@ +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: Install dependencies + run: | + composer install --prefer-dist --no-progress --no-interaction + + - name: Test + run: | + sudo -E ./vendor/bin/behat \ No newline at end of file From dfae09d0fd0e85d44b43e1897c6c8dcada9b26b3 Mon Sep 17 00:00:00 2001 From: Immanuel Raj Date: Tue, 11 Jun 2024 15:19:43 +0530 Subject: [PATCH 4/4] Behat: Install EasyEngine by default - Let's not do it in the behat test - Install Latest ee (Develop Branch) and test changes against it. - Use latest docker and docker compose - Use composer chache in github actions - Remove unnecessary packages Signed-off-by: Immanuel Raj --- .github/workflows/behat.yml | 47 +++++++++++++++++++++++++-- features/admintools.feature | 3 +- features/bootstrap/FeatureContext.php | 16 --------- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/.github/workflows/behat.yml b/.github/workflows/behat.yml index f42b1f0..e256685 100644 --- a/.github/workflows/behat.yml +++ b/.github/workflows/behat.yml @@ -28,10 +28,53 @@ jobs: 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: | - composer install --prefer-dist --no-progress --no-interaction + 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: | - sudo -E ./vendor/bin/behat \ No newline at end of file + set -e + cd "$GITHUB_WORKSPACE/../easyengine" + sudo -E ./vendor/bin/behat + env: + COMPOSE_INTERACTIVE_NO_CLI: 1 \ No newline at end of file diff --git a/features/admintools.feature b/features/admintools.feature index 5cd392d..03a66e5 100644 --- a/features/admintools.feature +++ b/features/admintools.feature @@ -1,8 +1,7 @@ Feature: EasyEngine Admin Tools Scenario: Enable and disable admin tools with EasyEngine - Given I have installed EasyEngine if not installed - And I have created a WordPress site at "example.com" + 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" diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index b72ce78..7c48132 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -6,24 +6,8 @@ class FeatureContext implements Context, SnippetAcceptingContext { private $command_output; - private static $easyengine_installed = false; private static $site_created = false; - /** - * @Given I have installed EasyEngine if not installed - */ - public function iHaveInstalledEasyengineIfNotInstalled() - { - if (!self::$easyengine_installed) { - $install_command = 'wget -qO ee https://rt.cx/ee4 && sudo bash ee && sudo rm ee'; - $install_output = shell_exec($install_command); - if (!file_exists('/usr/local/bin/ee')) { - throw new Exception("EasyEngine could not be installed."); - } - self::$easyengine_installed = true; - } - } - /** * @Given I have created a WordPress site at :site */