This is what I have:
$observer = $this->getMock('SomeObserverClass', array('method')); $observer->expects($this->once()) ->method('method') ->with($this->equalTo($arg1));
But the method should take two parameters. I am only testing that the first parameter is being passed correctly (as $arg1).
How do test the second parameter?
PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.
Stub. Stubs are used with query like methods - methods that return things, but it's not important if they're actually called. $stub = $this->createMock(SomeClass::class); $stub->method('getSomething') ->willReturn('foo'); $sut->action($stub);
Mockery is by default a stubbing library, not a mocking one (which is confusing because of its name). That means that ->shouldReceive(...) by default is "zero or more times". When using ->once(), you say it should be called zero or one time, but not more. This means it'll always pass.
I believe the way to do this is:
$observer->expects($this->once()) ->method('method') ->with($this->equalTo($arg1),$this->equalTo($arg2));
Or
$observer->expects($this->once()) ->method('method') ->with($arg1, $arg2);
If you need to perform a different type of assertion on the 2nd arg, you can do that, too:
$observer->expects($this->once()) ->method('method') ->with($this->equalTo($arg1),$this->stringContains('some_string'));
If you need to make sure some argument passes multiple assertions, use logicalAnd()
$observer->expects($this->once()) ->method('method') ->with($this->logicalAnd($this->stringContains('a'), $this->stringContains('b')));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With