Bu örnek bize PHPUnit'in data providers özelliğini nasıl kullanabileceğimizi gösteriyor. Data providers testlerimizi daha önceden belirlenen verilere ve umulan sonuçlara karşı test etmemizi sağlarlar.


ParameterUtil


namespace Application\Util;

class ParameterUtil
{
const MIN_PAGE = 0;

public function getPage($page)
{
if (!$page || $page < 1) {
$page = self::MIN_PAGE;
}

return (int) $page;
}
}

ParameterUtilTest


namespace tests\Application\Util;

use Application\Util\ParameterUtil;
use PHPUnit\Framework\TestCase;

class ParameterUtilTest extends TestCase
{
/**
* @dataProvider getPageDataProvider
*/
public function testGetPage($page, $expected)
{
$parameterUtil = new ParameterUtil();

$result = $parameterUtil->getPage($page);

$this->assertEquals($expected, $result);
}

public function getPageDataProvider()
{
return [
[-1, 0],
[0, 0],
[1, 1],
[1.5, 1],
['', 0],
[null, 0],
[' ', 0],
['-1', 0],
[5, 5],
];
}
}

Test


$ vendor/bin/phpunit
PHPUnit 5.7.22 by Sebastian Bergmann and contributors.

......... 9 / 9 (100%)

Time: 97 ms, Memory: 4.25MB

OK (9 tests, 1 assertions)