Created
March 24, 2020 14:39
-
-
Save tonysm/6c5b04c7aeab6c486a78f565595aa47a to your computer and use it in GitHub Desktop.
Property based testing in PHP with DataProviders, Generators, and Faker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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