PHPUnit, mocked interfaces, and instanceof

后端 未结 3 1885
悲哀的现实
悲哀的现实 2020-12-15 15:41

Sometimes in my code, I\'ll check to see if a particular object implements an interface:

if ($instance instanceof Interface) {};

However, c

相关标签:
3条回答
  • 2020-12-15 16:05

    there is also assertInstanceOf as of 3.5.0

    Example:

    $this->assertInstanceOf('\Models\User', $this->userService->findById(1));
    
    0 讨论(0)
  • 2020-12-15 16:23

    This works for me:

    $mock = $this->getMock('TestInterface');
    $this->assertTrue($mock instanceof TestInterface);
    

    Maybe it's a typo or maybe $instance isn't what you think it is?

    0 讨论(0)
  • 2020-12-15 16:24

    Use PhpUnit function assertInstanceOf.

    Example:

    $this->assertInstanceOf(ResponseInterface::class, $signInResponse);
    
    0 讨论(0)
提交回复
热议问题