A
A
Alexey Verkhovtsev2017-12-20 01:08:13
PHP
Alexey Verkhovtsev, 2017-12-20 01:08:13

Why does code coverage give me 0 phpunit?

Hello everyone, I continue to study PHPUnit and testing in general. I want to know the test coverage.
My code

<?php

namespace app;
class User2
{
    private $password;

    public function getPassword()
    {
        if (isset($this->password)) {
            return $this->password;
        } else {
            return null;
        }
    }

    public function setPassword($login)
    {
        if (is_string($login)) {
            $this->password = $this->cryptPassword($login);
            return true;
        }
        return false;
    }

    protected function cryptPassword($login)
    {
        return md5(uniqid($login));
    }
}

Test code
<?php

namespace tests;

use PHPUnit\Framework\TestCase;
use app\User2;

class User2Test extends TestCase
{
    public function invokeMethod(&$object, $methodName, array $parameters = [])
    {
        $reflection = new \ReflectionClass(get_class($object));
        $method = $reflection->getMethod($methodName);
        $method->setAccessible(true);
        return $method->invokeArgs($object, $parameters);
    }

    public function testGetPassword()
    {
        $obj = new User2();
        $login = 'login';
        $obj->setPassword($login);
        $this->assertEquals(32, strlen($obj->getPassword()));
    }

    public function testCryptPassword()
    {
        $obj = new User2();
        $login = 'login';
        $password = $this->invokeMethod($obj, 'cryptPassword', [$login]);
        $this->assertEquals(32, strlen($password));
    }
}

phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         forceCoversAnnotation="true"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         verbose="true"
        colors="true">
    <testsuite name="default">
        <directory suffix="Test.php">tests</directory>
    </testsuite>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">app</directory>
        </whitelist>
    </filter>
</phpunit>

I execute this command "phpunit --coverage-html tests\coverage" I open User2.php.html and I see
5a398d8d99b81485194025.png
And in the console this is what
5a398db74e8a5556002373.png
Please explain what I'm doing wrong. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2017-12-20
@Sanasol

English in black says that the tests are skipped and why they are skipped
https://phpunit.de/manual/3.7/en/appendixes.annota...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question