Summer Sale! All accounts are 50% off this week.

BernardoBF4's avatar

Adding new databse for testing only

I am trying to add a new databse to my project in order to use it for testing, and to have the "official" database used for developers to work on it. I'd like both databases to be accessed within the same PHPMyAdmin, but I do not know how to configure my project for that. I've seen some people on the web using another .env file, something like .env.testing, but I've not been able to go on with all the configuration with docker and php unit.

Does someone know any tutorial for that or is able to help me?

My configs bellow:

.env file

APP_NAME=Biglietto
APP_ENV=local
APP_KEY=base64:LZZnP9ldEbLWLGD6V3LBtdikL9hyxHpoybWlLhBs0j0=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=mysql_biglietto
DB_PORT=3306
DB_DATABASE=biglietto
DB_USERNAME=root
DB_PASSWORD=123456

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

docker-compose.yml

version: "3.3"
services:
  biglietto:
    build:
      context: .
      dockerfile: ./devops/Dockerfile.local
    ports:
      - 80:8000
    volumes:
      - ./:/var/www/html
    networks:
      - biglietto
    tty: true
    container_name: biglietto  
  mysql_biglietto:
    image: mariadb:10.3
    entrypoint: docker-entrypoint.sh --sql-mode='STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
    ports:
      - 3306:3306
    networks:
      - biglietto
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_USER=user
      - MYSQL_PASSWORD=123456
      - MYSQL_DATABASE=biglietto
    container_name: mysql_biglietto
  phpmyadmin_biglietto:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8181:80
    networks:
      - biglietto
    environment:
      PMA_HOST: mysql_biglietto
      UPLOAD_LIMIT: 300M
    container_name: phpmyadmin_biglietto
networks:
  biglietto:
    external:
      name: biglietto_network

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"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
        <!-- <env name="DB_CONNECTION" value="mysql"/> -->
        <!-- <env name="DB_DATABASE" value=":memory:"/> -->
    </php>
</phpunit>
0 likes
9 replies
rodrigo.pedra's avatar

On your phpunit.xml uncomment the database related env variables, and add the ones missing (if needed) to configure your test database.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
        <!-- uncomment these -->
        <env name="DB_CONNECTION" value="mysql"/>
        <env name="DB_DATABASE" value="test_database"/>
        <!-- add DB_USER, DB_PASSWORD, etc. if test database lives in a different database server -->
    </php>
</phpunit>
1 like
BernardoBF4's avatar

@rodrigo.pedra Thanks, that helps a lot. In this case, I'd have to setup the new database manually, but I'd also like to have that new test database to be added whenever the project is instanciated by any person (it's a github repo), thus I think I'd also need to add it to my docker-compose.yml, right?

Sinnbeck's avatar

@BernardoBF4 personally I switched to use lando as it "just works". It automatically adds the database for you so you just need to set phpunit.xml to use it. It's just a wrapper for docker

https://sinnbeck.dev/posts/using-lando-to-run-laravel-in-docker

https://sinnbeck.dev/posts/getting-vite-and-laravel-to-work-with-lando

I ran docker directly for over 2 years before switching and don't want to go back.

In short. For a new user to the team

  1. Install docker
  2. Install lando
  3. Clone repo
  4. Run lando start
  5. Work

Oh and last note. For working on my databases I personally use dbeaver. A database manager you install locally. It's really good and supports many databases (including remote)

2 likes
BernardoBF4's avatar

@Sinnbeck I’ll give a look into that. But for now, as I do not wish to learn this new tool, do you have any ideas on how to do what I asked above?

rodrigo.pedra's avatar

@BernardoBF4

Yes, I guess if you have it on docker file, then it will exists on your dockerized database instance.

As this is a testing database I think it is fine to have its credentials on your version control.

1 like
BernardoBF4's avatar

@rodrigo.pedra I have tried adding a new MySQL container to the .yml, but the two databases didn’t end up in the same PHPMyAdmin

Please or to participate in this conversation.