77
88use Behat \Behat \Context \Context ;
99use Behat \Behat \Hook \Scope \AfterFeatureScope ;
10-
10+ use Behat \ Behat \ Hook \ Scope \ AfterScenarioScope ;
1111
1212use Behat \Gherkin \Node \PyStringNode ,
1313 Behat \Gherkin \Node \TableNode ;
1414
15+ define ( 'EE_SITE_ROOT ' , getenv ('HOME ' ) . '/ee-sites/ ' );
16+
1517class FeatureContext implements Context
1618{
1719 public $ command ;
1820 public $ webroot_path ;
21+ public $ ee_path ;
22+
23+ /**
24+ * Initializes context.
25+ */
26+ public function __construct ()
27+ {
28+ $ this ->commands = [];
29+ $ this ->ee_path = getcwd ();
30+ }
1931
2032 /**
2133 * @Given ee phar is generated
@@ -55,14 +67,35 @@ public function isInstalled($command)
5567 */
5668 public function iRun ($ command )
5769 {
58- $ this ->command = EE ::launch ($ command , false , true );
70+ $ this ->commands [] = EE ::launch ($ command , false , true );
5971 }
72+
73+ /**
74+ * @When I create subsite :subsite in :site
75+ */
76+ public function iCreateSubsiteInOfType ($ subsite , $ site )
77+ {
78+ $ php_container = implode ( explode ( '. ' , $ subsite ) ) . '_php_1 ' ;
79+
80+ EE ::launch ( "docker exec -it --user='www-data' $ php_container sh -c 'wp site create --slug= $ site' " , false , true );
81+ }
82+
6083 /**
6184 * @Then return value should be 0
6285 */
63- public function returnCodeShouldBe0 ()
86+ public function returnValueShouldBe0 ()
6487 {
65- if ( 0 !== $ this ->command ->return_code ) {
88+ if ( 0 !== $ this ->commands [0 ]->return_code ) {
89+ throw new Exception ("Actual return code is not zero: \n" . $ this ->command );
90+ }
91+ }
92+
93+ /**
94+ * @Then return value of command :index should be 0
95+ */
96+ public function returnValueOfCommandShouldBe0 ($ index )
97+ {
98+ if ( 0 !== $ this ->commands [ $ index - 1 ]->return_code ) {
6699 throw new Exception ("Actual return code is not zero: \n" . $ this ->command );
67100 }
68101 }
@@ -80,7 +113,21 @@ public function afterDelayOfSeconds( $time )
80113 */
81114 public function stdoutShouldReturnExactly ($ output_stream , PyStringNode $ expected_output )
82115 {
83- $ command_output = $ output_stream === "STDOUT " ? $ this ->command ->stdout : $ this ->command ->stderr ;
116+ $ command_output = $ output_stream === "STDOUT " ? $ this ->commands [0 ]->stdout : $ this ->commands [0 ]->stderr ;
117+
118+ $ command_output = str_replace (["\033[1;31m " ,"\033[0m " ],'' ,$ command_output );
119+
120+ if ( $ expected_output ->getRaw () !== trim ($ command_output )) {
121+ throw new Exception ("Actual output is: \n" . $ command_output );
122+ }
123+ }
124+
125+ /**
126+ * @Then /(STDOUT|STDERR) of command :index should return exactly/
127+ */
128+ public function stdoutOfCommandShouldReturnExactly ($ output_stream , $ index , PyStringNode $ expected_output )
129+ {
130+ $ command_output = $ output_stream === "STDOUT " ? $ this ->commands [ $ index - 1 ]->stdout : $ this ->commands [ $ index - 1 ]->stderr ;
84131
85132 $ command_output = str_replace (["\033[1;31m " ,"\033[0m " ],'' ,$ command_output );
86133
@@ -95,14 +142,50 @@ public function stdoutShouldReturnExactly($output_stream, PyStringNode $expected
95142 */
96143 public function stdoutShouldReturnSomethingLike ($ output_stream , PyStringNode $ expected_output )
97144 {
98- $ command_output = $ output_stream === "STDOUT " ? $ this ->command ->stdout : $ this ->command ->stderr ;
145+ $ command_output = $ output_stream === "STDOUT " ? $ this ->commands [ 0 ] ->stdout : $ this ->commands [ 0 ] ->stderr ;
99146
100147 $ expected_out = isset ($ expected_output ->getStrings ()[0 ]) ? $ expected_output ->getStrings ()[0 ] : '' ;
101148 if (strpos ($ command_output , $ expected_out ) === false ) {
102149 throw new Exception ("Actual output is: \n" . $ command_output );
103150 }
104151 }
105152
153+
154+ /**
155+ * @Then /(STDOUT|STDERR) of command :index should return something like/
156+ */
157+ public function stdoutOfCommandShouldReturnSomethingLike ($ output_stream , $ index , PyStringNode $ expected_output )
158+ {
159+ $ command_output = $ output_stream === "STDOUT " ? $ this ->commands [ $ index - 1 ]->stdout : $ this ->commands [ $ index - 1 ]->stderr ;
160+
161+ $ expected_out = isset ($ expected_output ->getStrings ()[0 ]) ? $ expected_output ->getStrings ()[0 ] : '' ;
162+ if (strpos ($ command_output , $ expected_out ) === false ) {
163+ throw new Exception ("Actual output is: \n" . $ command_output );
164+ }
165+ }
166+
167+ /**
168+ * @Then The site :site should be :type multisite
169+ */
170+ public function theSiteShouldBeMultisite ( $ site , $ type )
171+ {
172+ $ result = EE ::launch ("cd " . EE_SITE_ROOT . "$ site && docker-compose exec --user='www-data' php sh -c 'wp config get SUBDOMAIN_INSTALL' " , false , true );
173+
174+ if ( $ result ->stderr ) {
175+ throw new Exception ("Found error while executing command: $ result ->stderr " );
176+ }
177+
178+ if ($ type === 'subdomain ' && trim ($ result ->stdout ) !== '1 ' ) {
179+ throw new Exception ("Expecting SUBDOMAIN_INSTALL to be 1. Got: $ result ->stdout " );
180+ }
181+ else if ($ type === 'subdir ' && trim ($ result ->stdout ) !== '' ) {
182+ throw new Exception ("Expecting SUBDOMAIN_INSTALL to be empty. Got: $ result ->stdout " );
183+ }
184+ else if ($ type !== 'subdomain ' && $ type !== 'subdir ' ) {
185+ throw new Exception ("Found unknown site type: $ type " );
186+ }
187+ }
188+
106189 /**
107190 * @Then The :site db entry should be removed
108191 */
@@ -120,7 +203,7 @@ public function theDbEntryShouldBeRemoved($site)
120203 */
121204 public function theWebrootShouldBeRemoved ($ site )
122205 {
123- if (file_exists (getenv ( ' HOME ' ) . " /ee-sites/ " . $ site )) {
206+ if (file_exists (EE_SITE_ROOT . $ site )) {
124207 throw new Exception ("Webroot has not been removed! " );
125208 }
126209 }
@@ -150,7 +233,7 @@ public function followingContainersOfSiteShouldBeRemoved($site, TableNode $table
150233 */
151234 public function theSiteShouldHaveWebroot ($ site )
152235 {
153- if (!file_exists (getenv ( ' HOME ' ) . " /ee-sites/ " . $ site )) {
236+ if (!file_exists (EE_SITE_ROOT . $ site )) {
154237 throw new Exception ("Webroot has not been created! " );
155238 }
156239 }
@@ -160,7 +243,7 @@ public function theSiteShouldHaveWebroot($site)
160243 */
161244 public function theSiteShouldHaveWordpress ($ site )
162245 {
163- if (!file_exists (getenv ( ' HOME ' ) . " /ee-sites/ " . $ site . "/app/src/wp-config.php " )) {
246+ if (!file_exists (EE_SITE_ROOT . $ site . "/app/src/wp-config.php " )) {
164247 throw new Exception ("WordPress data not found! " );
165248 }
166249 }
@@ -244,13 +327,24 @@ public function requestOnWithResolveOptionShouldContainFollowingHeaders($host, $
244327 }
245328 }
246329
330+ /**
331+ * @AfterScenario
332+ */
333+ public function cleanupScenario (AfterScenarioScope $ scope )
334+ {
335+ $ this ->commands = [];
336+ chdir ($ this ->ee_path );
337+ }
338+
247339 /**
248340 * @AfterFeature
249341 */
250342 public static function cleanup (AfterFeatureScope $ scope )
251343 {
252344 $ test_sites = [
253- 'hello.test ' ,
345+ 'wp.test ' ,
346+ 'wpsubdom.test ' ,
347+ 'wpsubdir.test ' ,
254348 'example.test ' ,
255349 'www.example1.test ' ,
256350 'example2.test ' ,
0 commit comments