Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Created July 16, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarektkaczyk/e030787ecca95507508ff7a766d08dda to your computer and use it in GitHub Desktop.
Save jarektkaczyk/e030787ecca95507508ff7a766d08dda to your computer and use it in GitHub Desktop.
PHPUnit - how to set expectations against exact methods calls
<?php
$mock = $this->createMock(SomeDependency::class);
$mock->expects($this->once())->method('firstMethod')->willReturn('something');
$mock->expects($this->once())->method('secondMethod')->willReturn('something else');
$cut = new SomeClass($mock)
$cut->act();
// how to ensure there's no method called other than the 2 above?
// I'm using workaround like so currently:
$mock->expects($this->never())->method(
$this->logicalNot(
$this->in('firstMethod', 'secondMethod')
)
);
// 'in' is a custom constraint:
// in_array($other, ...$items)
// -------------------------------------
// SomeClass
public function act()
{
$this->dependency->firstMethod(); // okay
$this->dependency->secondMethod(); // okay
$this->dependency->anotherMethod(); // test should fail at this call
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment