John Davidson

PHP Unit / Mockery - Mock static function not working

0 comments
Message:


Hey so I have the below method and when trying to mock the Manager::getConfiguration call, I cannot get it to work. I have tried using a namedMock as well as the unit test decorators to have it run in another thread. It either gives me a class already exists error or actually pulls the manager configuration which I do not want.


public function getBaseTypeManagerConfig() {
$baseTypeManagerConfig = array();

if (!empty($this->dataConfig)
&& !empty($baseType = $this->dataConfig->getBaseType())) {
$baseTypeManagerConfig = Manager::getConfiguration($baseType);
}

return $baseTypeManagerConfig;
}

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
public function testBaseTypeManagerConfig() {
$managerMock = Mockery::namedMock('Manager', 'ManagerStub');
$managerMock->shouldReceive('getConfiguration')->andReturn(self::MOCKED_MANAGER_DECODED_JSON);

$listModule = Mockery::mock('GenericListModule')->makePartial();
$listModule->shouldReceive([
'getBaseType' => 'events',
]);

$config = \SWL\Lib\Generic\ConfigFactory::getConfig('Events_Event');
$listModule->shouldReceive('setDataConfig')->andSet('dataConfig', $config);

$baseTypeManagerConfig = $listModule->getBaseTypeManagerConfig();
$this->assertEquals(array(), $baseTypeManagerConfig);
}

// Attempt at using named mock to replace 'Manager' class with mock
class ManagerStub {
public static function getConfiguration($configType) {
return array();
}
}

Any thoughts on why I can't use this named mock to replace the Manager call? New to unit testing and phpunit/mockery so any other pointers welcome. Thank you! :)



My Answer:
>manager)) {
$managerConfig = Manager::getConfiguration();
$baseTypeManagerConfig = $managerConfig['base_types'];
}

return $baseTypeManagerConfig;
}

To mock the static method `Manager::getConfiguration()` in your unit test, you can use Mockery to create a mock object for the `Manager` class and then mock the `getConfiguration()` method on that mock object.

Here's an example of how you can do this in your unit test:

php
use Mockery\Adapter\Phpunit\MockeryTestCase;

class YourTestClass extends MockeryTestCase
{
public function testGetBaseTypeManagerConfig()
{
$managerMock = Mockery::mock('overload:Manager');
$managerMock->shouldReceive('getConfiguration')->andReturn(['base_types' => ['mocked_base_type']]);

$yourObject = new YourClass();
$result = $yourObject->getBaseTypeManagerConfig();

$this->assertEquals(['mocked_base_type'], $result);
}
}


In this example, we create a mock object for the `Manager` class using `Mockery::mock('overload:Manager')`. We then use the `shouldReceive()` method to mock the `getConfiguration()` method on the mock object and specify the return value that we want.

By using this approach, you should be able to mock the static method `Manager::getConfiguration()` in your unit test without actually calling the real method.

Rate this post

3 of 5 based on 2619 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us