Question

So I was writing my tests for PHPUnit when a test started exploding all over the place. Its main reason was that Config::get() was throwing - undefined get() for class Core.class.php

This class is called in the test as such:

class CoreTest extends PHPUnit_Framework_TestCase {

    protected $object;

    protected function setUp() {
        $this->object = new Core;
    }

    // .. other tests
}

So I went and investigated the class and I saw that the construct has the following:

public function __construct() {        
    $this->absUrl  = Config::get(Config::ABS_URL);
    $this->baseDir = Config::get(Config::BASE_DIR);
    $this->session = new Session('core');
    return $this;
}

is there a way I can stub these out? or deal with them in a way that the test does not explode?

I was reading This information on stubbing static methods but I am not sure how to apply it here. Thanks.

Was it helpful?

Solution

You would have to mock the class, and not execute the constructor, but rather, build in the values you need and then use the mock in your tests.

Do you have a sample of what you have tried? You likely need to modify the Core() class to support dependency injection (the Config object and the Session object).

$stub = $this->getMock('Core');

// Configure the stub.
$stub->expects($this->any())
     ->method('CreateSession')
     ->will($this->returnValue('foo'));

$this->assertEquals('foo', $stub->CreateSession());

In your example code though, you will likely need to modify the Core() class to accept the Session and Config objects to be passed (either via the constructor, or via a Set dependency) and some modifications to the Session class as well.

class Core
{
    private $SessionObject;
    private $ConfigObject;
    public function __construct(Config $Config, Session $Session)           // Constructor Dependency
    {
        $this->ConfigObject = $Config;
        $this->absUrl  = $this->ConfigObject::get(Config::ABS_URL);
        $this->baseDir = $this->ConfigObject::get(Config::BASE_DIR);
        $this->session = $Session;
        $this->session->SetSessionType('core');
        return $this;
    }
}

or

class Core
{
    private $SessionObject;
    private $ConfigObject;
    public function __construct()           
    {
    }

    // Set Dependencies
    public function SetConfigObject(Config $Config)
    {
        $this->ConfigObject = $Config;
    }

    public function SetSessionObject(Session $Session)
    {
        $this->SessionObject = $Session;
    }

    public function BuildObject($SessionType)
    {
        $this->absUrl  = $this->ConfigObject::get(Config::ABS_URL);
        $this->baseDir = $this->ConfigObject::get(Config::BASE_DIR);
        $this->session->SetSessionType($SessionType);
    }
}

Now your production code will pass the Config and Session objects properly, and you then use Mock objects in your tests to pass objects with the states that you need, to return hard set values when calling the get method on the object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top