47

Lets say I have a class that contains a function that uses type hinting like this:

class Testable 
{
    function foo (Dependency $dependency) 
    {

    }
}

And I want to unit test this class Testable using this code:

$dependencyMock = $this->getMockBuilder('Dependency')
        ->disableOriginalConstructor()
        ->getMock();


$testable = new Testable($dependencyMock);

If I use PHPUnit to create a stub of $dependency and then try to call the function foo using this mock (like above), I will get a fatal error that says:

Argument 1 passed to function foo() must be an instance of Dependency, instance of Mock_Foo given

How can I unit test this function with PHPUnit and still stub $dependency?

6
  • show your testcode where you create your mock. Dec 7, 2013 at 21:37
  • are you sure Dependency is a class that exists? Shouldn't it have a namespace?
    – Wouter J
    Dec 8, 2013 at 14:08
  • 1
    try to use full namespace like \some\name\space\Dependency when you use mocking, it should fix Dec 8, 2013 at 16:36
  • Thank you! Using a namespace fixes the error. Dec 8, 2013 at 22:27
  • 1
    @Shakil you should move your comment to an answer so it may be accepted. Dec 9, 2013 at 18:23

3 Answers 3

49

Use full namespace when you use mocking, it will fix the mockery inheritance problem.

$dependencyMock = $this->getMockBuilder('\Some\Name\Space\Dependency')
    ->disableOriginalConstructor()
    ->getMock();
$testable = new Testable($dependencyMock);
2
  • Thanks for this. I don't think the fact that you need the full namespace was clear in the documentation. Once I saw your post I thought "oh! of course" but I def didn't get that from the documentation. Jun 1, 2016 at 21:27
  • For a more robust way to do this in PHP 5.5+ stackoverflow.com/questions/20446771/…
    – GWed
    Dec 25, 2018 at 11:10
21

The easiest way to use a full namespace in PHP 5.4+ is with the class static method:

SomeClass::class

So in the OP's example:

$dependencyMock = $this->getMockBuilder(Dependency::class)
    ->disableOriginalConstructor()
    ->getMock();
$testable = new Testable($dependencyMock);

This makes refactoring with an IDE a lot easier

2
  • 3
    This is better than the other answers (in PHP 5.4+). ::class is the way to go to ensure the correct class is being used
    – dKen
    Jan 14, 2016 at 10:28
  • With this answer, I finally got it. Thanks a lot! Apr 6, 2016 at 6:31
5

My explication for the Shakil's answer:

I had the same problem.

Following the symfony2 cookbook, I created a mock of

\Doctrine\Common\Persistence\ObjectManager

and my service constructor was:

use Doctrine\ORM\EntityManager;

/* ... */

public function __construct(EntityManager $oEm)
{
    $this->oEm = $oEm;
}

So I created my unit test (following symfony2 cookbook):

$entityManager = $this->getMockBuilder('\Doctrine\Common\Persistence\ObjectManager')
    ->disableOriginalConstructor()
    ->getMock();

$myService = new MyService($entityManager);

Then I had the error:

Argument 1 passed to MyService::__construct() must be an instance of Doctrine\ORM\EntityManager, instance of Mock_ObjectManager_f4068b7f given

First I though that type hinting was incompatible with unit tests, because a mock instance was passed to the constructor instead of an instance of EntityManager.

So after some research, the class Mock_ObjectManager_f4068b7f is in fact a dynamic class extending the class of your mock (in my case Doctrine\ORM\EntityManager), so the type hinting is not a problem and works well.

My solution was to create a mock of Doctrine\ORM\EntityManager instead of \Doctrine\Common\Persistence\ObjectManager:

$entityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
    ->disableOriginalConstructor()
    ->getMock();

$myService = new MyService($entityManager);

I am just beginning with unit tests, so you may find my explication evident :p

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.