-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Brain Monkey to mock class methods? #54
Comments
Hi @Tabrisrp Brain Monkey is a tool to mock functions, not class methods. If you're using modern PHPUnit version, you've at least two options to mock class methods without Brain Monkey. Built-in PHPUnit mock builderThe first is to make use of built-in PHPUnit mock builder: public function testShouldPurge() {
$url = 'http://example.org';
$dependency = $this->getMockBuilder(\Vendor\Name\Dependency::class)
->setMethods(['purge'])
->getMock();
$dependency
->expects($this->once())
->method('purge')
->with($this->equalTo($url));
$handler = new Handler($dependency);
$handler->clean_domain($url);
} The documentation for more advanced usage can be found in PHPUnit docs. ProphecyThe second option is to use Prophecy, which IIRC is included in PHPUnit since v6: public function testShouldPurge() {
$url = 'http://example.org';
$dependency = $this->prophesize(\Vendor\Name\Dependency::class);
$dependency->purge($url)->shouldBeCalled();
$handler = new Handler($dependency->reveal());
$handler->clean_domain($url);
} Please refer to Prophecy docs for more advanced usage. However, if you're using Brain Monkey, you've a 3rd option. MockeryBrain Monkey requires Mockery so, having it in you vendor folder, you can use it without much effort. And if you're following the docs to setup tests for Brain Monkey, your tests are already setup to use it. So you can also do something like this: public function testShouldPurge() {
$url = 'http://example.org';
$dependency = \Mockery::mock(\Vendor\Name\Dependency::class);
$dependency->shouldReceive('purge')->once()->with($url);
$handler = new Handler($dependency);
$handler->clean_domain($url);
} As usual, please refer to Mockery docs for more advanced usage. In test suites that use Brain Monkey, to use Mockery to mock class methods has the benefit to use same API for both class methods and functions (or WP hooks). In fact, Brain Monkey at its core is a "bridge" between the function-redefinition feature provided by Patchwork and the mock capabilities of Mockery. |
I'm keeping this issue open, because I often got asked similar questions. I will change the title for the benefit of people who'll come later. |
Thank you for the fast and detailed answer, using Mockery directly works perfectly in my case 👍 |
Changed my mind about keeping issues open for answered questions. I guess if this is necessary it should be added to documentation. |
This is more a question on the right use of
expect
than an issue.I'm trying to test that a method was called, but I'm not sure I'm doing it right.
I have a dependency class with a purge method
And another class using this dependency and method in a
clean_domain
methodIn my tests I have the following test method
But am getting the error
Mockery\Exception\InvalidCountException: Method Vendor_Name_Purge (<Any Arguments>) from Mockery_0 should be called exactly 1 times but called 0 times.
when running the tests.What am I missing here?
The text was updated successfully, but these errors were encountered: