sethammons
sethammons

Reputation: 771

PHPunit test not mocking dependencies

I am attempting to use PHPunit to mock out some dependencies, but my mock objects don't seem to be working.

Logger.php

class Logger
{
    function __construct($log_path)
    {
        // make sure file exists, open file handle
    }
    function write($msg)
    {
        // write message to log 
    }
}

MyCurl.php

class MyCurl
{
    function __construct($url)
    {
        // set my default curl options
    }
    function exec()
    {
        // execute curl request and capture output
    }
}

function_x.php

require_once("Logger.php");
require_once("MyCurl.php");
function function_x($params)
{
    // set some stuff up;
    $LoggerObj = new Logger($filepath);
    $CurlObj   = new MyCurl($url);
    // more stuff
    $LoggerObj->write($CurlObj->exec());
    // do stuff
    return $result;
}

function_x_Test.php

require_once('function_x.php');
class functionXTest extends PHPUnit_Framework_TestCase
{
     public function testCleanRun()
     {

         $MockLogger = $this->getMockBuilder('Logger')->disableOriginalConstructor()->setMethods(array('write', '__destruct'))->getMock();
         $MockLogger->expects($this->any())->method('write')->will($this->returnValue(true));

         $MockCurl   = $this->getMockBuilder('MyCurl')->disableOriginalConstructor()->setMethods(array('exec', '__destruct'))->getMock();
         $MockCurl->expects($this->any())->method('exec')->will($this->returnValue('exec returnz'));

         $result = function_x($params);
         // start assertions with function_x results
    }
}

When I run my test, it shows that the original constructor is being called for my Logger class. It does not seem to be using the mocked class. I assumed that if I declared the mock in the test that all calls to the original class would be mocked, thus eliminating those dependencies. Clearly, I am doing something wrong. Can anyone either lend me a helping hand or point me in the right direction? Thanks!

Upvotes: 3

Views: 1311

Answers (1)

jaudette
jaudette

Reputation: 2313

Mocking is replacing an object (see documentation), not a class.

So, to get your example working with mocks, you should inject the objects (dependency injection):

function function_x($params, $logger = null, $curl = null)
{
  //Here, you can set logger and curl if they are null.
  // only do this to make sure legacy code works.
  if(!$logger) {
    $logger = new Logger();
  }
  if(!$curl) {
    $curl = new MYCurl();
  }

  //rest of your code
}

and in your test, you call

require_once('function_x.php');
class functionXTest extends PHPUnit_Framework_TestCase
{
  public function testCleanRun()
  {

    $MockLogger = $this->getMockBuilder('Logger')->disableOriginalConstructor()->setMethods(array('write', '__destruct'))->getMock();
    $MockLogger->expects($this->any())->method('write')->will($this->returnValue(true));

    $MockCurl   = $this->getMockBuilder('MyCurl')->disableOriginalConstructor()->setMethods(array('exec', '__destruct'))->getMock();
    $MockCurl->expects($this->any())->method('exec')->will($this->returnValue('exec returnz'));

    $result = function_x($params, $MockLogger, $MockCurl);
     // start assertions with function_x results
  }
}

Upvotes: 3

Related Questions