Skip to content

Instantly share code, notes, and snippets.

@emeraldinspirations
Last active August 15, 2017 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emeraldinspirations/604785809ad96ca387d83a32c7eb86fd to your computer and use it in GitHub Desktop.
Save emeraldinspirations/604785809ad96ca387d83a32c7eb86fd to your computer and use it in GitHub Desktop.
Is random in test always bad?
<?php
class Demo
{
protected $Property;
public function getProperty() : int
{
return $this->Property;
}
public function setProperty(int $Value)
{
if ($Value == 1) {
$this->Property = 1;
} elseif ($Value == 2) {
$this->Property = 2;
} else {
$this->Property = 3;
}
}
}
class DemoTest extends TestCase
{
public function setUp()
{
$this->demo = new Demo();
}
/**
* @expectedException \DomainException
* @expectedExceptionCode 1502275279
*/
public function testInvalidProperty()
{
$this->demo->setProperty(null);
}
public function dataProperty()
{
return [
[1, 1],
[2, 2],
[3, 3],
];
}
/**
* Verify setProperty stores ANY valid integer in the protected Property variable
*
* @param mixed $Set The value passed to setProperty
* @param mixed $Expected The expected value
*
* @dataProvider dataProperty
*
* @return void
*/
public function testValidProperty($Set, $Expected)
{
$this->demo->setProperty($Set);
$this->assertProperty($Expected, $this->demo->getProperty());
}
/**
* @group fuzzTest
*/
public function testRandomProperty()
{
// This will find bug
for ($i = 1; $i <= 10; $i++) {
$RandomInteger = rand(PHP_INT_MIN, PHP_INT_MAX);
$this->testValidProperty($RandomInteger, $RandomInteger);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment