ame180's avatar

Problems setting up automatic tests for Laravel

Hey!

I've been trying to set up automatic tests for Laravel for a while now but nothing seems to work. I wasn't able to find anything useful in the documentation, and for some reason any testing presets don't work for me.

Basically - Unit tests run fine, except for the ones using a database, and no feature tests run for me - always getting a 500. In case of Feature tests I was able to identify with dump() that I get the error Mix manifest does not exist.

EDIT: Fixed Mix manifest does not exist with $this->withoutMix(); on tests. My questions is - how are tests meant to run WITH mix? Do you use npm? Do you upload precompiled assets?

Then there comes the question of - what's up with the database? I don't know the error yet (as the Unit tests that use it are on another branch which is clean so I couldn't check yet), but maybe someone has a clue? I don't have anything fancy - a usual MariaDB set up locally, and on the repo it tries to setup SQLite.

To make it clear, the configuration is pretty much default, official one from GitHub, so I'm kind of confused why such basic things don't seem to work for me.

EDIT: I think I found it! Default GitHub file DOESN'T RUN MIGRATIONS! It's a bit more than that, but things started moving! I'll be sure to update it if I solve it. My god I spent days on this and just as I ask it all CLICKS

Here's my workflow.yml file:

name: Laravel

on:
  push:
    branches:
      - '*'
  pull_request:
    branches:
      - '*'

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    steps:
    - uses: shivammathur/setup-php@b7d1d9c9a92d8d8463ce36d7f60da34d461724f8
      with:
        php-version: '8.0'
    - uses: actions/checkout@v2
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Create Database
      run: |
        mkdir -p database
        touch database/database.sqlite
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: sqlite
        DB_DATABASE: database/database.sqlite
      run: vendor/bin/phpunit

And here's my phpunit.xml: (the only thing I changed is APP_URL, for some reason on local, while in my .env it's localhost/myapp, Feature tests fail with 404 unless I give them localhost instead

<?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>
        <server name="APP_ENV" value="testing"/>
        <server name="APP_URL" value="http://localhost"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

To be honest I'm trying to learn Docker, and set up a docker setup for myself, but I was unable to find any sources capable of teaching me anything really. So if you can't help but happen to know any sources to learn how to set up own Dockerfile and stuff like this (for beginners) then please let me know (I'm not talking about how to USE Docker, I can do that just fine, SETTING IT UP FOR MY OWN REPOSITORY is the killer.)

TO MAKE IT CLEAR ALL THE TESTS PASS JUST FINE LOCALLY, I ONLY HAVE PROBLEMS SETTING THEM UP TO WORK REMOTELY (GitHub, CircleCI etc.)

0 likes
1 reply
ame180's avatar

To solve all my problems, I had to:

  • use $this->withoutMix(); in all of my tests where I didn't want mix
  • add php artisan migrate to my workflow file, for example directly under touch database/database.sqlite
  • depending where you put the migration, it also needs the DB environment variables, so you either copy the env: part and put it right before the other run: as well, or you put it at the top of the file (which makes those variables work for the whole file not just one run), or change .env.example to an .env for testing (which has the DB vars set up)

Please or to participate in this conversation.