phpunit - How to mock up a method whose name is "method"? -


class foo {   public function method() {    }   public function bar() {    } } 

if have class foo, can change bar's behaviour using below syntax.

$stub = $this->createmock(foo::class);  $stub->expects($this->any())     ->method('bar')     ->willreturn('baz'); 

limitation: methods named "method" example shown above works when original class not declare method named "method". if original class declare method named "method" $stub->expects($this->any())->method('dosomething')->willreturn('foo'); has used.

https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs


but question is, how can change foo::method()'s behaviour in phpunit? possible?

this works fine: tested php 7.0.9/phpunit 4.8.27

public function testmethod() {     $stub = $this->getmock(foo::class);      $stub->expects($this->once())         ->method('method')         ->willreturn('works!');      $this->assertequals('works!', $stub->method('method')); } 

edit:

tested php 7.0.9/phpunit 5.6.2 also:

public function testmethodwithdeprecatedgetmock() {     $stub = $this->getmock(foo::class);      $stub->expects($this->once())         ->method('method')         ->willreturn('works!');      $this->assertequals('works!', $stub->method('method')); }  public function testmethodwithcreatemock() {     $stub = $this->createmock(foo::class);      $stub->expects($this->once())         ->method('method')         ->willreturn('works!');      $this->assertequals('works!', $stub->method('method')); } 

show deprecation warning first method test pass successfully.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -