Code Monkey home page Code Monkey logo

vscode-phpunit's Introduction

PHP: Unit Test Explorer UI for Visual Studio Code

Run your PHP tests using the Test Explorer UI.

Screenshot

This project is a fork from recca0120/vscode-phpunit ❤️

Features

  • Shows a Test Explorer in the Test view in VS Code's sidebar with all detected tests and suites and their state
  • Adds CodeLenses to your test files for starting and debugging tests
  • Adds Gutter decorations to your test files showing the tests' state
  • Adds line decorations to the source line where a test failed
  • Shows a failed test's log when the test is selected in the explorer
  • Lets you choose test suites or individual tests in the explorer that should be run automatically after each file change
  • Forwards the console output from PHPUnit to a VS Code output channel
  • Run tests into the docker container

Getting started

  • Install the extension
  • Restart VS Code and open the Test view
  • Run your tests using the Run icons in the Test Explorer or the CodeLenses in your test file
  • For running tests on a docker container see the configuration
  • For running phpunit on a remote system or using vagrant see Troubleshooting

Configuration

Custom debugger configuration

Other options

Property Description
testExplorer.codeLens Show a CodeLens above each test or suite for running or debugging the tests
testExplorer.gutterDecoration Show the state of each test in the editor using Gutter Decorations
testExplorer.onStart Retire or reset all test states whenever a test run is started
testExplorer.onReload Retire or reset all test states whenever the test tree is reloaded

Commands

The following commands are available in VS Code's command palette, use the ID to add them to your keyboard shortcuts:

ID Command
test-explorer.reload Reload tests
test-explorer.run-all Run all tests
test-explorer.run-file Run tests in current file
test-explorer.run-test-at-cursor Run the test at the current cursor position
test-explorer.cancel Cancel running tests

Troubleshooting

All tests are appearing twice with "*** duplicate ID ***" found.

You likely have a file sensitive file system. The default value of "phpunit.files": "{test,tests,Test,Tests}/**/*Test.php", should be changed to "phpunit.files": "{test,tests}/**/*Test.php",

I'm using Vagrant / Homestead for remote execution

Your tests will list in the explorer for reading locally, but you need to run your tests remotely using PHP on your vagrant machine.

To do this you need to configure the following settings:

"phpunit.relativeFilePath": true, - This will ensure your files are located correctly for local checks

"phpunit.phpunit": "/FULL_PATH_TO/vendor/bin/phpunit", - your remote path, likely in /var/www/html/

"phpunit.discoverConfigFile": false, if true the extension automatically try to find phpunit.xml or phpunit.xml.dist in you project.

"phpunit.configFile": "/FULL_PATH_TO/phpunit.xml", path to phpunit.xml or phpunit.xml.dist file. If this config is specified the "phpunit.discoverConfigFile" automatically turns to false

"phpunit.php": "/usr/local/bin/vagrant exec php", this is to execute PHP on the remote machine. You'll need to install and configure https://github.com/p0deje/vagrant-exec - this wraps the command like using ssh -C

You can also use the method above to execute on Docker remotely

"phpunit.docker": true, this is to enable run the tests into the docker container

"phpunit.dockerImage": "docker run --rm -v $(pwd):$(pwd) -w=$(pwd) php:8", this is to use the docker container to run the tests

My /usr/local/bin/vagrant isn't found?

Spawn likely can't find vagrant locally. You need to switch to using your regular terminal using something like "phpunit.shell": "/bin/bash", or "phpunit.shell": "/bin/zsh",

Wallaby.js

Wallaby.js

This repository contributors are welcome to use Wallaby.js OSS License to get test results immediately as you type, and see the results in your editor right next to your code.

vscode-phpunit's People

Contributors

amir9480 avatar aphix-mark avatar dependabot[bot] avatar dominicvonk avatar jellisii avatar ngyuki avatar recca0120 avatar renandelmonico avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vscode-phpunit's Issues

"FAILURES!" = Pass

Hey
Thanks for continuing the development of the original extension!

Just wanted to let you know that the issue reported in recca0120#104 is still a problem in this version here.


I run PHPUnit in Docker (I develop on Windows), with these settings:

Workspace settings:

		"phpunit.php": "",
		"phpunit.phpunit": "C:\\node\\npm.cmd",
		"phpunit.args": ["run", "build-runtests", "--"],
		"phpunit.relativeFilePath": true,
		"phpunit.remoteCwd": "/opt/app/",
		"phpunit.files": "myapp/tests/*Test.php",

packages.json:

"build-runtests": "gulp build-debug && docker-compose run --rm --entrypoint 'php' myapp /opt/app/myapp/tests/run_phpunit.php",

My run_phpunit.php basically runs the test(s), prints the output, and returns a proper return code (would be "1" if tests fail).
I confirm that it's definitely returning the right error code.

The problem I have with this is that if there are tests that fail, I'm still seeing a green success icon:

image

After several experiments, I realized that as long as the output contains "FAILURES!", the test will be successful.

run_phpunit.php:

    echo "FAILURES!";
    exit(1);

image

run_phpunit.php:

    echo "SUCCESS!";
    exit(1);

image

I'm using testdox="true" in my phpunit.xml, btw.

Somehow, it appears to me that this extension is looking at the output, rather than relying on the exit code,
and there's a bug in the way it looks at the output.

To fix this, I'm changing my run_phpunit.php so that if there are failures, it will replace "FAILURES!" with something else...

run_phpunit.php:

    $rc = null;
    $cmd_output_ar = array();
    exec($cmd, $cmd_output_ar, $rc);
    $output = implode("\n", $cmd_output_ar);
    echo str_replace('FAILURES!', '', $output);

image

