Laravel 6.2/docker/docker-compose phpunit running in local environment rather than testing

Viewed 511

I've seen others run into this, but I haven't been able to find a solution. My Dockerfile:

FROM php:7.2-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip \
    sudo \
    nginx

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip opcache

# Set working directory
WORKDIR /var/www
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    mkdir -p /home/$user/.config/valet/Log && \
    chown -R www-data:www-data /home/$user && \
    chown -R www-data:www-data /var/www
RUN rm /etc/nginx/sites-enabled/default && \
    pecl install xdebug
# install node
RUN /bin/bash -c "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash && \
    source /root/.bashrc && \
    nvm install v12.18.3"

ENTRYPOINT ["./entrypoint.sh"]

docker-compose.yml

version: '3.4'
services:
  db:
    image: mysql:5.7
    restart: always
    container_name: ufr-mysql-master
    environment:
      MYSQL_DATABASE: workit
      MYSQL_USER: local
      MYSQL_PASSWORD: local
      MYSQL_ROOT_PASSWORD: admin
    ports:
      - "3306:3306"
    volumes:
      - my-db:/var/lib/mysql
  testdb:
    image: mysql:5.7
    restart: always
    container_name: ufr-mysql-test
    environment:
      MYSQL_DATABASE: workit_test
      MYSQL_USER: local
      MYSQL_PASSWORD: local
      MYSQL_ROOT_PASSWORD: admin
    ports:
      - "3307:3306"
    tmpfs: /var/lib/mysql
  app:
    build:
      args:
        user: david
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: workit
    container_name: workit-app
    restart: on-failure
    working_dir: /var/www/
    environment:
      PHP_IDE_CONFIG: "serverName=localhost"
    volumes:
      - ./:/var/www
      - ./workit.conf:/etc/nginx/conf.d/workit.conf
      - ./ext-xdebug.ini:/usr/local/etc/php/conf.d/ext-xdebug.ini
      - ./php-fpm.conf:/usr/local/etc/php-fpm.conf
      - ./log:/var/www/log
    ports:
    - "80:80"
  redis:
    image: redis
    command: ["redis-server", "--appendonly", "yes","--requirepass","Redis.123"]
    volumes:
      - redis-data:/data
    container_name: redis-master
    ports:
      - "6379:6379"
volumes:
  my-db:
  redis-data:

and phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="View">
            <directory suffix="Test.php">./tests/View</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing" force="true"/>
        <env name="CACHE_DRIVER" value="array" force="true"/>
        <env name="SESSION_DRIVER" value="array" force="true"/>
        <env name="QUEUE_DRIVER" value="sync" force="true"/>
        <env name="DB_CONNECTION" value="workit_testing" force="true"/>
    </php>
</phpunit>

When I run docker exec -it workit-app /bin/bash and attempt vendor/bin/phpunit inside that app container, it wipes my local database and config('app.env') clearly dumps out "local" inside the tests. I have tried running vendor/bin/phpunit --configuration phpunit.xml, but it appears that phpunit is already using the phpunit config and the environment is possibly before overridden back to local. I added the force="true" in the phpuni.xml as an experiment and that did not help. I've run php artisan config:cache and php artisan config:cache --env=testing many times and still have not been able to successfully run tests in the testing environment. The runningUnitTests method in vendor/laravel/framework/src/Illuminate/Foundation/Application.php logs $this['env'] as "local" as well. I do have a .env.testing file and here it is for good measure:

APP_KEY=base64:qt79b9nmZiUikL3U98WbT9Z1TnEj2yf8DBaE7zY4GFg=
APP_ENV=testing
BCRYPT_ROUNDS=4
CACHE_DRIVER=array
MAIL_DRIVER=array
QUEUE_CONNECTION=sync
SESSION_DRIVER=array
DB_CONNECTION=mysql
DB_DATABASE=workit_test
DB_HOST=127.0.0.1
DB_PORT=3307
DB_USERNAME=local
DB_PASSWORD=local
SCOUT_DRIVER=null
UNIVERSAL_TEST_PIN_CODE=true
TEST_PHONE_NUMBER='+61111111111, +61666666666, +61900900900'

The "workit_test" container is up and running and "ready for connections". Another point to add is that the Dockerfile and docker-compose.yml were set up by another team who are using PHPStorm as their IDE, whereas I am using VS Code and they are not running into the same issue due to their PHPStorm configuration. I would like to fix my situation without throwing off theirs.

At this point I've sunk a good several hours, so any help or leading questions even would be seriously appreciated.

0 Answers
Related