Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:13:47+00:00 2026-06-14T17:13:47+00:00

I am attempting to use PHPunit to mock out some dependencies, but my mock

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:13:50+00:00Added an answer on June 14, 2026 at 5:13 pm

    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
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to use the data sources pluigin for rubymine 4.0.3 but I
Attempting to use asp.net mvc's Action Result of File. So it would seem that
I am attempting to use PHP to edit the DOM document tree. However, I
I'm attempting to use Google's Guava ImmutableSet class to create a set of immutable
I am attempting use Java Pathfinder and I have pathfinder working. import gov.nasa.jpf.jvm.Verify; user.java:2:
So I'm attempting to use the MVC pattern for a game I'm working on.
I am attempting to use the Exhibit pattern in a padrino application but to
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
When attempting to use HttpWebRequest to retrieve a page from my dev server, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.