diff --git a/.github/workflows/behat.yml b/.github/workflows/behat.yml new file mode 100644 index 0000000..e256685 --- /dev/null +++ b/.github/workflows/behat.yml @@ -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 \ No newline at end of file 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" } } diff --git a/features/admintools.feature b/features/admintools.feature new file mode 100644 index 0000000..03a66e5 --- /dev/null +++ b/features/admintools.feature @@ -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/" \ No newline at end of file diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php new file mode 100644 index 0000000..7c48132 --- /dev/null +++ b/features/bootstrap/FeatureContext.php @@ -0,0 +1,90 @@ +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