Best Way to Unit Test a Website With Multiple User Types with PHPUnit

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.

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.

Answers


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.


Need Your Help

Resolving associated objects in SL4 RIA

entity-framework silverlight-4.0 lazy-loading rich-internet-application deferred-execution

Having created a standard Silverlight Business Application in VS2010 and set up a model from a SQL Server database, I have various entities and associations, among which AssetGroup and Asset are in...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.