[TYPO3-mvc] HowTo write UnitTest for Controller?

Patrick Schriner patrick.schriner at diemedialen.de
Mon Mar 18 10:32:27 CET 2013


Hello Heinz,

Testing list action I would test

- if list action calls the repositories findAll method
- if list action assigns the result as "clients"

As you don't would not want to test the repository you would build mock  
for the ClientRepository.

Patrick

On Sun, 17 Mar 2013 13:38:36 +0100, Heinz Schilling <webdesign at webtekk.ch>  
wrote:

> I like the concept of Test-Driven Development, but can't understand the  
> writing of a controller test.
>
> I hope I get some help and explanation here from you.
>
> TYPO3 6.0.4
> Extbase 6.0.0
> Fluid 6.0.0
> PHPUnit 3.6.12
>
> Replace <Vendor> with your company name for example.
> Replace <Extension> with your extension name.
>
> Classes/Domain/Model/Client.php
> <?php
> namespace <Vendor>\<Extension>\Domain\Model;
> class Client extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
>     ...
> }
> ?>
>
> Domain/Repository/ClientRepository.php
> <?php
> namespace <Vendor>\<Extension>\Domain\Repository;
> class ClientRepository extends \TYPO3\CMS\Extbase\Persistence\Repository  
> {
> }
> ?>
>
> Classes/Controller/ClientController.php
> <?php
> namespace <Vendor>\<Extension>\Controller;
> class ClientController extends  
> \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
>
>     /**
>      * clientRepository
>      *
>      * @var \<Vendor>\<Extension>\Domain\Repository\ClientRepository
>      * @inject
>      */
>     protected $clientRepository;
>
>     /**
>      * action list
>      *
>      * @return void
>      */
>     public function listAction() {
>         $clients = $this->clientRepository->findAll();
>         $this->view->assign('clients', $clients);
>     }
>     ...
> }
> ?>
>
> Tests/Unit/Controller/ClientControllerTest.php
> <?php
> namespace <Vendor>\<Extension>\Tests;
> class ClientControllerTest extends  
> \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
>     ???
>
>     /**
>      * @test
>      */
>     public function listActionFindAllClients() {
>         ???
>     }
>
> }
> ?>
>
> Examples I found with Google are old or without namespace or haven't any  
> code description. I don't get them all together to a understandable  
> example for me.
>
> Thank you for helping
> Heinz


-- 
Patrick Schriner


More information about the TYPO3-project-typo3v4mvc mailing list