0

I have a function in my HandlerClass that works fine:

/**
     * @param $entity
     * @return null|object
     */
    public function findEntityById($id)
    {
        if($id)
        {
            $entity = $this->repository->find($id);
            if (empty($entity)) {
                return false;
            }
            return $entity;
        }
        return false;
    }

I wrote a test for a function:getCheckedList(), which uses this function:findEntityById(). I mocked the function:findEntityById(), so that it should return false.

$reviewHandler = $this->getMock(
            'My\Bundle\Handler\RevHandler',
            array('saveEntity'),
            array(
                $this->entityManager,
                'My\Bundle\Entity\Rev',
                $this->guzzleClient,
                $this->serializer,
                $this->apiSettings
            )
        );

        $reviewHandler->expects($this->once())
            ->method('findEntityById')
            ->will($this->returnValue(false));

        $result = $reviewHandler->getCheckedList(22);

        $this->assertArrayHasKey(0,$result);
        $this->assertEquals(22,$result[0]);

But then I get an error after my test:report:phpunit

PHP Fatal error:  Call to a member function find() on a non-object on line 152

And this is this line in my function:findEntityById() which is mocked and which throws an error in my test:

$entity = $this->repository->find($id);

In the __construct form my RevHandler I call the parent::__construct:

public function __construct($entityManager, $entityClass, $guzzleClient, $serializer, $apiSettings)
    {
        parent::__construct($entityManager, $entityClass, $guzzleClient, $serializer, $apiSettings);
    }

In my parent::__construct it looks like this:

public function __construct(EntityManager $entityManager, $entityClass, $guzzleClient, $serializer, $apiSettings)
    {
        $this->entityManager = $entityManager;
        $this->entityClass = $entityClass;
        $this->repository = $this->entityManager->getRepository($this->entityClass);
        $this->guzzleClient = $guzzleClient;
        $this->serializer = $serializer;
        $this->apiSettings = $apiSettings;
    }

The code without testing works fine, but I don't know what can be wrong when I'm testing. Any idea? THANKS!!!

4
  • Have you assigned $this->repository anywhere? It should be an object Commented Jul 8, 2014 at 7:11
  • How ca I do this? I updated my question with the parent::__construct, please have a look. Commented Jul 8, 2014 at 7:13
  • Why do you think it's an object? Your $this->entityManager->getRepository doesn't return what you assume Commented Jul 8, 2014 at 7:15
  • How can I set the repository as an object? It should be set automatically in my parent::__construct?! Commented Jul 8, 2014 at 7:16

1 Answer 1

2

Have it, it was too simple and a really stupid failure from me:

WRONG:

$reviewHandler = $this->getMock(
            'My\Bundle\Handler\RevHandler',
            array('saveEntity'),
            array(
                $this->entityManager,
                'My\Bundle\Entity\Rev',
                $this->guzzleClient,
                $this->serializer,
                $this->apiSettings
            )
        );

RIGHT:

$reviewHandler = $this->getMock(
            'My\Bundle\Handler\RevHandler',
            array('findEntityById'),
            array(
                $this->entityManager,
                'My\Bundle\Entity\Rev',
                $this->guzzleClient,
                $this->serializer,
                $this->apiSettings
            )
        );

I gave the wrong function to my mocked object! Wrong: saveEntity Right: findEntityById

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.