Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexeygeno opened this issue Mar 29, 2023 · 0 comments
Open

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

alexeygeno opened this issue Mar 29, 2023 · 0 comments

Comments

@alexeygeno
Copy link

alexeygeno commented Mar 29, 2023

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

    }

}
@alexeygeno alexeygeno changed the title Mocking time function in one test method affects the other with no such mocking inside Mocking time function in one test method affects the other Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant