The drop is always movingYou know that saying about standing on the shoulders of giants? Drupal is standing on a huge pile of midgetsAll content management systems suck, Drupal just happens to suck less.Popular open source software is more secure than unpopular open source software, because insecure software becomes unpopular fast. [That doesn't happen for proprietary software.]Drupal makes sandwiches happen.There is a module for that

Only this, and nothing more

Submitted by nk on Sun, 2013-12-15 07:29

So I am writing a PHPUnit mock object, added ensure calls and I wanted to also ensure that no more method calls happens than the ones I ensured. This is done by the following snippet (the actual code does more with the expects call of course):

<?php
$schema
= $this->getMockBuilder('Drupal\Core\Database\Schema')
  ->
disableOriginalConstructor()
  ->
getMock();
$schema->expects($this->at(0))
  ->
method('tableExists');
$schema->expects($this->at(1))
  ->
method('createTable');
$schema->expects($this->at(2))
  ->
method('createTable');
$schema->expects($this->exactly(3))
  ->
method($this->anything());
?>

There are two things of note here: One, the method() call accepts not just the typical strings but also constraints which are not the same as the matchers passed to expects().

Two, expectations can overlap. PHPunit will happily fire every expectation, even if there is more than one set for the same method. If method() would only accept simple strings then it'd be tempting to collect expectations by method name (or presume PHPunit works that way) but simply this is not happening because, as above, method() accepts more than strings. Instead, when a method call happens, PHPUnit fires each expectation and each decide whether it's a match by using the the matcher passed to expects and the constraint to method and nothing else. So even if we add a with() call, it is not used to determine matching only to verify the expected behavior and this is why it is not possible to simply mock multiple calls to the same method with different arguments.

Commenting on this Story is closed.