Skip to content

Commit 5dbafde

Browse files
MAGECLOUD-1064: Introduce functional tests (#50)
1 parent 7a71b7a commit 5dbafde

File tree

9 files changed

+162
-35
lines changed

9 files changed

+162
-35
lines changed

.travis.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
sudo: false
1+
sudo: required
22
dist: trusty
3+
addons:
4+
hosts:
5+
- magento2.travis
36
services:
47
- mysql
58
language: php
@@ -9,6 +12,8 @@ php:
912
env:
1013
global:
1114
- COMPOSER_BIN_DIR=~/bin
15+
- MAGENTO_HOST_NAME="magento2.travis"
16+
- SANDBOX_KEY="travis"
1217
matrix:
1318
- TEST_SUITE=static
1419
- TEST_SUITE=unit
@@ -27,4 +32,4 @@ script:
2732
- if [ $TEST_SUITE == "static" ]; then phpcs src --standard=tests/static/phpcs-ruleset.xml -p -n; fi;
2833
- if [ $TEST_SUITE == "static" ]; then phpmd src xml tests/static/phpmd-ruleset.xml; fi;
2934
- if [ $TEST_SUITE == "unit" ]; then phpunit --configuration tests/unit/phpunit.xml.dist; fi;
30-
- if [ $TEST_SUITE == "integration" ]; then phpunit --configuration tests/integration/phpunit.xml.dist; fi;
35+
- if [ $TEST_SUITE == "integration" ]; then phpunit --verbose --configuration tests/integration/phpunit.xml.dist; fi;

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"squizlabs/php_codesniffer": " ^3.0",
2222
"phpmd/phpmd": "@stable",
2323
"php-mock/php-mock-phpunit": "^2.0",
24-
"phpunit/php-code-coverage": "^5.2"
24+
"phpunit/php-code-coverage": "^5.2",
25+
"illuminate/config": "^5.5"
2526
},
2627
"bin": [
2728
"m2-ece-build",

src/App/Container.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\MagentoCloud\Process\ConfigDump as ConfigDumpProcess;
1616
use Monolog\Formatter\LineFormatter;
1717
use Monolog\Handler\StreamHandler;
18+
use Monolog\Logger;
1819
use Psr\Container\ContainerInterface;
1920

2021
/**
@@ -196,21 +197,18 @@ public function get($id)
196197
private function createLogger(string $name): \Closure
197198
{
198199
return function () use ($name) {
199-
$formatter = new LineFormatter("[%datetime%] %level_name%: %message% %context% %extra%\n");
200-
$formatter->allowInlineLineBreaks();
201-
$formatter->ignoreEmptyContextAndExtra();
202-
203-
$magentoRoot = $this->get(\Magento\MagentoCloud\Filesystem\DirectoryList::class)
204-
->getMagentoRoot();
200+
$formatter = new LineFormatter(
201+
"[%datetime%] %level_name%: %message% %context% %extra%\n",
202+
null,
203+
true,
204+
true
205+
);
206+
$magentoRoot = $this->get(\Magento\MagentoCloud\Filesystem\DirectoryList::class)->getMagentoRoot();
207+
$logLevel = getenv('LOG_LEVEL') ?: Logger::DEBUG;
205208

206-
return $this->makeWith(\Monolog\Logger::class, [
207-
'name' => $name,
208-
'handlers' => [
209-
(new StreamHandler($magentoRoot . '/var/log/cloud.log'))
210-
->setFormatter($formatter),
211-
(new StreamHandler('php://stdout'))
212-
->setFormatter($formatter),
213-
],
209+
return new Logger($name, [
210+
(new StreamHandler($magentoRoot . '/var/log/cloud.log', $logLevel))->setFormatter($formatter),
211+
(new StreamHandler('php://stdout', $logLevel))->setFormatter($formatter),
214212
]);
215213
};
216214
}

src/Test/Integration/AcceptanceTest.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function setUp()
2929
$this->bootstrap = Bootstrap::create();
3030

3131
shell_exec(sprintf(
32-
"cd %s && rm -rf init",
32+
'cd %s && rm -rf init',
3333
$this->bootstrap->getSandboxDir()
3434
));
3535
}
@@ -40,7 +40,7 @@ protected function setUp()
4040
protected function tearDown()
4141
{
4242
shell_exec(sprintf(
43-
"cd %s && php bin/magento setup:uninstall -n",
43+
'cd %s && php bin/magento setup:uninstall -n',
4444
$this->bootstrap->getSandboxDir()
4545
));
4646
}
@@ -54,7 +54,7 @@ public function testDefault(array $environment)
5454
$application = $this->bootstrap->createApplication($environment);
5555

5656
shell_exec(sprintf(
57-
"cp -f %s %s",
57+
'cp -f %s %s',
5858
$this->bootstrap->getConfigFile('config.php'),
5959
$this->bootstrap->getSandboxDir() . '/app/etc/config.php'
6060
));
@@ -72,6 +72,7 @@ public function testDefault(array $environment)
7272
$commandTester->execute([]);
7373

7474
$this->assertSame(0, $commandTester->getStatusCode());
75+
$this->assertContentPresence($environment);
7576
}
7677

7778
/**
@@ -103,7 +104,7 @@ public function testDeployInBuild(array $environment)
103104
$application = $this->bootstrap->createApplication($environment);
104105

105106
shell_exec(sprintf(
106-
"cp -f %s %s",
107+
'cp -f %s %s',
107108
__DIR__ . '/_files/config_scd_in_build.php',
108109
$this->bootstrap->getSandboxDir() . '/app/etc/config.php'
109110
));
@@ -121,6 +122,7 @@ public function testDeployInBuild(array $environment)
121122
$commandTester->execute([]);
122123

123124
$this->assertSame(0, $commandTester->getStatusCode());
125+
$this->assertContentPresence($environment);
124126
}
125127

126128
/**
@@ -134,4 +136,44 @@ public function deployInBuildDataProvider(): array
134136
],
135137
];
136138
}
139+
140+
/**
141+
* @param array $environment
142+
*/
143+
private function assertContentPresence(array $environment)
144+
{
145+
$config = $this->bootstrap->mergeConfig($environment);
146+
$routes = $config->get('routes');
147+
148+
if ($config->get('skip_front_check') === true || !$routes) {
149+
return;
150+
}
151+
152+
$routes = array_keys($routes);
153+
$defaultRoute = reset($routes);
154+
$pageContent = file_get_contents($defaultRoute);
155+
156+
$this->assertContains(
157+
'Home Page',
158+
$pageContent,
159+
'Check "Home Page" phrase presence'
160+
);
161+
$this->assertContains(
162+
'CMS homepage content goes here.',
163+
$pageContent,
164+
'Check "CMS homepage content goes here." phrase presence'
165+
);
166+
}
167+
168+
/**
169+
* @return array
170+
*/
171+
public function upgradeDataProvider(): array
172+
{
173+
return [
174+
'default configuration' => [
175+
'environment' => [],
176+
],
177+
];
178+
}
137179
}

