0

I've got this line in my unit testing files:

$this->object->drupal->shouldReceive('drupalSetMessage')->once();

but I've got the errors like:

$ ./tests/phpunit -c tests/phpunit.xml

...

There was 1 error:

1) SessionTest::testFoo
Mockery\Exception\InvalidCountException: Method drupalSetMessage(<Any Arguments>) from Mockery_1_Drupal_Util_Drupal should be called
 exactly 1 times but called 0 times.

tests/vendor/mockery/mockery/library/Mockery/CountValidator/Exact.php:38
tests/vendor/mockery/mockery/library/Mockery/Expectation.php:309
tests/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:119
tests/vendor/mockery/mockery/library/Mockery/Container.php:301
tests/vendor/mockery/mockery/library/Mockery/Container.php:286
tests/vendor/mockery/mockery/library/Mockery.php:165

How can I debug to see with what parameters the method has been called during the test?

kenorb
  • 155,785
  • 88
  • 678
  • 743

1 Answers1

0

The workaround which I've found involves modifying _call()/_callStatic in the Mock.php.


For example, I've edited the file: vendor/mockery/mockery/library/Mockery/Mock.php.

Then I've added the line:

if ($method == 'drupalSetMessage') var_dump($method . " " . var_export($args, TRUE));

So the functions looks like:

public function __call($method, array $args)
{
    if ($method == 'drupalSetMessage') var_dump($method . " " . var_export($args, TRUE));
    return $this->_mockery_handleMethodCall($method, $args);
}

public static function __callStatic($method, array $args)
{
    if ($method == 'someStaticMethodName') var_dump($method, $args);
    return self::_mockery_handleStaticMethodCall($method, $args);
}

And here is less distractive logging line (which can handle circular references):

syslog(LOG_DEBUG, sprintf("%s: %s(%s)", __METHOD__, $method, json_encode($args, TRUE)));

Then the output can be found in the syslog. On macOS, the log stream can be displayed by the following command:

log stream --level debug --predicate 'processImagePath contains "php"

To print the list of functions where it happens (backtrace), this line could be useful:

printf("%s(%s) at %s\n", $method, json_encode($args, TRUE), implode(', ', array_column(debug_backtrace(), 'function')));
kenorb
  • 155,785
  • 88
  • 678
  • 743