Skip to content

Commit ca9d0f7

Browse files
author
dmitriy
committed
fixed issue with test environment. Now you can use test environment as test and ci
1 parent 6837e9f commit ca9d0f7

25 files changed

+2066
-115
lines changed

Diff for: .circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- run:
1313
name: Start container and verify it's working
1414
command: |
15-
make start-ci
15+
make start-test
1616
1717
- run:
1818
name: Wait for DB container is running and initialize DB

Diff for: .dockerignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ bootstrap/cache
1111
Dockerfile
1212
_ide_helper.php
1313
docker-compose.yml
14-
docker-compose-ci.yml
14+
docker-compose-test-ci.yml
1515
docker-compose-prod.yml
1616
Makefile

Diff for: .env.local renamed to .env.dev

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
APP_NAME=Laravel
2-
APP_ENV=local
2+
APP_ENV=dev
33
APP_KEY=base64:KgeWah2LwOk5HLjCYuIZjaQQwX59ASqUjCKZMD6H4Ew=
44
APP_DEBUG=true
55
APP_URL=http://localhost

Diff for: .env.prod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
APP_NAME=Laravel
2-
APP_ENV=production
2+
APP_ENV=prod
33
APP_KEY=
44
APP_DEBUG=false
55
APP_URL=http://localhost

Diff for: .env.testing renamed to .env.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
APP_NAME=Laravel
2-
APP_ENV=testing
2+
APP_ENV=test
33
APP_KEY=base64:KgeWah2LwOk5HLjCYuIZjaQQwX59ASqUjCKZMD6H4Ew=
44
APP_DEBUG=false
55
APP_URL=http://localhost

Diff for: .env.example renamed to .env.test-ci

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
# by default test environment using .env.test. So all tests will use the same database. If you need to separate it, just use make env-test-ci command and then run migrations and seed.
12
APP_NAME=Laravel
2-
APP_ENV=local
3-
APP_KEY=
4-
APP_DEBUG=true
3+
APP_ENV=test
4+
APP_KEY=base64:KgeWah2LwOk5HLjCYuIZjaQQwX59ASqUjCKZMD6H4Ew=
5+
APP_DEBUG=false
56
APP_URL=http://localhost
67

78
LOG_CHANNEL=stack
89

910
DB_CONNECTION=mysql
10-
DB_HOST=127.0.0.1
11+
DB_HOST=mysql
1112
DB_PORT=3306
12-
DB_DATABASE=homestead
13-
DB_USERNAME=homestead
13+
DB_DATABASE=laravel
14+
DB_USERNAME=root
1415
DB_PASSWORD=secret
1516

1617
BROADCAST_DRIVER=log

Diff for: Dockerfile

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ FROM php:7.3-fpm
33
# set main params
44
ARG BUILD_ARGUMENT_DEBUG_ENABLED=false
55
ENV DEBUG_ENABLED=$BUILD_ARGUMENT_DEBUG_ENABLED
6-
ARG BUILD_ARGUMENT_APP_ENV=development
7-
ENV APP_ENV=$BUILD_ARGUMENT_APP_ENV
8-
ARG BUILD_ARGUMENT_ENV_FILE=.env.local
9-
ENV ENV_FILE=$BUILD_ARGUMENT_ENV_FILE
6+
ARG BUILD_ARGUMENT_ENV=dev
7+
ENV ENV=$BUILD_ARGUMENT_ENV
108
ENV APP_HOME /var/www/html
119

1210
# check environment
13-
RUN if [ "$BUILD_ARGUMENT_APP_ENV" = "default" ]; then echo "Set BUILD_ARGUMENT_APP_ENV in docker build-args like --build-arg BUILD_ARGUMENT_APP_ENV=development" && exit 2; \
14-
elif [ "$BUILD_ARGUMENT_APP_ENV" = "development" ]; then echo "Building development environment."; \
15-
elif [ "$BUILD_ARGUMENT_APP_ENV" = "production" ]; then echo "Building production environment."; \
16-
else echo "Set correct BUILD_ARGUMENT_APP_ENV in docker build-args like --build-arg BUILD_ARGUMENT_APP_ENV=development. Available choices are development,production." && exit 2; \
11+
RUN if [ "$BUILD_ARGUMENT_ENV" = "default" ]; then echo "Set BUILD_ARGUMENT_ENV in docker build-args like --build-arg BUILD_ARGUMENT_ENV=dev" && exit 2; \
12+
elif [ "$BUILD_ARGUMENT_ENV" = "dev" ]; then echo "Building development environment."; \
13+
elif [ "$BUILD_ARGUMENT_ENV" = "test" ]; then echo "Building test environment."; \
14+
elif [ "$BUILD_ARGUMENT_ENV" = "prod" ]; then echo "Building production environment."; \
15+
else echo "Set correct BUILD_ARGUMENT_ENV in docker build-args like --build-arg BUILD_ARGUMENT_ENV=dev. Available choices are dev,test,prod." && exit 2; \
1716
fi
1817

1918
# install all the dependencies and enable PHP modules
@@ -47,13 +46,13 @@ RUN mkdir -p $APP_HOME/public
4746
RUN chown -R www-data:www-data $APP_HOME
4847

4948
# put php config for Laravel
50-
COPY ./docker/$BUILD_ARGUMENT_APP_ENV/www.conf /usr/local/etc/php-fpm.d/www.conf
51-
COPY ./docker/$BUILD_ARGUMENT_APP_ENV/php.ini /usr/local/etc/php/php.ini
49+
COPY ./docker/$BUILD_ARGUMENT_ENV/www.conf /usr/local/etc/php-fpm.d/www.conf
50+
COPY ./docker/$BUILD_ARGUMENT_ENV/php.ini /usr/local/etc/php/php.ini
5251

