1

Hi try to test my controller this controller call getRepository method the getRepopsitory is a function of DocumentManager.

I have to pass differente parameters to getREpository() but when i try do mock DocumentManager et set Methods getRepository I wasn't enable to mock the method withe two different result.

My test :

<?php

  public function testGetLetter()
    {
        $_box = new Box();
        $_box->setName('test-A');
        $_box->setId('abc01');
        $_boxId = $_box->getId();
        $ids = [];

        for ($i = 0; $i < 10; $i++) {
            $letter = new letter();
            $letter->setContent('content: ' . $i);
            $_box->addLetter($letter);
            $letter->setBox($_box);
            $ids[] = $_boxId;
        }

        $request = $this->createMock("Symfony\Component\HttpFoundation\Request");

        $boxRepository = $this->createMock(BoxRepository::class);
        $boxRepository->expects($this->once())
            ->method('find')
            ->willReturn($_box);

        $letterRepo = $this->createMock(LetterRepository::class);


        $documentManager = $this->getMockBuilder(DocumentManager::class)
            ->disableOriginalConstructor()
            ->setMethods(['getRepository'])
            ->getMock();

        $documentManager->expects($this->once())
            ->method('getRepository')
            ->with($this->equalTo('Bundle:Box'))
            ->will($this->returnValue($boxRepository));


        $documentManager->expects($this->once())
            ->method('getRepository')
            ->with($this->equalTo('Bundle:Letter'))
            ->will($this->returnValue($letterRepo));


        $boxControler = new boxController();


        $boxControler->getletters($palletId, $documentManager, $request);

    }

My controller

public function getletters(string $id, DocumentManager $dm, Request $request): Response
    {
        $box = $dm->getRepository('Bundle:Box')->find($id);

        $letters = $box->getLetters();
        $letterRepository = $dm->getRepository('Bundle:Letter');
        $result = [];
        foreach ($letters as $letter){
            $result [] = $letterRepository->prepareLetterData($letter);
        }
        return $this->setResponse($result, $request);
    }

error :

Testing Tests\Controller\BoxControllerTest

Expectation failed for method name is equal to "getRepository" when invoked 1 time(s)
Parameter 0 for invocation DocumentManagerDecorator::getRepository('Bundle:Box') does not match expected value.
Failed asserting that two strings are equal.
Expected :'Bundle:Letter'
Actual   :'Bundle:Box'
 <Click to see difference>

 /var/www/html/Controller/BoxController.php:151
 /var/www/html/Tests/Controller/BoxControllerTest.php:147

I saw this post but I can't apply the response to my situation

2
  • Did you see this answer on that post? It sounds like returnValueMap is what you're looking for. Commented Aug 21, 2018 at 14:37
  • try with ->with('Bundle:Box') instead of ->with($this->equalTo('Bundle:Box')) Commented Aug 21, 2018 at 14:53

1 Answer 1

3

You can use at()

$mock->expects($this->at(0))
    ->method('foo')
    ->willReturn('firstValue');

$mock->expects($this->at(1))
    ->method('foo')
    ->willReturn('secondValue');

or as @iainn mentioned returnValueMap FROM PHPUnit doc

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.