PHP server is crashing

I´m using the phpunit extension and i'm having a few issues.

I can run my tests 2 or 3 times before I get the following error in the picture.

image

After this happens I only can run my tests after restarting vscode.

Below is the following error that is printed in the output.

image

settings.json

{
    "phpunit.phpunit": "/var/www/vendor/bin/phpunit",
    "testExplorer.useNativeTesting": true,
    "phpunit.files": "tests/**/*Test.php",
    "phpunit.remoteCwd": "/var/www",
    "phpunit.relativeFilePath": true,
    "phpunit.php": "/usr/bin/docker compose exec -T accounts2 ",
    "phpunit.shell": "bash",
    "phpunit.args": ["--configuration ./phpunit.xml"]
}

Not working with Docker and WSL2 - Windows 10 - wrong path escaped

I am trying to run this inside Docker in Windows 10 with WSL2, but it is not working at all. The screenshot shows the definitions I am using and that the path escaped by the spawn command shows it wrong.

image

Executing the correct command would work, as show in the next screenshot:

image

plugin inject wrong config file

I have a DDEV project I run in Remote WSL session.

If I configure this plugin to run with the following settings

    "phpunit.relativeFilePath": true,
    "phpunit.phpunit": "/var/www/html/vendor/bin/phpunit",
    "phpunit.docker": true,
    "phpunit.dockerImage": "docker exec -it 833f9",

I get the following error:

spawn docker exec -it 833f9 bash -c "var/www/html/vendor/bin/phpunit -c /home/user13/code/phpunit.xml" ENOENT

The following is successful

  • docker exec -it 833f9 "/var/www/html/vendor/bin/phpunit"

I do not understand why -c /home/user13/code/phpunit.xml is being injected.
This config file is my WSL path. If the command is running in docker, then it should be the dockered path?

I check the actual path:

docker exec -it 833f9 "bash"

$ find ~+ -type f -name phpunit.xml
/var/www/html/phpunit.xml

I can run the following in my terminal:

  • docker exec -it 833f9 bash -c "/var/www/html/vendor/bin/phpunit -c /var/www/html/phpunit.xml"

So I think the correct spawn command should be:

  • spawn docker exec -it 833f9 bash -c "/var/www/html/vendor/bin/phpunit -c /var/www/html/phpunit.xml"
  • spawn docker exec -it 833f9 bash -c "/var/www/html/vendor/bin/phpunit -c /home/user13/code/phpunit.xml"

VSCode 1.66.2
renandelmonico.vscode-php-test-explorer 1.0.1

TypeError: Cannot read properties of undefined (reading 'loc')

Every time I save a change on a file that is not inside the main folder of the workspace (but it's a folder in the folders settings of the workspace), I get this crash and I must restart VSCode to be able to run tests again:

TypeError: Cannot read properties of undefined (reading 'loc')
	at n.swapLocations (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:154736)
	at n.resolvePrecedence (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:155463)
	at a (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:157455)
	at r.read_expr (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:217563)
	at r.read_expr_item (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:219090)
	at r.read_expr (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:216069)
	at r.read_argument_list (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:228199)
	at r.read_function_argument_list (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:227870)
	at r.recursive_variable_chain_scan (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:250227)
	at r.read_variable (c:\Users\myuser\.vscode\extensions\renandelmonico.vscode-php-test-explorer-1.0.2\server\out\server.js:2:248412)
[Info  - 16:24:08] Connection to server got closed. Server will restart.

If you need to know my configs, please let me know.
Thank you!

phpunit.ssh setting not working

Hi !

I used PHPUnit extension to connect to remote machine successfuly. I run tests using "Ctrl+Shift+P -> PHPUnit Test -> myTestCase" as instance. See output below.

PHPUnit 8.5.26
Runtime: PHP 7.2.24-0ubuntu0.18.04.11 with Xdebug 2.6.0
Configuration: /home/sb-0/phpunit.xml
...... 6 / 6 (100%)
Time: 31.95 seconds, Memory: 16.00 MB
OK (6 tests, 7 assertions)

But when I run test using Test Explorer, or Run in top label of test function headers, the output command executed automatically by extension ignores ssh configuration and runs directly "phpunit.php": "/usr/bin/php", command. See output below.

[INFO] {
title: 'PHPUnit LSP',
command: '/usr/bin/php',
arguments: [
'/home/sb-0/www/iats/code/resources/composer/phpunit/phpunit/phpunit',
'-v',
'-c',
'/home/sb-0/phpunit.xml',
'--filter',
'/^.::testBadgeName.$/',
'/home/sb-0/www/iats/code/module/badge/test/BadgeUnitTest.phpunit.php'
]
}

How can we use current phpunit configuration to run tests using PHPUnit Test Explorer extension?
Am I doing something wrong?

Thnaks !!
Great work!!

Not working with Docker and differents paths

In my local project the paths is src/ but in docker it is mounted as /var/www/html/.

    volumes:
      - ./src/:/var/www/html/:rw

When the test is launched it runs as "src/tests/Unit/ExampleTest.php" and shows an error, but it should be "tests/Unit/ExampleTest.php".

$ docker-compose exec container bash -c "vendor/bin/phpunit --configuration ./phpunit.xml --filter /^.*::testThatTrueIsTrue.*$/ src/tests/Unit/ExampleTest.php"

But the correct:

$ docker-compose exec container bash -c "vendor/bin/phpunit --configuration ./phpunit.xml --filter /^.*::testThatTrueIsTrue.*$/ tests/Unit/ExampleTest.php"

Would there be a possibility to add an option with mapped paths? ex:

"phpunit.pathMappings": {
    "${workspaceRoot}/src/":"/var/www/html/"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.