Skip to content

Commit 8e65aca

Browse files
Add Behat Testing
Signed-off-by: Immanuel Raj <iamimmanuelraj@gmail.com>
1 parent 7aa5af3 commit 8e65aca

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

features/admintools.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: EasyEngine Admin Tools
2+
3+
Scenario: Enable and disable admin tools with EasyEngine
4+
Given I have installed EasyEngine if not installed
5+
And I have created a WordPress site at "example.com"
6+
When I run "ee admin-tools enable example.com"
7+
Then I should be able to access "http://example.com/ee-admin/"
8+
When I run "ee admin-tools disable example.com"
9+
Then I should not be able to access "http://example.com/ee-admin/"
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
use Behat\Behat\Context\Context;
4+
use Behat\Behat\Context\SnippetAcceptingContext;
5+
6+
class FeatureContext implements Context, SnippetAcceptingContext
7+
{
8+
private $command_output;
9+
private static $easyengine_installed = false;
10+
private static $site_created = false;
11+
12+
/**
13+
* @Given I have installed EasyEngine if not installed
14+
*/
15+
public function iHaveInstalledEasyengineIfNotInstalled()
16+
{
17+
if (!self::$easyengine_installed) {
18+
$install_command = 'wget -qO ee https://rt.cx/ee4 && sudo bash ee && sudo rm ee';
19+
$install_output = shell_exec($install_command);
20+
if (!file_exists('/usr/local/bin/ee')) {
21+
throw new Exception("EasyEngine could not be installed.");
22+
}
23+
self::$easyengine_installed = true;
24+
}
25+
}
26+
27+
/**
28+
* @Given I have created a WordPress site at :site
29+
*/
30+
public function iHaveCreatedAWordpressSiteAtExampleCom()
31+
{
32+
if (!self::$site_created) {
33+
$site = "example.com";
34+
$site_directory = "/opt/easyengine/sites/$site";
35+
36+
if (!file_exists($site_directory)) {
37+
$command = "ee site create $site --type=php";
38+
$this->command_output = shell_exec($command);
39+
40+
$site_url = "http://$site";
41+
sleep(5);
42+
$site_accessible = $this->isSiteAccessible($site_url);
43+
44+
if (!$site_accessible) {
45+
throw new Exception("Failed to create WordPress site at $site. The site is not accessible.");
46+
}
47+
} else {
48+
echo "WordPress site at $site already exists. Skipping site creation.";
49+
}
50+
51+
self::$site_created = true;
52+
}
53+
}
54+
55+
/**
56+
* Check if a site is accessible
57+
*
58+
* @param string $site_url The URL of the site to check
59+
* @return bool True if the site is accessible, false otherwise
60+
*/
61+
private function isSiteAccessible($site_url)
62+
{
63+
$headers = get_headers($site_url);
64+
return $headers && strpos($headers[0], '200') !== false;
65+
}
66+
67+
/**
68+
* @When I run "ee admin-tools enable :site"
69+
*/
70+
public function iRunEnableAdminTools($site)
71+
{
72+
$this->command_output = shell_exec("ee admin-tools enable $site");
73+
}
74+
75+
/**
76+
* @When I run "ee admin-tools disable :site"
77+
*/
78+
public function iRunDisableAdminTools($site)
79+
{
80+
$this->command_output = shell_exec("ee admin-tools disable $site");
81+
}
82+
83+
/**
84+
* @Then I should be able to access :url
85+
*/
86+
public function iShouldBeAbleToAccess($url)
87+
{
88+
sleep(5);
89+
$headers = @get_headers($url);
90+
if (!$headers || strpos($headers[0], '200') === false) {
91+
throw new Exception("Failed to access $url. Expected 200 status code, but got: " . (is_array($headers) ? implode(', ', $headers) : 'No headers received'));
92+
}
93+
}
94+
95+
/**
96+
* @Then I should not be able to access :url
97+
*/
98+
public function iShouldNotBeAbleToAccess($url)
99+
{
100+
sleep(5);
101+
$headers = @get_headers($url);
102+
if (!$headers || strpos($headers[0], '403') === false) {
103+
throw new Exception("Failed to access $url. Expected 403 status code, but got: " . (is_array($headers) ? implode(', ', $headers) : 'No headers received'));
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)