Created
January 24, 2023 00:46
-
-
Save HectorTorres/a00f7f55fce27df1722f804e1b8df9ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// test/Unit/UserTest.php | |
namespace Tests\Unit; | |
use PHPUnit\Framework\TestCase; | |
class Password { | |
protected $password = ""; | |
function __construct($password = "") | |
{ | |
$this->password = $password; | |
} | |
public function validate(){ | |
if(strlen($this->password) < 8){ | |
throw new InvalidPasswordException(); | |
} | |
if($this->get_strength() == 0){ | |
throw new InvalidPasswordException(); | |
} | |
} | |
public function get_strength(){ | |
$strength = 0; | |
if(preg_match('/[A-Za-z]/', $this->password)){ | |
// Letter found! | |
$strength +=1; | |
} | |
if(preg_match('/[0-9]/', $this->password)){ | |
// Digit found! | |
$strength += 1; | |
} | |
return $strength; | |
} | |
} | |
class PasswordTest extends TestCase{ | |
public function testOneDatatypePasswordGivesScoreOf1(){ | |
$pass = new Password("Foopassword"); | |
$strength = $pass->get_strength(); | |
$this->assertEquals(1, $strength); | |
$pass = new Password("123123123"); | |
$strength = $pass->get_strength(); | |
$this->assertEquals(1, $strength); | |
} | |
public function testTwoDatatypePasswordGivesScoreOf2(){ | |
$pass = new Password("Foobar123"); | |
$strength = $pass->get_strength(); | |
$this->assertEquals(2, $strength); | |
} | |
public function testTwoDatatypePasswordGivesScoreOf0(){ | |
$pass = new Password("-"); | |
$strength = $pass->get_strength(); | |
$this->assertEquals(0, $strength); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment