Felix
Felix

Reputation: 5629

Typo3 PHPUNIT tests with multiple arguments

I'm starting with writing some UnitTests for my Extension, now I've got the problem that I got this error:

Call to a member function hasArgument() on a non-object in C:\xampp\htdocs\typo3\typo3conf\ext\rere\Classes\Controller\FachController.php on line 121

In Line 121 in FachController is this:

if ($this->request->hasArgument('modul')) {

I Think the problem is that I don't send the Argument to the controller.

So I tried this:

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

    $view = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
    $view->expects($this->once())->method('assign')->with(array('newFach' => $fach, 'modul' => 1));
    $this->inject($this->subject, 'view', $view);

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

But that dosn't have any effect ... why?

Update:

Dosn't work right now ... tried to add more mocks ... for other elements like findByUid ... but how should the mock look like?

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

    $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request');
    $mockRequest->expects($this->once())->method('hasArgument')->with('modul');

    $this->inject($this->subject, 'request', $mockRequest);

    $mockRequest->expects($this->once())->method('findByUid')->with(1);
    $this->inject($this->subject, 'request', $mockRequest);

    $view = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
    $this->inject($this->subject, 'view', $view);
    //$view->expects($this->once())->method('assign')->with('modul', $modul);

    $this->subject->showAction();

Update 3

Code of New Action got error by getUid()

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

    $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')->with(1);
    $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);
}

enter image description here

Upvotes: 0

Views: 377

Answers (1)

derhansen
derhansen

Reputation: 6133

Your test fails, because $this->request is not known to your unit test for the newAction. You need to create a mockObject for the request and inject it in test for the newAction as shown below:

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

Upvotes: 2

Related Questions