2
votes

I have some problems with the PHP UnitTests for my Controller:

this is the function in my controller code that has to be tested

public function newAction(\ReRe\Rere\Domain\Model\Fach $newFach = NULL) {
    // Holt die übergebene Modulnummer
    if ($this->request->hasArgument('modul')) {
        // Holt das Modul-Objekt aus dem Repository
        $modul = $this->modulRepository->findByUid($this->request->getArgument('modul'));
    }
    // Ausgabe in der View
    $this->view->assignMultiple(array(
        'newFach' => $newFach, self::MODULUID => $modul->getUid(), 'modulname' => $modul->getModulname(), 'modulnummer' => $modul->getModulnr(), 'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
    ));
}

this is the PHPUnit-Test code for the function

public function newActionAssignsTheGivenFachToView() {
    $fach = new \ReRe\Rere\Domain\Model\Fach();
    $modul = array('');

    $MockGetArgument = $this->getMock('ReRe\Rere\Domain\Repository\ModulRepository', array('getArgument'), array(), '', FALSE);
    $MockGetArgument->expects($this->any())->method('getArgument')->with('modul');

    $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request');
    $mockRequest->expects($this->any())->method('hasArgument')->with('modul');
    $this->inject($this->subject, 'request', $mockRequest);

    $objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\ObjectManager', array(), array(), '', FALSE);
    $this->inject($this->subject, 'objectManager', $objectManager);

    $modulRepository = $this->getMock('ReRe\\Rere\\Domain\\Repository\\ModulRepository');
    $modulRepository->expects($this->any())->method('findByUid')->will($this->returnValue($modul));
    $this->inject($this->subject, 'modulRepository', $modulRepository);

    $view = $this->getMock(self::VIEWINTERFACE);
    $view->expects($this->any())->method(self::ASSIGN)->with('newFach', $fach);
    $this->inject($this->subject, 'view', $view);

    $this->subject->newAction($fach);
}

I keep getting this error as I run the test

Error in test case newActionAssignsTheGivenFachToView File: /Applications/MAMP/typo3_src/typo3/sysext/extbase/Classes/Mvc/Controller/AbstractController.php Line: 162 Argument 1 passed to TYPO3\CMS\Extbase\Mvc\Controller\AbstractController::injectObjectManager() must implement interface TYPO3\CMS\Extbase\Object\ObjectManagerInterface, instance of Mock_ObjectManager_fa2fde18 given, called in /Applications/MAMP/typo3_src/typo3/sysext/core/Tests/BaseTestCase.php on line 260 and defined

And this is the line from AbstractController.php that was called

public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $ObjectManager) {
    $this->ObjectManager = $ObjectManager;
    $this->arguments = $this->ObjectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments');
}

How can I implement this interface TYPO3\CMS\Extbase\Object\ObjectManagerInterface ?

I really appreciate every answers ! I have been trying and looking for answers for weeks :(

UPDATE 18.02.2015: PROBLEM SOLVED

CONFIGURED CODE:

$fach = new \ReRe\Rere\Domain\Model\Fach();
        $modul = new \ReRe\Rere\Domain\Model\Modul();

        $request = $this->getMock(self::REQUEST, array(), array(), '', FALSE);
        $request->expects($this->once())->method('hasArgument')->will($this->returnValue($this->subject));

        $modulRepository = $this->getMock(self::MODULREPOSITORY, array('findByUid'), array(), '', FALSE);
        $modulRepository->expects($this->once())->method('findByUid')->will($this->returnValue($modul));
        $this->inject($this->subject, 'modulRepository', $modulRepository);

        $request->expects($this->once())->method('getArgument')->will($this->returnValue($this->subject));
        $this->inject($this->subject, 'request', $request);

        $view = $this->getMock(self::VIEWINTERFACE);
        $view->expects($this->once())->method('assignMultiple')->with(array(
            'newFach' => $fach,
            'moduluid' => $modul->getUid(),
            'modulname' => $modul->getModulname(),
            'modulnummer' => $modul->getModulnr(),
            'gueltigkeitszeitraum' => $modul->getGueltigkeitszeitraum()
        ));
        $this->inject($this->subject, 'view', $view);

        $this->subject->newAction($fach);
1

1 Answers

1
votes

You are mocking the wrong (inexistent) ObjectManager. The correct namespace is TYPO3\\CMS\\Extbase\\Object\\ObjectManager.

So the correct line should be $objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManager', ...);

Otherwise I wonder why you mock the ObjectManager at all. You don't use it in your method.

One side note: Your code will fail if there is no 'modul' inside the request. Then $modul is not set and you will call getUid(), getModulname(), ... on an non-object.