src/Test/Integration/Bootstrap.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\MagentoCloud\Test\Integration;
77

8+
use Illuminate\Config\Repository;
89
use Magento\MagentoCloud\Application;
910
use Magento\MagentoCloud\Filesystem\DirectoryList;
1011

@@ -41,7 +42,7 @@ public function run()
4142
{
4243
$sandboxDir = $this->getSandboxDir();
4344

44-
if (is_dir($sandboxDir)) {
45+
if (file_exists($sandboxDir . '/composer.lock')) {
4546
return;
4647
}
4748

@@ -57,7 +58,9 @@ public function run()
5758
);
5859
}
5960

60-
mkdir($sandboxDir, 0777, true);
61+
if (!is_dir($sandboxDir)) {
62+
mkdir($sandboxDir, 0777, true);
63+
}
6164

6265
switch ($deployConfig[static::DEPLOY_TYPE]) {
6366
case static::DEPLOY_TYPE_GIT:
@@ -116,15 +119,18 @@ public function run()
116119
*/
117120
public function createApplication(array $environment): Application
118121
{
119-
$environment = array_replace_recursive(
120-
require $this->getConfigFile('environment.php'),
121-
$environment
122-
);
122+
$environment = $this->mergeConfig($environment);
123123

124124
$_ENV = array_replace($_ENV, [
125-
'MAGENTO_CLOUD_VARIABLES' => base64_encode(json_encode($environment['variables'])),
126-
'MAGENTO_CLOUD_RELATIONSHIPS' => base64_encode(json_encode($environment['relationships'])),
127-
'MAGENTO_CLOUD_ROUTES' => base64_encode(json_encode($environment['routes'])),
125+
'MAGENTO_CLOUD_VARIABLES' => base64_encode(json_encode(
126+
$environment->get('variables', [])
127+
)),
128+
'MAGENTO_CLOUD_RELATIONSHIPS' => base64_encode(json_encode(
129+
$environment->get('relationships', [])
130+
)),
131+
'MAGENTO_CLOUD_ROUTES' => base64_encode(json_encode(
132+
$environment->get('routes', [])
133+
)),
128134
]);
129135

130136
$server[\Magento\MagentoCloud\App\Bootstrap::INIT_PARAM_DIRS_CONFIG] = [
@@ -137,6 +143,18 @@ public function createApplication(array $environment): Application
137143
->createApplication();
138144
}
139145

146+
/**
147+
* @param array $environment
148+
* @return Repository
149+
*/
150+
public function mergeConfig(array $environment): Repository
151+
{
152+
return new Repository(array_replace_recursive(
153+
require $this->getConfigFile('environment.php'),
154+
$environment
155+
));
156+
}
157+
140158
/**
141159
* @return string
142160
*/

tests/integration/etc/environment.php.dist

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
$baseUrl = getenv('MAGENTO_HOST_NAME') ?: 'localhost';
4+
35
return [
46
'relationships' => [
57
'database' => [
@@ -13,11 +15,11 @@ return [
1315
],
1416
],
1517
'routes' => [
16-
'http://localhost/' => [
18+
"http://{$baseUrl}/" => [
1719
'type' => 'upstream',
1820
'original_url' => 'http://{default}',
1921
],
20-
'https://localhost/' => [
22+
"https://{$baseUrl}/" => [
2123
'type' => 'upstream',
2224
'original_url' => 'https://{default}',
2325
],
@@ -40,4 +42,5 @@ return [
4042
],
4143
'type' => 'project',
4244
],
45+
'skip_front_check' => false,
4346
];

tests/travis/before_install.sh

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,36 @@ trap '>&2 echo Error: Command \`$BASH_COMMAND\` on line $LINENO failed with exit
88

99
case $TEST_SUITE in
1010
integration)
11+
export SANDBOX_KEY="$SANDBOX_KEY"
12+
export MAGENTO_HOST_NAME="$MAGENTO_HOST_NAME"
13+
1114
mysql -e 'CREATE DATABASE IF NOT EXISTS integration_tests;'
1215

13-
composer config -a -n -g github-oauth.github.com "$GH_TOKEN"
14-
composer config -a -n -g http-basic.repo.magento.com "$REPO_USERNAME" "$REPO_PASSWORD"
15-
composer config -a -n -g http-basic.connect20-qa01.magedevteam.com "$CONNECT20_USERNAME" "$CONNECT20_PASSWORD"
16+
composer config -a -n -g github-oauth.github.com ${GH_TOKEN}
17+
composer config -a -n -g http-basic.repo.magento.com ${REPO_USERNAME} ${REPO_PASSWORD}
18+
composer config -a -n -g http-basic.connect20-qa01.magedevteam.com ${CONNECT20_USERNAME} ${CONNECT20_PASSWORD}
19+
20+
# Install apache
21+
sudo apt-get update
22+
mkdir -p ${TRAVIS_BUILD_DIR}/tests/integration/tmp/sandbox-${SANDBOX_KEY}
23+
sudo apt-get install apache2 libapache2-mod-fastcgi
24+
sudo cp ${TRAVIS_BUILD_DIR}/tests/travis/config/www.conf ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/
25+
26+
# Enable php-fpm
27+
sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
28+
sudo a2enmod rewrite actions fastcgi alias
29+
echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
30+
~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm
31+
32+
# Configure apache virtual hosts
33+
sudo cp -f ${TRAVIS_BUILD_DIR}/tests/travis/config/apache_virtual_host /etc/apache2/sites-available/000-default.conf
34+
sudo sed -e "s?%TRAVIS_BUILD_DIR%?$(pwd)?g" --in-place /etc/apache2/sites-available/000-default.conf
35+
sudo sed -e "s?%MAGENTO_HOST_NAME%?${MAGENTO_HOST_NAME}?g" --in-place /etc/apache2/sites-available/000-default.conf
36+
37+
sudo usermod -a -G www-data travis
38+
sudo usermod -a -G travis www-data
39+
40+
phpenv config-rm xdebug.ini
41+
sudo service apache2 restart
1642
;;
1743
esac
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<VirtualHost *:80>
2+
DocumentRoot %TRAVIS_BUILD_DIR%/tests/integration/tmp/sandbox-travis
3+
ServerName %MAGENTO_HOST_NAME%
4+
5+
<Directory "%TRAVIS_BUILD_DIR%/tests/integration/tmp/sandbox-travis">
6+
Options FollowSymLinks MultiViews ExecCGI
7+
AllowOverride All
8+
Require all granted
9+
</Directory>
10+
11+
<IfModule mod_fastcgi.c>
12+
AddHandler php7-fcgi .php
13+
Action php7-fcgi /php7-fcgi
14+
Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
15+
FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -host 127.0.0.1:9000 -pass-header Authorization
16+
<Directory /usr/lib/cgi-bin>
17+
Require all granted
18+
</Directory>
19+
</IfModule>
20+
</VirtualHost>

tests/travis/config/www.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[www]
2+
3+
user = www-data
4+
group = www-data
5+
6+
listen = 127.0.0.1:9000
7+
8+
pm = dynamic
9+
pm.max_children = 10
10+
pm.start_servers = 4
11+
pm.min_spare_servers = 2
12+
pm.max_spare_servers = 6
13+
14+
chdir = /

0 commit comments

Comments
 (0)