php-mock/php-mock-phpunit

Mocking time function in one test method affects the other

alexeygeno opened this issue · 0 comments

When invoking testCorrectOtp it's the time stub defined in testNotExpiredOtp runs. Doesn't seem like correct and expected behaviour.

<?php declare(strict_types=1);
namespace AlexGeno\PhoneVerificationTests\Manager;

use PHPUnit\Framework\TestCase;


final class ManagerTest extends TestCase
{

    private \AlexGeno\PhoneVerification\Provider\Stub $providerStub;
    private \AlexGeno\PhoneVerification\Storage\Memory $storageMemory;

    const MIN_OTP_LENGTH = 2;
    const MAX_OTP_LENGTH = 6;


    const MAX_WRONG_OPT_ATTEMPTS = 3;

    use \phpmock\phpunit\PHPMock;

    protected function  setUp():void{
        $this->providerStub =  new \AlexGeno\PhoneVerification\Provider\Stub();
        $this->storageMemory =  new \AlexGeno\PhoneVerification\Storage\Memory();
    }

    public function phoneNumbers(): array
    {
        return [
            'UKR' => ['+380935258272'],
//            'US'  => ['+15417543010'],
//            'UK'  => ['+442077206312']
        ];
    }
    /**
     * @dataProvider phoneNumbers
     */
    public function testCorrectOtp($phoneNumber):void
    {
        $manager = new \AlexGeno\PhoneVerification\Manager($this->storageMemory,  $this->providerStub);
        $otp = $manager->start($phoneNumber);
        $this->assertIsInt($otp);
        $self = $manager->complete($phoneNumber, $otp);
        $this->assertEquals($self, $manager);
    }

    /**
     * @dataProvider phoneNumbers
     */
    public function testNotExpiredOtp($phoneNumber):void
    {
        $manager = new \AlexGeno\PhoneVerification\Manager($this->storageMemory,  $this->providerStub, ['otp_exp_period' => 300]);

        $time = $this->getFunctionMock('AlexGeno\PhoneVerification', "time");
        $time->expects($this->exactly(2))->willReturnOnConsecutiveCalls(0, 299);

        $otp = $manager->start($phoneNumber);
        $this->assertIsInt($otp);  //time call #1

        $self = $manager->complete($phoneNumber, $otp); //time call #2

        $this->assertEquals($self, $manager);

    }

}