jasonterando / vscode-php-tdd

Microsoft Visual Code Extension to support PHP Test Driven Development
https://marketplace.visualstudio.com/items?itemName=jasonterando.vscode-php-tdd
MIT License
3 stars 2 forks source link

PHP TDD Visual Studio Code Extension

This is a Visual Studio Code extension to assist with TDD in PHP. It includes tools that help to create, run and edit unit tests in your PHP application. By default, this extension uses PHPUnit, but it can be configured to run other unit test frameworks.

Features

Overview Animation

Requirements

Note: As of version 0.1.0, if your PHP does not have JSON and tokenization built into it (i.e. they get loaded as extensions), they will get properly loaded if the enablePHPExtensions configuration property is set to true.

Operation

Initialize

Make sure you have PHP and Composer installed, and that you can run php and composer from a command prompt / shell. If not, use the links in the Requirements section to install.

To initialize the php-tdd, open a Workspace and run the command "PHP TDD: Initialize PHP Unit Test Project", which will do the following (by default configuration). The extension will copy files required for unit tests to the workspace folder. It will then ensure that Composer and PHPUnit are installed in the vendor folder.

Run or Edit a Single Unit Test

Once php-tdd is initialized, you can open a PHP file and move your cursor to a function or class (outside of a function) and run the command "PHP TDD: Run PHP Unit Test". If there is a unit test defined as a "@testFunction" comment line, it will be run, and the results shown in the status line (pass or fail). If there is not a unit test defined, a unit test will be created and brought up to edit. You can later edit the unit test by moving your cursor to the function or class, and run the command "PHP TDD: Edit PHP Unit Test".

All PHP-TDD Commands

About the Base Unit Test Case

The base unit test case implements the following three reflection-based methods to make unit testing a little more convenient.

To use any of these, reference the method using $this in any test class. For example:

    /**
     * @covers MainModule\DemoClass::myPrivateMethod
     **/
    public function testDemoClassMyPrivateMethod() {
        $obj = new DemoClass();
        $this->setProperty($obj, '_privateProperty', 'foo');
        $this->assertEquals('bar', $this->callmethod($obj, 'myPrivateMethod'));
    }

This is provided as a convenience so that you can test any function as you are working on it. If you are categorically opposed to unit testing protected/private functions, don't use it :)

Configuration

Extension Settings

This extension contributes the following settings:

Configuration Variables

The following configuration variables can be inserted into commaands and directory

Docker

If you want to run PHPUnit and Composer from Docker containers, php-tdd includes a Docker harness. You will need to override the following configuration settings in your workspace:

    "php-tdd": {
        "commands": {
            "runUnitTest": "docker-compose -f __TEST_SUBDIRECTORY__/docker-compose.yaml run phpunit --testdox -c __TEST_SUBDIRECTORY__/phpunit.xml --filter __FUNCTION__ __TEST_SUBDIRECTORY__",
            "runAllUnitTests": "docker-compose -f __TEST_SUBDIRECTORY__/docker-compose.yaml run phpunit --testdox -c __TEST_SUBDIRECTORY__/phpunit.xml __TEST_SUBDIRECTORY__",
            "runCodeCoverage": "docker-compose -f __TEST_SUBDIRECTORY__/docker-compose.yaml run phpunit-coverage --testdox -c __TEST_SUBDIRECTORY__/phpunit.coverage.xml __TEST_SUBDIRECTORY__"
        },
        "composer": {
            "commands": {
                "require": "docker-compose -f __TEST_SUBDIRECTORY__/docker-compose.yaml run composer require __FLAGS__ __PACKAGE__",
                "update": "docker-compose -f __TEST_SUBDIRECTORY__/docker-compose.yaml run composer update",
                "dumpAutoload": "docker-compose -f __TEST_SUBDIRECTORY__/docker-compose.yaml run composer dump-autoload"
            }
        }
    }

Release Notes

See (./CHANGELOG.md)[CHANGELOG.md] for release information.

To-Do's

Development

Unit tests can be run by executing npm run unit-test or by loading the project in Visual Code and launch the "Unit Tests" tasks. Note that unit tests are run without launcing Visual Code. If you want to run the tests with Visual Code running, launch the "Integration Tests" task.