Search code examples
phpunitphpstormcode-coverage

Configuring phpStorm and XML for phpUnit 7 (remote) Coverage


I'm playing for a week with phpUnit.

I'm slowly going forward with documentation at:

At this point I'm at code Coverage. I've managed to generate small test --coverage-html (via console). I want everything to work via phpStorm.

I'm struggling with inclusion paths. I can see errors in console, but these aren't to helpful at all.

This is how my console output looks like:

enter image description here

This is the only place I use this file in

enter image description here

This is how folder structure for tested and displayed (in console) file looks like

|- dir:Boostrap
|- dir:Coverage
|- dir:Database
|- dir:Interfaces
|- dir:Methods
|---- file: BasicCalculations.php (line 3 inclusion)
|- dir:Tests
|---- file:DataDisplayingTest.php (file that I'm testing)
|---- dir:Data Providers
|-------- file:BasicCalculationDataProvider.php (line 4 inclusion)

What I've tried/What I've made so far

This is how my phpunit.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="phpunit.xsd"
         cacheResult="true"
         verbose="true">

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory>/var/www/html/phpUnit</directory>
        </whitelist>
    </filter>

    <php>
         <includePath>/var/www/html/phpUnit</includePath>
    </php>
</phpunit>

I've played around with directory/incluedPath, tried variation like:

 - /var/www/html/phpUnit
 - /var/www/html/phpUnit/
 - .
 - ./
 - <file>pointed/tested/file/here/</file>

I'm working with:

  • phpStorm
  • phpUnit 7.x Remotely
  • php 7.x Remotely
  • xdebug Remotely

To be more clear:

  • what am I doing wrong?
  • how can I deal with inclusions problems?
  • what is causing all this inclusion path problems?

Solution

  • I’ve manage to solve all the problems I had. I’ve used some of the informations provided in the links I’ve pointed above.

    First of all

    Include errors

    PhpUnit xml uses directive includePath, which in my case looked like this:

    <php>
          <includePath>/var/www/html/phpUnit</includePath>
    </php>
    

    Generally at this point the problem is with… existence of includePath in xml file. This attribute changes the inclusion path.

    So lets say that You got project structure like that:

    - dir: Classes
    –- dir: A
    –-- file: A.php class: A (extends B)
    –- dir: B
    –-- file: B.pphp class: B
    -file: index.php
    

    So from the look of file A.php You would need to include B.php like that: ../B/B.php

    Since the working directory is

    /var/www/html/phpUnit/Classes/
    

    But now since You’ve set up inclusion path to:

    var/www/html/phpUnit
    

    File A, tries to load file B from the perspective of phpUnit folder, and it’s kinda looking for file in:

    var/www/html
    

    Not having this directive is not solving the problem as phpUnit seems to use some other default path.

    I’ve solved this problem by changing the way I include files in project:

    Just instead using:

    include_once '../Interfaces/BasicCalculationsInterface.php';
    

    I’ve started doing it like this:

    include_once __DIR__.'/../Interfaces/BasicCalculationsInterface.php';
    

    This way:

    • Single files tests work fine
    • Project itself works fine
    • phpStorm detects methods, classes etc in included file
    • Group tests work well too

    Writing file index.html permission denied

    I’ve stumbled upon this problem as well. This actually is some kind of phpStorm issue, which I don’t know how to solve permanently but I’ve dealt with it for the xml file from which I can run all my tests.

    Basically phpStorm added some default configurations for executed tests.

    In menu go to

    Run/Edit Configurations

    Take a look at field Test Runner options.

    In my case phpStorm added

    --coverage-html /
    

    Everything would be fine, but I use Ubuntu on laptop as remote host, and phpStorm tries this way to create files in / directory for which there is no writing permission. Changing this to some writeable folder or removing this line solved the problem.

    And that’s all, this is how my xml file looks like at this point (just in case someone would like to have something to look at)

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="phpunit.xsd"
             verbose="true">
        <filter>
            <whitelist addUncoveredFilesFromWhitelist="false">
                <directory suffix=".php">/var/www/html/phpUnit</directory>
            </whitelist>
        </filter>
    
        <logging>
            <log type="coverage-clover" target="/var/www/html/phpunit/coverage.xml" lowUpperBound="35" highLowerBound="70"/>
            <log type="coverage-html" target="/var/www/html/phpUnit/phpStormCoverageTest" lowUpperBound="35"
                 highLowerBound="70"/>
        </logging>
    
        <testsuites>
            <testsuite name="allTests">
                <directory suffix="Test.php">/var/www/html/phpUnit/Tests</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    

    Preview of working html/phpStorm coverage enter image description here

    enter image description here