Question

I have a class name as a string property. One method (fetchObject) creates new instance of this class. I would like to test this method with PHPUnit and Mockery.

That's the code

class Maker
{
    private $_className = 'Article';

    public function fetchObject( $array )
    {
        return new $this->_className( $array );
    }
}

I tried something like this

$data = array( 'title' => 'Amazing title' );
$article = m::mock( 'Article', array( $data ) );

$maker= new Maker;
$result = $maker->fetchObject( array() );

Test works but I'm not sure if it checks for constructor call and I don't know how to check if param was passed.

Was it helpful?

Solution

Normally you would mock the Maker() class, and ensure it returned from the mock, the object you are looking for.

The other option I have done to test things like this, is to simply test Maker and call the fetchObject() routine with a given class expectation, and then test with an InstanceOf check that the returned object is of the desired class.

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