Skip to content

Mocking time function in one test method affects the other #58

Description

@alexeygeno

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);

    }

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions