public function testIsLoginLimitReached() { $maxLoginAttempts = 3; $user = $this->getMockBuilder('\\pmill\\Auth\\Interfaces\\AuthUser')->getMock(); $user->method('getAuthId')->willReturn(1); $user->method('getAuthPassword')->willReturn($this->correctHashedPassword); $auth = new \pmill\Auth\Authenticate(); $auth->setMaxAttempts($maxLoginAttempts); $auth->resetLoginAttempts(); for ($i = 1; $i <= $maxLoginAttempts + 1; $i++) { try { $auth->login($user, 'incorrect-password'); } catch (\pmill\Auth\Exceptions\PasswordException $e) { if ($i <= $maxLoginAttempts) { $this->assertEquals($i, $auth->getLoginAttempts()); $this->assertFalse($auth->isLoginLimitReached()); } else { $this->assertEquals($maxLoginAttempts + 1, $auth->getLoginAttempts()); $this->assertTrue($auth->isLoginLimitReached()); } } } }
<?php require_once '../vendor/autoload.php'; require_once 'PasswordUser.php'; $output = []; $user = new examples\PasswordUser(); $user->setId(1); $user->setUsername('username'); $user->setPassword('hunter2'); $auth = new \pmill\Auth\Authenticate(); $auth->setMaxAttempts(3); /** * Attempt login with correct password */ $auth->login($user, 'hunter2'); $output[] = 'successful login'; $auth->logout(); /** * Attempt login with incorrect password */ try { $auth->login($user, 'incorrect-password'); } catch (\pmill\Auth\Exceptions\PasswordException $e) { $output[] = 'login failed, incorrect password'; } /** * Expected output: * successful login * login failed, incorrect password */ print_r($output);