Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit e185c3d

Browse files
committed
add docker env for test migration diff
1 parent ac4ae33 commit e185c3d

10 files changed

+315
-4
lines changed

.dockerignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/.idea
2+
/.git
3+
/tests/tmp/*
4+
/vendor
5+
/.dockerignore
6+
/.editorconfig
7+
/.env
8+
/.env.dist
9+
/.gitattributes
10+
/.gitignore
11+
/.php_cs.cache
12+
/composer.lock
13+
/Makefile

Makefile

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,27 @@ install:
1818
test:
1919
php $(PHPARGS) vendor/bin/phpunit
2020

21-
.PHONY: all check-style fix-style install test
21+
clean_all:
22+
docker-compose down
23+
sudo rm -rf tests/tmp/*
24+
25+
clean:
26+
sudo rm -rf tests/tmp/app/*
27+
28+
up:
29+
docker-compose up -d
30+
31+
cli:
32+
docker-compose exec php bash
33+
34+
migrate:
35+
docker-compose run --rm php sh -c 'cd /app/tests && ./yii migrate --interactive=0'
36+
37+
installdocker:
38+
docker-compose run --rm php composer install && chmod +x tests/yii
39+
40+
testdocker:
41+
docker-compose run --rm php sh -c 'vendor/bin/phpunit tests/unit'
42+
43+
.PHONY: all check-style fix-style install test clean clean_all up cli installdocker migrate testdocker
2244

docker-compose.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
version: "3.5"
2+
services:
3+
php:
4+
build:
5+
dockerfile: tests/docker/Dockerfile
6+
context: .
7+
volumes:
8+
- ./tests/tmp/.composer:/root/.composer:rw
9+
- .:/app
10+
environment:
11+
- TZ=UTC
12+
- TIMEZONE=UTC
13+
- DB_USER=dbuser
14+
- DB_PASSWORD=dbpass
15+
- IN_DOCKER=docker
16+
depends_on:
17+
- mysql
18+
- postgres
19+
- maria
20+
tty: true
21+
networks:
22+
net: {}
23+
mysql:
24+
image: mysql:5.7
25+
ports:
26+
- '13306:3306'
27+
volumes:
28+
- ./tests/tmp/mysql:/var/lib/mysql:rw
29+
environment:
30+
TZ: UTC
31+
MYSQL_ALLOW_EMPTY_PASSWORD: 1
32+
MYSQL_USER: dbuser
33+
MYSQL_PASSWORD: dbpass
34+
MYSQL_DATABASE: testdb
35+
networks:
36+
net: {}
37+
maria:
38+
image: mariadb
39+
ports:
40+
- '23306:3306'
41+
volumes:
42+
- ./tests/tmp/maria:/var/lib/mysql:rw
43+
environment:
44+
TZ: UTC
45+
MYSQL_ALLOW_EMPTY_PASSWORD: 1
46+
MYSQL_USER: dbuser
47+
MYSQL_PASSWORD: dbpass
48+
MYSQL_DATABASE: testdb
49+
MYSQL_INITDB_SKIP_TZINFO: 1
50+
networks:
51+
net: {}
52+
postgres:
53+
image: postgres:12
54+
ports:
55+
- '15432:5432'
56+
volumes:
57+
- ./tests/tmp/postgres:/var/lib/postgresql/data:rw
58+
environment:
59+
TZ: UTC
60+
PGTZ: UTC
61+
POSTGRES_USER: dbuser
62+
POSTGRES_PASSWORD: dbpass
63+
POSTGRES_DB: testdb
64+
networks:
65+
net: {}
66+
67+
networks:
68+
net: {}

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
convertErrorsToExceptions="true"
55
convertNoticesToExceptions="true"
66
convertWarningsToExceptions="true"
7-
stopOnFailure="false">
7+
stopOnFailure="true">
88
<testsuites>
99
<testsuite name="Test Suite">
10-
<directory suffix="Test.php">./tests</directory>
10+
<directory suffix="Test.php">./tests/unit</directory>
1111
</testsuite>
1212
</testsuites>
1313
<filter>

tests/DbTestCase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace tests;
4+
5+
use Yii;
6+
use yii\di\Container;
7+
use yii\helpers\ArrayHelper;
8+
use yii\helpers\FileHelper;
9+
10+
class DbTestCase extends \PHPUnit\Framework\TestCase
11+
{
12+
13+
protected function prepareTempDir()
14+
{
15+
FileHelper::removeDirectory(__DIR__ . '/tmp/docker_app');
16+
FileHelper::createDirectory(__DIR__ . '/tmp/docker_app');
17+
Yii::setAlias('@app', __DIR__ . '/tmp/docker_app');
18+
}
19+
20+
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
21+
{
22+
$fileConfig = require __DIR__ . '/config/console.php';
23+
new $appClass(ArrayHelper::merge($fileConfig, $config));
24+
}
25+
26+
/**
27+
* Destroys application in Yii::$app by setting it to null.
28+
*/
29+
protected function destroyApplication()
30+
{
31+
Yii::$app = null;
32+
Yii::$container = new Container();
33+
}
34+
}

