I have the following class:
namespace Utils\Random;
class RandomHelper
{
const LUCKY_NUMBER=3;
public static function lucky()
{
return rand(0,6)==self::LUCKY_NUMBER;
}
}
And I want to test this class using a unit test:
namespace Tests\Random;
use PHPUnit\Framework\TestCase;
class RandomHelperTest extends TestCase
{
public function testLucky()
{
// Mock rand here
// Here I want the rand return a value that is not 3
}
public function testLuckyFails()
{
// Mock rand here
// Here I want the rand return a value that is not 3
}
}
But in order my test to be a Unit test I want to mock the php standart function rand
in order to be able to have a constant result in my test.
As you can see I have conflicting needs, therefore the solution seems not to be ok with me. On one test I want to check when ther method lucky
teturns true and on the other hand I want to be able when the function lucky will return false.
So do you have any idea hot to do that?