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 7566835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:30:47+00:00 2026-05-30T14:30:47+00:00

I’m trying to test some of my php code but fail with mocking objects.

  • 0

I’m trying to test some of my php code but fail with mocking objects. I want to mock a function which does a curl request since it shouldn’t do the request every time/test. Mocking the curl function itself, works. Requiring it for another class and mocking it there, fails.

Here are the files:

helper.curl.php:

  class Helper_Curl {

    /*
    ** __construct($callDomain) - construct new curl helper using set calldomain
    */
    public function __construct($callDomain) {
      $this->callDomain = $callDomain;
    }

    /*
    ** sendPostArrayToApi($Array) - HTTP POST request to server
    */
    public function sendPostArrayToApi($Array) {

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $this->callDomain);
      // [... curl Settings ...]
      $return = curl_exec($ch);
      curl_close($ch);
      return $return;
    }     
  }

class.request.php

  require_once('helper.curl.php');

  class RequestClass {

    function _set($data, $set_type='edit'){

      // [... some code ...]

      $curl = new Helper_Curl('http://example.com');
      $xml = $curl->sendPostArrayToApi($InfoArray);

      if($xml == "") {
        return false;
      }
      // [... some more code ...]
    }     
  }

And finally the test:

  class ClassRequestTest extends PHPUnit_Framework_TestCase {

    public function setUp() {
      require_once('class.request.php');
    }

    public function testSetRefund() {

      $xml = '<?xml version="1.0" encoding="UTF-8"?>
              <response>
                <info-id>12345</info-id>
                <transmit-id>315</transmit-id>
              </response>';

      $stubCurl = $this->getMock('Helper_Curl');

      $stubCurl->expects($this->once())
               ->method('sendPostArrayToApi')
               ->will($this->returnValue($xml));

      $this->object->_set($DataArray);
    }

    protected function tearDown() {
      unset($this->object);
    }
  }

The sendPostArrayToApi function from the class Helper_Curl should be mocked and return the given xml data for the _set function to go on with. How do I attach $stubCurl to $this->object? Is it possible anyhow?

Thanks for your time.

  • 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-05-30T14:30:48+00:00Added an answer on May 30, 2026 at 2:30 pm

    As I understand it you need to use mocking like this in conjunction with auto-loading. Since you have a “hard” dependency on your cURL helper (i.e. a “require_once” in your class.request file) when it gets to that call in the code PHP will only see the real cURL class and will use that. If you use auto-loading, then PHP can intercept the call to autoload that class and substitute the mock.

    Otherwise (and this may be a better solution), you should look at some kind of IoC solution whereby the dependency on the cURL class is not hard-coded in your request class (i.e. you don’t instantiate the cURL object directly in the request class). There are various ways to achieve this. DI or perhaps more specifically Dependency Injection Containers are the current trend in PHP. You might also want to look at a Service Locator instead. There’s a good article by Martin Fowler which describes all these patterns and the various pros and cons. With either of these patterns, you can much more easily substitute test objects (mocks or stubs etc) into the code under test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.