From dc30354699a255af41cec311fedd8f2c4eaf8003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 12 Jul 2019 21:44:26 +0200 Subject: [PATCH] Dockerize phpunit (#66) --- .env.example | 3 -- .travis.yml | 16 +++++----- Dockerfile | 25 ++++++++++++++++ composer.json | 4 +-- docker-compose.yml | 45 +++++++++++++++++++++++++++++ src/Traits/BootstrapsTenancy.php | 1 - test | 5 ++++ tests/TenantDatabaseManagerTest.php | 4 +-- tests/TestCase.php | 10 ++++--- 9 files changed, 93 insertions(+), 20 deletions(-) delete mode 100644 .env.example create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100755 test diff --git a/.env.example b/.env.example deleted file mode 100644 index 775d99e8..00000000 --- a/.env.example +++ /dev/null @@ -1,3 +0,0 @@ -DB_DATABASE=travis_tenancy -DB_USERNAME=foo -DB_PASSWORD=bar \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 94a09dd4..ff31db18 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,22 +7,22 @@ php: - '7.2' services: - - mysql - - redis-server + - docker before_install: - - echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini + - docker-compose up -d install: - - travis_retry composer require "laravel/framework:$LARAVEL_VERSION" "orchestra/testbench:$TESTBENCH_VERSION" - - travis_retry composer install --no-interaction + - travis_retry docker-compose exec test composer require --no-interaction "laravel/framework:$LARAVEL_VERSION" "orchestra/testbench:$TESTBENCH_VERSION" before_script: - - mysql -e 'CREATE DATABASE travis_tenancy;' - - export DB_USERNAME=root DB_PASSWORD="" DB_DATABASE=travis_tenancy CODECOV_TOKEN="24382d15-84e7-4a55-bea4-c4df96a24a9b" + - export DB_USERNAME=root DB_PASSWORD="" DB_DATABASE=tenancy CODECOV_TOKEN="24382d15-84e7-4a55-bea4-c4df96a24a9b" - cat vendor/laravel/framework/src/Illuminate/Foundation/Application.php| grep 'const VERSION' -script: vendor/bin/phpunit -v --coverage-clover=coverage.xml +script: docker-compose exec test vendor/bin/phpunit -v --coverage-clover=coverage.xml + +after_script: + - docker-compose down after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..a6727e03 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM ubuntu:18.04 + +LABEL maintainer="Samuel Ć tancl" + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update \ + && apt-get install -y curl zip unzip git sqlite3 \ + php7.2-fpm php7.2-cli \ + php7.2-pgsql php7.2-sqlite3 php7.2-gd \ + php7.2-curl php7.2-memcached \ + php7.2-imap php7.2-mysql php7.2-mbstring \ + php7.2-xml php7.2-zip php7.2-bcmath php7.2-soap \ + php7.2-intl php7.2-readline php7.2-xdebug \ + php-msgpack php-igbinary \ + && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \ + && mkdir /run/php + +RUN apt-get install php7.2-redis + +RUN apt-get -y autoremove \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +WORKDIR /var/www/html \ No newline at end of file diff --git a/composer.json b/composer.json index 8d3aec87..e6fc6818 100644 --- a/composer.json +++ b/composer.json @@ -10,14 +10,14 @@ } ], "require": { - "illuminate/support": "5.7.*||5.8.*", + "illuminate/support": "5.8.*||5.7.*", "webpatser/laravel-uuid": "^3.0", "predis/predis": "^1.1" }, "require-dev": { "vlucas/phpdotenv": "^2.2||^3.3", "psy/psysh": "@stable", - "laravel/framework": "5.7.*||5.8.*", + "laravel/framework": "5.8.*||5.7.*", "orchestra/testbench": "~3.7||~3.8", "league/flysystem-aws-s3-v3": "~1.0" }, diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..3f1575c6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,45 @@ +version: '3' +services: + test: + build: + context: . + networks: + - testnet + depends_on: + - mysql + - postgres + - redis + volumes: + - .:/var/www/html + environment: + DOCKER: 1 + DB_PASSWORD: password + DB_USERNAME: root + DB_DATABASE: main + TENANCY_TEST_REDIS_HOST: redis + TENANCY_TEST_MYSQL_HOST: mysql + TENANCY_TEST_PGSQL_HOST: postgres + stdin_open: true + tty: true + mysql: + image: mysql:5.7 + environment: + MYSQL_ROOT_PASSWORD: password + MYSQL_DATABASE: main + MYSQL_USER: user + MYSQL_PASSWORD: password + networks: + - testnet + postgres: + image: postgres:11 + environment: + POSTGRES_PASSWORD: password + POSTGRES_USER: user + POSTGRES_DB: main + redis: + image: redis:alpine + networks: + - testnet +networks: + testnet: + driver: bridge \ No newline at end of file diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php index f8441b66..1121cc73 100644 --- a/src/Traits/BootstrapsTenancy.php +++ b/src/Traits/BootstrapsTenancy.php @@ -5,7 +5,6 @@ namespace Stancl\Tenancy\Traits; use Stancl\Tenancy\CacheManager; use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; -use Symfony\Component\Debug\Exception\FatalThrowableError; use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException; trait BootstrapsTenancy diff --git a/test b/test new file mode 100755 index 00000000..b22f03b1 --- /dev/null +++ b/test @@ -0,0 +1,5 @@ +#!/bin/bash + +# for development +docker-compose up -d +docker-compose exec test vendor/bin/phpunit "$@" \ No newline at end of file diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 8d25913a..150db99c 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -40,7 +40,7 @@ class TenantDatabaseManagerTest extends TestCase /** @test */ public function mysql_database_can_be_created_and_deleted() { - if (! $this->isTravis()) { + if (! $this->isContainerized()) { $this->markTestSkipped('As to not bloat your MySQL instance with test databases, this test is not run by default.'); } @@ -57,7 +57,7 @@ class TenantDatabaseManagerTest extends TestCase /** @test */ public function mysql_database_can_be_created_and_deleted_using_queued_commands() { - if (! $this->isTravis()) { + if (! $this->isContainerized()) { $this->markTestSkipped('As to not bloat your MySQL instance with test databases, this test is not run by default.'); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 8a81121c..fc250f6d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -53,6 +53,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase $app['config']->set([ 'database.redis.client' => 'phpredis', + 'database.redis.cache.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'), + 'database.redis.default.host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'), 'database.redis.tenancy' => [ 'host' => env('TENANCY_TEST_REDIS_HOST', '127.0.0.1'), 'password' => env('TENANCY_TEST_REDIS_PASSWORD', null), @@ -67,6 +69,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase 'suffix' => '.sqlite', ], 'database.connections.sqlite.database' => ':memory:', + 'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'), + 'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'), 'tenancy.filesystem.disks' => [ 'local', 'public', @@ -114,11 +118,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase return substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length); } - public function isTravis() + public function isContainerized() { - // Multiple, just to make sure. Someone might accidentally - // set one of these environment vars on their computer. - return env('CI') && env('TRAVIS') && env('CONTINUOUS_INTEGRATION'); + return env('CONTINUOUS_INTEGRATION') || env('DOCKER'); } public function assertArrayIsSubset($subset, $array, string $message = ''): void