5352
# install Xdebug in case development environment
5453
COPY ./docker/other/do_we_need_xdebug.sh /tmp/
55-
COPY ./docker/development/xdebug.ini /tmp/
56-
RUN chmod u+x /tmp/do_we_need_xdebug.sh && /tmp/do_we_need_xdebug.sh $BUILD_ARGUMENT_APP_ENV
54+
COPY ./docker/dev/xdebug.ini /tmp/
55+
RUN chmod u+x /tmp/do_we_need_xdebug.sh && /tmp/do_we_need_xdebug.sh
5756

5857
# install composer
5958
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer
@@ -74,10 +73,10 @@ USER www-data
7473

7574
# copy source files and config file
7675
COPY --chown=www-data:www-data . $APP_HOME/
77-
COPY --chown=www-data:www-data $ENV_FILE $APP_HOME/.env
76+
COPY --chown=www-data:www-data .env.$ENV $APP_HOME/.env
7877

7978
# install all PHP dependencies
80-
RUN if [ "$BUILD_ARGUMENT_APP_ENV" = "development" ]; then composer install --no-interaction --no-progress; \
79+
RUN if [ "$BUILD_ARGUMENT_ENV" = "dev" ] || [ "$BUILD_ARGUMENT_ENV" = "test" ]; then composer install --no-interaction --no-progress; \
8180
else composer install --no-interaction --no-progress --no-dev; \
8281
fi
8382

Diff for: Makefile

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
1-
ENV=local
21
dir=${CURDIR}
32
project=-p laravel
43
service=laravel:latest
54

65
start:
76
@docker-compose -f docker-compose.yml $(project) up -d
87

9-
start-ci:
10-
@docker-compose -f docker-compose-ci.yml $(project) up -d
8+
start-test:
9+
@docker-compose -f docker-compose-test-ci.yml $(project) up -d
1110

1211
start-prod:
1312
@docker-compose -f docker-compose-prod.yml $(project) up -d
1413

1514
stop:
1615
@docker-compose -f docker-compose.yml $(project) down
1716

18-
stop-ci:
19-
@docker-compose -f docker-compose-ci.yml $(project) down
17+
stop-test:
18+
@docker-compose -f docker-compose-test-ci.yml $(project) down
2019

2120
stop-prod:
2221
@docker-compose -f docker-compose-prod.yml $(project) down
2322

2423
restart: stop start
25-
restart-ci: stop-ci start-ci
24+
restart-test: stop-test start-test
2625
restart-prod: stop-prod start-prod
2726

28-
env-local:
29-
cp ./.env.local ./.env
30-
31-
env-prod:
32-
cp ./.env.prod ./.env
27+
env-test-ci:
28+
@make exec cmd="cp ./.env.test-ci ./.env"
3329

3430
ssh:
3531
@docker-compose $(project) exec laravel bash
@@ -41,11 +37,10 @@ exec:
4137
@docker-compose $(project) exec laravel $$cmd
4238

4339
clean:
44-
rm -rf $(dir)/reports
40+
rm -rf $(dir)/reports/*
4541

4642
prepare:
47-
mkdir $(dir)/reports
48-
mkdir $(dir)/reports/coverage
43+
mkdir -p $(dir)/reports/coverage
4944

5045
wait-for-db:
5146
@make exec cmd="php artisan db:wait"
@@ -65,11 +60,14 @@ info:
6560

6661
drop-migrate:
6762
@make exec cmd="php artisan migrate:fresh"
68-
@make exec cmd="php artisan migrate:fresh --env=testing"
63+
@make exec cmd="php artisan migrate:fresh --env=test"
64+
65+
migrate-prod:
66+
@make exec cmd="php artisan migrate --force"
6967

7068
migrate:
7169
@make exec cmd="php artisan migrate --force"
72-
@make exec cmd="php artisan migrate --force --env=testing"
70+
@make exec cmd="php artisan migrate --force --env=test"
7371

7472
seed:
7573
@make exec cmd="php artisan db:seed --force"

Diff for: app/Console/Commands/DbWaitDatabase.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace App\Console\Commands;
35

46
use DB;
@@ -7,6 +9,11 @@
79

810
class DbWaitDatabase extends Command
911
{
12+
/**
13+
* Wait sleep time for db connection in seconds
14+
*
15+
* @var int
16+
*/
1017
const WAIT_SLEEP_TIME = 2;
1118

1219
/**
@@ -30,18 +37,26 @@ class DbWaitDatabase extends Command
3037
*
3138
* @return integer
3239
*/
33-
public function handle(DB $db)
40+
public function handle(DB $db): int
3441
{
3542
for ($i = 0; $i < 60; $i += self::WAIT_SLEEP_TIME) {
43+
3644
try {
3745
$db::select('SHOW TABLES');
46+
$this->info('Connection to the database is ok!');
47+
3848
return 0;
3949
} catch (QueryException $e) {
50+
$this->comment('Trying to connect to the database seconds:' . $i);
4051
sleep(self::WAIT_SLEEP_TIME);
52+
4153
continue;
4254
}
55+
4356
}
4457

58+
$this->error('Can not connect to the database');
59+
4560
return 1;
4661
}
4762
}

0 commit comments

Comments
 (0)