Skip to content

Rest of a function or method is ignored after an anonymous function #424

@shanept

Description

@shanept

I have a class function 'even' in which I filter an array with an anonymous function. Without testing 'even', I can achieve 100% code coverage on this function, and 0% code coverage on the anonymous function.

complete coverage untested

Obviously, if I test it I can achieve full coverage of the function. The issue seems to be related to how Code Coverage detects the end of the function, were it must be a closing brace and nothing more on a line.

The issue may be bypassed by altering Example.php to look as follows:

$numbers = array_filter($numbers,
function($number) {
    return $number % 2 === 0;
}
);

Here is the contents of my files, so you may duplicate the issue. Please note I have commented out the test in TestExample.php so it will not run. You can expect 100% coverage if the test is run.

Example.php

<?php

class Example
{
    public function even($numbers)
    {
        $numbers = array_filter($numbers, function($number) {
            return $number % 2 === 0;
        });

       return array_merge($numbers);
    }
}

TestExample.php

<?php
include "./Example.php";

class TestExample extends PHPUnit_Framework_TestCase
{
    /*public function testReturnsEvenNumbers()
    {
        $numbers = range(1, 10);
        $expect  = range(2, 10, 2);

        $example = new Example;
        $numbers = $example->even($numbers);

        $this->assertEquals($numbers, $expect);
    }*/
}

phpunit.xml.dist

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit color="true"
        convertErrorsToExceptions="true"
        convertWarningsToExceptions="true"
        convertNoticesToExceptions="true"
        stopOnFailures="false"
        syntaxCheck="false">
    <testsuites>
        <testsuite name="example">
            <file>./TestExample.php</file>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <file>./Example.php</file>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./coverage" />
    </logging>
</phpunit>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions