2

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!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
ankush981
  • 5,159
  • 8
  • 51
  • 96

1 Answers1

4

There is a typo in your addUser method - should be

if (isset($this->users[$mail])) {
    throw new Exception("User {$mail} already in system.\n");
}

BTW, I think it's is a bad test, because you even can't get what is wrong from the first view:)

Nikita U.
  • 3,540
  • 1
  • 28
  • 37
  • Thank you, thank you, thank you! Can't tell you what mental hell I went through to understand what was wrong. What do you recommend for learning PHP unit testing? – ankush981 Nov 01 '15 at 16:02
  • 2
    Not a trivial question:) I never read anything about testing, but many people recommends book "Test Driven Development: By Example" by Kent Beck. There is a list of great literature here http://stackoverflow.com/questions/1711/ (when I'm looking for new reading I go here first, TDD by Example will be next). Also I recommend to post your code to http://codereview.stackexchange.com . There is a big chance that someone will point some mistakes with good explanations and "how to do better". – Nikita U. Nov 01 '15 at 16:41
  • 14
    OMG! Thank you so much for the link and for Codereview. I had no clue SE was running something as cool as that! – ankush981 Nov 01 '15 at 16:44