Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@HectorTorres
Created January 24, 2023 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HectorTorres/a00f7f55fce27df1722f804e1b8df9ca to your computer and use it in GitHub Desktop.
Save HectorTorres/a00f7f55fce27df1722f804e1b8df9ca to your computer and use it in GitHub Desktop.
<?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