PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 18 ■ TESTING WITH PHPUNIT


}


return null;
}
}


Here is the simple User class:

class User {
private $name;
private $mail;
private $pass;
private $failed;


function __construct( $name, $mail, $pass ) {


if ( strlen( $pass ) < 5 ) {
throw new Exception(
"Password must have 5 or more letters");
}


$this->name = $name;
$this->mail = $mail;
$this->pass = $pass;
}


function getName() {
return $this->name;
}


function getMail() {
return $this->mail;
}


function getPass() {
return $this->pass;
}


function failed( $time ) {
$this->failed = $time;
}
}


Of course, I amend the UserStoreTest class to account for these changes. So code designed to work
with an array like this:


public function testGetUser() {
$this->store->addUser( "bob williams", "[email protected]", "12345" );
$user = $this->store->getUser( "[email protected]" );
$this->assertEquals( $user['mail'], "[email protected]" );
//...


is converted into code designed to work with an object like this:


public function testGetUser() {
$this->store->addUser( "bob williams", "[email protected]", "12345" );
$user = $this->store->getUser( "[email protected]" );

Free download pdf