tests/TestCase.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace tests;
3+
4+
use Yii;
5+
use yii\db\Connection;
6+
use yii\db\Schema;
7+
use yii\helpers\ArrayHelper;
8+
use yii\helpers\FileHelper;
9+
use yii\web\Application;
10+
11+
class TestCase extends \PHPUnit\Framework\TestCase
12+
{
13+
protected function prepareTempDir()
14+
{
15+
FileHelper::removeDirectory(__DIR__ . '/tmp/app');
16+
FileHelper::createDirectory(__DIR__ . '/tmp/app');
17+
Yii::setAlias('@app', __DIR__ . '/tmp/app');
18+
}
19+
20+
protected function mockApplication(?Connection $dbMock, array $extendConfig = []):Application
21+
{
22+
$config = ArrayHelper::merge([
23+
'id' => 'yii2-openapi-test',
24+
'basePath' => __DIR__ . '/tmp/app',
25+
'components'=>[]
26+
], $extendConfig);
27+
if($dbMock !== null){
28+
$config['components']['db'] = $dbMock;
29+
}
30+
return new Application($config);
31+
}
32+
33+
protected function mockRealApplication($config = [], $appClass = '\yii\console\Application')
34+
{
35+
$fileConfig = require __DIR__ . '/config/console.php';
36+
new $appClass(ArrayHelper::merge($fileConfig, $config));
37+
}
38+
39+
protected function mockDbSchemaAsEmpty($driver = 'mysql')
40+
{
41+
$schema = $this->createMock(Schema::class);
42+
$schema->method('getTableSchema')->willReturn(null);
43+
$schema->method('findUniqueIndexes')->willReturn([]);
44+
$schema->method('quoteValue')->willReturnCallback(function($v){ return "'$v'";});
45+
$db = $this->createMock(Connection::class);
46+
$db->method('getSchema')->willReturn($schema);
47+
$db->method('getTableSchema')->willReturn(null);
48+
$db->method('getDriverName')->willReturn($driver);
49+
return $db;
50+
}
51+
}

tests/bootstrap.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
2-
2+
defined('YII_DEBUG') or define('YII_DEBUG', true);
3+
defined('YII_ENV') or define('YII_ENV', 'test');
34
require __DIR__ . '/../vendor/autoload.php';
45
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
56

67
Yii::setAlias('@specs', __DIR__ . '/specs');
8+
Yii::setAlias('@fixtures', __DIR__ . '/fixtures');
9+
Yii::setAlias('@tests', __DIR__);

