bradfeehan / SublimePHPCoverage

A plugin for Sublime Text 2 and 3, which visualises PHP code coverage data in the editor.
MIT License
57 stars 10 forks source link

Some (extra) Usage documentation #26

Open GDmac opened 7 years ago

GDmac commented 7 years ago

It took quite some time to figure out how to unit test a single (current) file. e.g. edit src/Services/MyClass.php build/test -> test/MyClassTest.php

Netbeans uses a custom php file NetBeansSuite and extends PHPUnit_Framework_TestSuite (and then even does a recursive glob() into the test dir to find files :-/ )

Here is a solution with build-variant to quickly run phpunit against a single test file. i've put the tests in a single "test" directory.

in Sublime Text 3, Primary-B runs the last build-variant you have selected. (S3 Build system might change this). Press Primary-Shift-B to choose the variant.

If you do not want to generate coverage for every test run, you can configure coverage setting thru the build-variant or, for the whole project, thru the xml

project.sublime-project

{ 
  "folders": [ 
    { "path": "/Users/gdmac/Sites/projectdir" } 
  ], 
  "settings": { 
    "phpcoverage": { 
      "report_path": "build/logs/clover.xml",
    } 
  }, 
  "build_systems": [ 
    { 
      "name": "PHPUnit", 
      "working_dir": "${project_path}",
      "cmd": [ 
        "/usr/local/php5/bin/php",
        "/usr/local/bin/phpunit",
      ],
      "variants": [
        { 
          "name": "CurrentFile",
          "cmd": [ 
            "/usr/local/php5/bin/php",
            "/usr/local/bin/phpunit",
            // "--coverage-clover",
            // "build/logs/clover.xml",
            "test/${file_base_name}Test.php",
          ]
        },
      ]
    } 
  ] 
}

phpunit.xml

<?xml version="1.0"?>
<phpunit 
  bootstrap="vendor/autoload.php"
  colors="true"
>
  <filter>
    <whitelist addUncoveredFilesFromWhitelist="true">
      <directory suffix=".php">src</directory>
      <exclude>
        <!-- <directory suffix=".php">/path/to/files</directory> -->
        <file>src/Application.php</file>
      </exclude>
    </whitelist>
  </filter>

  <testsuites>
    <testsuite name="TestAll">
      <directory suffix="Test.php">test</directory>
    </testsuite>
  </testsuites>

  <logging>
    <log type="coverage-clover" target="build/logs/clover.xml" />
  </logging>

</phpunit>