Testing constructor args in phpunit

Testing constructor args in phpunit

October 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Testing constructor args in phpunit

Testing Constructor Arguments in PHPUnit

Introduction

When testing classes that use third-party libraries like Varien_Http_Client in Magento extensions, it’s crucial to focus on the inputs and outputs of your code rather than its internals. However, sometimes you might need to verify that a specific constructor is called with expected arguments.

In this post, we’ll explore how to test that a constructor was called with the right parameters using PHPUnit, even when the constructor is invoked by PHPUnit::getMock.

The Challenge

Let’s consider the following example from your code:

public function callApi(…)
{
    // Set default factory adapter to socket in case curl isn't installed.
    $client = new Varien_Http_Client($apiUri, array(
        'adapter' => 'Zend_Http_Client_Adapter_Socket',
        'timeout' => $timeout,
    ));
    $client->setRawData($xmlDoc->saveXML())->setEncType('text/xml');
    $response = $client->request($method);
    $results = '';
    if ($response->isSuccessful()) {
        $results = $response->getBody();
    }
    return $results;
}

You want to test that the Varien_Http_Client constructor is called with $apiUri, an array containing 'adapter' => 'Zend_Http_Client_Adapter_Socket' and 'timeout' => $timeout. The problem arises because you’re not creating a new instance of Varien_Http_Client directly. Instead, it’s being created by PHPUnit::getMock.

Mocking the Class

To overcome this challenge, we need to mock the Varien_Http_Client class itself rather than an instance.

  1. First, create a mock object:
$mock = $this->getMockBuilder('Varien_Http_Client')
    ->disableOriginalConstructor()
    ->setMethods(null)
    ->getMock();

In this example, we disable the original constructor and set all methods to be mocked (null). This ensures that the real Varien_Http_Client implementation isn’t used.

  1. Now, you can pass $mock as a parameter to your test subject:
$subject = new YourClass();
$subject->callApi($apiUri, $timeout, …);  // Assuming callApi takes these parameters
  1. Finally, you can verify that the constructor was called with specific arguments using PHPUnit’s expects method and the args parameter:
$this->assertEquals(
    array(
        'adapter' => 'Zend_Http_Client_Adapter_Socket',
        'timeout' => $timeout,
    ),
    $mock->constructorArguments[0]
);

Here, we’re asserting that the first argument passed to the constructor ($mock->constructorArguments[0]) matches our expected array.

Conclusion

Testing constructors in PHPUnit can be a bit tricky when dealing with third-party libraries. The key is to create a mock of the entire class using getMockBuilder, disable the original constructor, and then verify the constructor arguments directly.

Remember that the goal is not to test the internals of third-party code but rather to ensure your application interacts correctly with these external dependencies.

For more advanced testing techniques and best practices in Magento development, be sure to check out our comprehensive guide on Magento Testing at Laravel Company.

API Extensions Errors CLI

Our network