tests/config/console.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
return [
4+
'id' => 'cebe/yii2-openapi',
5+
'timeZone' => 'UTC',
6+
'basePath' => dirname(__DIR__) . '/tmp/docker_app',
7+
'runtimePath' => dirname(__DIR__) . '/tmp',
8+
'vendorPath' => dirname(__DIR__, 2) . '/vendor',
9+
'aliases' => [
10+
'@bower' => '@vendor/bower-asset',
11+
'@npm' => '@vendor/npm-asset',
12+
],
13+
//'bootstrap'=>['log'],
14+
'controllerMap' => [
15+
'migrate' => [
16+
'class' => \yii\console\controllers\MigrateController::class,
17+
'migrationPath' => dirname(__DIR__).'/migrations',
18+
],
19+
],
20+
'components' => [
21+
'pgsql' => [
22+
'class' => \yii\db\Connection::class,
23+
'dsn' => 'pgsql:host=postgres;dbname=testdb',
24+
'username' => 'dbuser',
25+
'password' => 'dbpass',
26+
'charset' => 'utf8',
27+
'tablePrefix'=>'itt_',
28+
],
29+
'mysql' => [
30+
'class' => \yii\db\Connection::class,
31+
'dsn' => 'mysql:host=mysql;dbname=testdb',
32+
'username' => 'dbuser',
33+
'password' => 'dbpass',
34+
'charset' => 'utf8',
35+
'tablePrefix'=>'itt_',
36+
],
37+
'maria' => [
38+
'class' => \yii\db\Connection::class,
39+
'dsn' => 'mysql:host=maria;dbname=testdb',
40+
'username' => 'dbuser',
41+
'password' => 'dbpass',
42+
'charset' => 'utf8',
43+
'tablePrefix'=>'itt_',
44+
],
45+
'db'=>[
46+
'class' => \yii\db\Connection::class,
47+
'dsn' => 'mysql:host=mysql;dbname=testdb',
48+
'username' => 'dbuser',
49+
'password' => 'dbpass',
50+
'charset' => 'utf8',
51+
'tablePrefix'=>'itt_',
52+
],
53+
],
54+
];

tests/docker/Dockerfile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
FROM php:7.1-cli
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
5+
RUN echo "force-unsafe-io" > /etc/dpkg/dpkg.cfg.d/02apt-speedup && \
6+
echo "Acquire::http {No-Cache=True;};" > /etc/apt/apt.conf.d/no-cache
7+
RUN apt-get update && \
8+
apt-get -y install \
9+
gnupg2 && \
10+
apt-key update && \
11+
apt-get update && \
12+
apt-get install -y --no-install-recommends \
13+
imagemagick \
14+
libmagickwand-dev libmagickcore-dev \
15+
libfreetype6-dev \
16+
libjpeg62-turbo-dev \
17+
libpng-dev \
18+
libwebp-dev \
19+
libicu-dev \
20+
libzip-dev \
21+
libpq-dev \
22+
nano \
23+
git \
24+
unzip\
25+
libxml2-dev \
26+
curl \
27+
libcurl4-openssl-dev \
28+
libssl-dev \
29+
--no-install-recommends && \
30+
apt-get clean && \
31+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
32+
&& docker-php-ext-install \
33+
zip \
34+
curl \
35+
bcmath \
36+
exif \
37+
gd \
38+
iconv \
39+
intl \
40+
opcache \
41+
pdo_mysql \
42+
pdo_pgsql \
43+
mbstring
44+
45+
# Install composer
46+
ENV COMPOSER_ALLOW_SUPERUSER=1 \
47+
PHP_USER_ID=33 \
48+
PHP_ENABLE_XDEBUG=0 \
49+
COMPOSER_HOME=/root/.composer/ \
50+
PATH=/app:/app/vendor/bin:/root/.composer/vendor/bin:$PATH
51+
52+
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
53+
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
54+
# Make sure we're installing what we think we're installing!
55+
&& php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" \
56+
&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer \
57+
&& rm -f /tmp/composer-setup.*
58+
59+
WORKDIR /app

tests/yii

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env php
2+
<?php
3+
require(__DIR__ . '/bootstrap.php');
4+
$config = require(__DIR__ . '/config/console.php');
5+
$app = new yii\console\Application($config);
6+
$exitCode = $app->run();
7+
exit($exitCode);

0 commit comments

Comments
 (0)