Answer the question
In order to leave comments, you need to log in
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));
}
}
<?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));
}
}
<?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>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question