Consider a simple class that stores the information of users:
<?php
class UserStore {
private $users = array();
function addUser($name, $mail, $pass) {
if (isset($this->users['mail'])) {
throw new Exception("User {$mail} already in system.\n");
}
if (strlen($pass) < 5) {
throw new Exception("Password must have 5 or more letters.");
}
$this->users[$mail] =
array(
'pass' => $pass,
'mail' => $mail,
'name' => $name,
);
return true;
}
function notifyPasswordFailure($mail) {
if(isset($this->users[$mail])) {
$this->users[$mail]['failed'] = time();
}
}
function getUser($mail) {
return $this->users[$mail];
}
}
And here's our test case to ensure that the class doesn't expect duplicate email ids:
<?php
class UserStoreTest extends PHPUnit_Framework_TestCase {
private $store;
public function setUp() {
$this->store = new UserStore();
}
public function tearDown() {
}
public function testAddUserDuplicate() {
try {
$ret = $this->store->addUser("Bob", "a@b.com", "123456");
$ret = $this->store->addUser("Bill", "a@b.com", "123456");
self::fail('Exception should\'ve been thrown.');
} catch (Exception $e) {
$const = $this->logicalAnd(
$this->logicalNot($this->contains("Bill")),
$this->isType('array')
);
self::AssertThat($this->store->getUser("a@b.com"), $const);
}
}
}
This example is taken from a book. The logic seems simple enough: Once an exception has been thrown on adding duplicate user, we ensure getUser()
doesn't give the second user. So I run this test and get the following error:
There was 1 failure:
1) UserStoreTest::testAddUserDuplicate
Failed asserting that Array (
'pass' => '123456'
'mail' => 'a@b.com'
'name' => 'Bill'
) does not contain 'Bill' and is of type "array".
WTF? The test failed! How? Looking at the test output, I see an array with name Bill. How is this possible? The way I see it, Bill was never added to users because an exception was thrown, then why do we see it in the output? Either I have made a mistake in understanding PHPUnit / this example, or the book's example is wrong. Please help!