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