Skip to content

Instantly share code, notes, and snippets.

@tonysm
Created March 24, 2020 14:39
Show Gist options
  • Save tonysm/6c5b04c7aeab6c486a78f565595aa47a to your computer and use it in GitHub Desktop.
Save tonysm/6c5b04c7aeab6c486a78f565595aa47a to your computer and use it in GitHub Desktop.
Property based testing in PHP with DataProviders, Generators, and Faker
<?php
namespace Tests\Unit;
use Faker\Factory;
use PHPUnit\Framework\TestCase;
class PropertyBasedTest extends TestCase
{
public function propertyBaseData()
{
$limit = 100;
$faker = Factory::create();
for ($i = 0; $i < $limit; ++$i) {
$a = $faker->randomNumber();
$b = $faker->randomNumber();
yield sprintf('Dividing %d by %d', $a, $b) => [$a, $b];
}
}
/**
* @dataProvider propertyBaseData
*/
public function testPropertyBased($a, $b)
{
$this->expectNotToPerformAssertions();
$calculator = new DummyCalculator();
$calculator->divide($a, $b);
}
}
class DummyCalculator
{
public function divide(int $a, int $b)
{
return $a / $b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment