Created
July 16, 2018 15:24
-
-
Save jarektkaczyk/e030787ecca95507508ff7a766d08dda to your computer and use it in GitHub Desktop.
PHPUnit - how to set expectations against exact methods calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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