简体   繁体   中英

Howto run better phpunit on a docker container from VSCode

I am trying to run php unit tests on a docker container from vs code using the better phpunit extension, but I cannot get it to work.

what I have so far:-

docker-compose.yml:-

version: '3.1'

services:
    php:
        build:
            context: .
            dockerfile: .docker/Dockerfile
        image: laraboard
        ports:
            - 8000:80
        restart: always
        volumes:
            - .:/var/www/html
        networks:
            - laraboard
    mysql:
        image: mysql:8.0
        volumes:
            - db_data:/var/lib/mysql
        restart: always
        ports:
            - 3306:3306
        environment:
            MYSQL_DATABASE: laraboard
            MYSQL_USER: root
            MYSQL_PASSWORD: password
            MYSQL_ROOT_PASSWORD: password
        networks:
            - laraboard
    phpmyadmin:
        depends_on:
            - mysql
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
            - 8001:80
        environment:
            PMA_HOST: mysql
            MYSQL_ROOT_PASSWORD: password 
        networks:
            - laraboard
networks:
    laraboard:
volumes:
    db_data:

settings.json:-

"better-phpunit.docker.enable": true,
    "better-phpunit.docker.command": "docker exec laraboard_php_1",
    "better-phpunit.docker.paths": {
        "c:/Users/Chris/Web/laraboard": "/var/www/html"
    }

ThreadTest.php:-

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ThreadsTest extends TestCase
{
    /** @test */
    public function a_user_can_browse_threads()
    {
        $response = $this->get('/threads');

        $response->assertStatus(200);
    }
}

with this setup I get error:-

OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec format error": unknown The terminal process "C:\Windows\System32\cmd.exe /d /c docker exec laraboard_php_1 /var/www/html/vendor/bin/phpunit.bat /var/www/html/tests/Feature/ThreadsTest.php --filter '^.*::a_user_can_browse_threads'" terminated with exit code: 126.

Where am I going wrong?

@Florian Engelhardt partyl answered my question, which lead me to finding a workaround, I have accepted his answer but would still like to extend it:-

I have found a workaround for this, but still feel that there must be a better way as it still has problems:-

The first problem is that I was using the container name instead of the service name in the docker.command and I needed to enter bash:

"better-phpunit.docker.command": "docker exec php bash",

I then had to change the phpunit binary as @Florian Engelhardt suggested:-

"better-phpunit.phpunitBinary": "${workspaceFolderBasename}/vendor/bin/phpunit",

This now stopped giving errors and displayed the phpunit help, so I knew I was getting somewhere.

I then had to change the suite commands (I don't know why but this fixed a single test run aswell):-

"better-phpunit.suiteSuffix": "/var/www/${workspaceFolderBasename}/tests",

The above simply tells phpunit to test everything in the laravel tests directory, I would of liked it to use the phpunit.xml config file but cannot get it to work, I have tried "/var/www/${workspaceFolderBasename}" as the docs say if no config is found it will automatically pull in the.xml but it didn't and I have tried "/var/www/${workspaceFolderBasename}/phpunit.xml" but this just tried to test the.xml. Anyway the above workaround works, maybe i'll add a new question for the phpunit.xml

Once I had the tests running I noticed that the colours were no longer working so I added:-

"better-phpunit.commandSuffix": "--colors=auto"

My settings.json now looks like:-

"better-phpunit.docker.enable": true,
    "better-phpunit.docker.command": "docker-compose exec workspace bash",
    "better-phpunit.docker.paths": {
        "${workspaceFolder}": "/var/www/${workspaceFolderBasename}"
    }, 
    "better-phpunit.phpunitBinary": "${workspaceFolderBasename}/vendor/bin/phpunit",
    "better-phpunit.suiteSuffix": "/var/www/${workspaceFolderBasename}/tests",
    "better-phpunit.commandSuffix": "--colors=auto"

Now onto the problems, while the tests run, so far I have found that any tests that are testing a form with a POST request fail due to CSRF token mismatch, where they do not fail if I run phpunit manually. I still need a better solution but for now the above is working for my needs, I simply use it as a quick way to run single tests and then test the suite manually.

The problem here might be, that the path to the phpunit executable is wrong. It should be /var/www/html/vendor/bin/phpunit instead of /var/www/html/vendor/bin/phpunit.bat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM