views:

448

answers:

3

I'm starting to learn how to use PHPUnit to test the website I'm working on. The problem I'm running into is that I have five different user types defined and I need to be able to test every class with the different types. I currently have a user class and I would like to pass this to each function but I can't figure out how to pass this or test the different errors that could come back as being correct or not.

Thanks

Edit: I should have said. I have a user class and I want to pass a different instance of this class to each unit test.

+1  A: 

If you're looking to test the actual UI, you could try using something like Selenium (www.openqa.org). It lets you write the code in PHP (which I'm assuming would work with phpUnit) to drive the browser..

Another approach would be to have a common method that could be called by each test for your different user type. ie, something like 'ValidatePage', which you could then call from TestAdminUser or TestRegularUser and have the method simply perform the same basic validation of what you're expecting..

Peter Bernier
+1  A: 

If your various user classes inherit from a parent user class, then I recommend you use the same inheritance structure for your test case classes.

Consider the following sample classes:

class User
{
    public function commonFunctionality()
    {
        return 'Something';
    }

    public function modifiedFunctionality()
    {
        return 'One Thing';
    }
}

class SpecialUser extends User
{
    public function specialFunctionality()
    {
        return 'Nothing';
    }

    public function modifiedFunctionality()
    {
        return 'Another Thing';
    }
}

You could do the following with your test case classes:

class Test_User extends PHPUnit_Framework_TestCase
{
    public function create()
    {
        return new User();
    }

    public function testCommonFunctionality()
    {
        $user = $this->create();
        $this->assertEquals('Something', $user->commonFunctionality);
    }

    public function testModifiedFunctionality()
    {
        $user = $this->create();
        $this->assertEquals('One Thing', $user->commonFunctionality);
    }
}

class Test_SpecialUser extends Test_User
{
    public function create() {
        return new SpecialUser();
    }

    public function testSpecialFunctionality()
    {
        $user = $this->create();
        $this->assertEquals('Nothing', $user->commonFunctionality);
    }

    public function testModifiedFunctionality()
    {
        $user = $this->create();
        $this->assertEquals('Another Thing', $user->commonFunctionality);
    }
}

Because each test depends on a create method which you can override, and because the test methods are inherited from the parent test class, all tests for the parent class will be run against the child class, unless you override them to change the expected behavior.

This has worked great in my limited experience.

Andrew Culver
+1  A: 

Just make sure you're not running into an anti-pattern here. Maybe you do too much work in the constructor? Or maybe these should be in fact different classes? Tests often give you clues about design of code. Listen to them.

phjr