Skip to content

Commit 6d7596e

Browse files
committed
Merge branch 'mrrobot47-travis/add-tests-and-integration' into develop
2 parents b42e188 + 907970a commit 6d7596e

File tree

5 files changed

+519
-0
lines changed

5 files changed

+519
-0
lines changed

.travis.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
sudo: required
2+
3+
language: php
4+
php: 7.1
5+
6+
env:
7+
global:
8+
- TEST_COMMAND=$(echo $TRAVIS_REPO_SLUG | cut -d/ -f 2) # Get command name to be tested
9+
10+
before_script:
11+
- |
12+
# Remove Xdebug for a huge performance increase:
13+
if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then
14+
phpenv config-rm xdebug.ini
15+
else
16+
echo "xdebug.ini does not exist"
17+
fi
18+
- ./ci/prepare.sh
19+
20+
script:
21+
- cd "$TRAVIS_BUILD_DIR/../easyengine"
22+
- ./vendor/bin/behat
23+
24+
after_script:
25+
- cat /opt/easyengine/ee.log
26+
27+
notifications:
28+
email:
29+
on_success: never
30+
on_failure: change

ci/prepare.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# called by Travis CI
4+
5+
# install dependencies
6+
wget -qO ee https://rt.cx/ee4beta && sudo bash ee
7+
rm ee
8+
9+
# Setup EE develop repo
10+
cd ..
11+
git clone https://github.com/EasyEngine/easyengine.git easyengine --depth=1
12+
cd easyengine
13+
14+
# Copy tests to EE repo
15+
rm -r features
16+
cp -R ../$TEST_COMMAND/features .
17+
18+
# Install composer dependencies and update them for tests
19+
composer update
20+
21+
# Place the command inside EE repo
22+
rm -r vendor/easyengine/$TEST_COMMAND
23+
cp -R ../$TEST_COMMAND vendor/easyengine/
24+
25+
# Create phar and test it
26+
php -dphar.readonly=0 ./utils/make-phar.php easyengine.phar --quite > /dev/null
27+
sudo php easyengine.phar cli info
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
3+
include_once(__DIR__ . '/../../php/class-ee.php');
4+
include_once(__DIR__ . '/../../php/utils.php');
5+
6+
use Behat\Behat\Context\Context;
7+
use Behat\Behat\Hook\Scope\AfterFeatureScope;
8+
9+
10+
use Behat\Gherkin\Node\PyStringNode,
11+
Behat\Gherkin\Node\TableNode;
12+
13+
class FeatureContext implements Context
14+
{
15+
public $command;
16+
public $webroot_path;
17+
18+
/**
19+
* @Given ee phar is generated
20+
*/
21+
public function eePharIsPresent()
22+
{
23+
// Checks if phar already exists, replaces it
24+
if(file_exists('ee-old.phar')) {
25+
// Below exec call is required as currenly `ee cli update` is ran with root
26+
// which updates ee.phar with root privileges.
27+
exec("sudo rm ee.phar");
28+
copy('ee-old.phar','ee.phar');
29+
return 0;
30+
}
31+
exec("php -dphar.readonly=0 utils/make-phar.php ee.phar", $output, $return_status);
32+
if (0 !== $return_status) {
33+
throw new Exception("Unable to generate phar" . $return_status);
34+
}
35+
36+
// Cache generaed phar as it is expensive to generate one
37+
copy('ee.phar','ee-old.phar');
38+
}
39+
40+
/**
41+
* @Given :command is installed
42+
*/
43+
public function isInstalled($command)
44+
{
45+
exec("type " . $command, $output, $return_status);
46+
if (0 !== $return_status) {
47+
throw new Exception($command . " is not installed! Exit code is:" . $return_status);
48+
}
49+
}
50+
51+
/**
52+
* @When I run :command
53+
*/
54+
public function iRun($command)
55+
{
56+
$this->command = EE::launch($command, false, true);
57+
}
58+
/**
59+
* @Then return value should be 0
60+
*/
61+
public function returnCodeShouldBe0()
62+
{
63+
if ( 0 !== $this->command->return_code ) {
64+
throw new Exception("Actual return code is not zero: \n" . $this->command);
65+
}
66+
}
67+
68+
/**
69+
* @Then /(STDOUT|STDERR) should return exactly/
70+
*/
71+
public function stdoutShouldReturnExactly($output_stream, PyStringNode $expected_output)
72+
{
73+
$command_output = $output_stream === "STDOUT" ? $this->command->stdout : $this->command->stderr;
74+
75+
$command_output = str_replace(["\033[1;31m","\033[0m"],'',$command_output);
76+
77+
$expected_out = isset($expected_output->getStrings()[0]) ? $expected_output->getStrings()[0] : '';
78+
if ( $expected_out !== trim($command_output)) {
79+
throw new Exception("Actual output is:\n" . $command_output);
80+
}
81+
}
82+
83+
/**
84+
* @Then /(STDOUT|STDERR) should return something like/
85+
*/
86+
public function stdoutShouldReturnSomethingLike($output_stream, PyStringNode $expected_output)
87+
{
88+
$command_output = $output_stream === "STDOUT" ? $this->command->stdout : $this->command->stderr;
89+
90+
$expected_out = isset($expected_output->getStrings()[0]) ? $expected_output->getStrings()[0] : '';
91+
if (strpos($command_output, $expected_out) === false) {
92+
throw new Exception("Actual output is:\n" . $command_output);
93+
}
94+
}
95+
96+
/**
97+
* @Then The :site db entry should be removed
98+
*/
99+
public function theDbEntryShouldBeRemoved($site)
100+
{
101+
$out = shell_exec("sudo bin/ee site list");
102+
if (strpos($out, $site) !== false) {
103+
throw new Exception("$site db entry not been removed!");
104+
}
105+
106+
}
107+
108+
/**
109+
* @Then The :site webroot should be removed
110+
*/
111+
public function theWebrootShouldBeRemoved($site)
112+
{
113+
if (file_exists(getenv('HOME') . "/ee-sites/" . $site)) {
114+
throw new Exception("Webroot has not been removed!");
115+
}
116+
}
117+
118+
/**
119+
* @Then Following containers of site :site should be removed:
120+
*/
121+
public function followingContainersOfSiteShouldBeRemoved($site, TableNode $table)
122+
{
123+
$containers = $table->getHash();
124+
$site_name = implode(explode('.', $site));
125+
126+
foreach ($containers as $container) {
127+
128+
$sevice = $container['container'];
129+
$container_name = $site_name . '_' . $sevice . '_1';
130+
131+
exec("docker inspect -f '{{.State.Running}}' $container_name > /dev/null 2>&1", $exec_out, $return);
132+
if (!$return) {
133+
throw new Exception("$container_name has not been removed!");
134+
}
135+
}
136+
}
137+
138+
/**
139+
* @Then The site :site should have webroot
140+
*/
141+
public function theSiteShouldHaveWebroot($site)
142+
{
143+
if (!file_exists(getenv('HOME') . "/ee-sites/" . $site)) {
144+
throw new Exception("Webroot has not been created!");
145+
}
146+
}
147+
148+
/**
149+
* @Then The site :site should have WordPress
150+
*/
151+
public function theSiteShouldHaveWordpress($site)
152+
{
153+
if (!file_exists(getenv('HOME') . "/ee-sites/" . $site . "/app/src/wp-config.php")) {
154+
throw new Exception("WordPress data not found!");
155+
}
156+
}
157+
158+
/**
159+
* @Then Request on :site should contain following headers:
160+
*/
161+
public function requestOnShouldContainFollowingHeaders($site, TableNode $table)
162+
{
163+
$url = 'http://' . $site;
164+
165+
$ch = curl_init();
166+
curl_setopt($ch, CURLOPT_URL, $url);
167+
curl_setopt($ch, CURLOPT_HEADER, true);
168+
curl_setopt($ch, CURLOPT_NOBODY, true);
169+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
170+
$headers = curl_exec($ch);
171+
curl_close($ch);
172+
173+
$rows = $table->getHash();
174+
175+
foreach ($rows as $row) {
176+
if (strpos($headers, $row['header']) === false) {
177+
throw new Exception("Unable to find " . $row['header'] . "\nActual output is : " . $headers);
178+
}
179+
}
180+
}
181+
182+
/**
183+
* @AfterFeature
184+
*/
185+
public static function cleanup(AfterFeatureScope $scope)
186+
{
187+
exec("sudo bin/ee site delete hello.test");
188+
if(file_exists('ee.phar')) {
189+
unlink('ee.phar');
190+
}
191+
if(file_exists('ee-old.phar')) {
192+
unlink('ee-old.phar');
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)