[Flow] Creating simple UnitTests (RestController)

Christian Müller christian.mueller at typo3.org
Wed Apr 2 10:20:59 CEST 2014


Hello Peter,

Peter R wrote:
> Hello,
>
> is there any examples, how i can test methods in controller
> (indexAction, showAction, updateAction, deleteAction)?

I would write functional tests for those controller actions and use the 
Http\Client class to make "real" requests. There should be examples in 
the Flow Functional tests.

> I tried following in test controller:
> public function setUp() {
> $this->subject = new StandardController();
>
> $this->view = $this->getMock('TYPO3\\Flow\\Mvc\\View\\ViewInterface');
> $this->inject($this->subject, 'view', $this->view);
> }
> /**
> * @test
> * @return void
> */
> public function listActionCanBeCalled() {
> $this->subject->listAction();
> }
>
> The result is this error:
> Fatal error: Call to a member function reset() on a non-object in
> C:\flow-project\Packages\Framework\TYPO3.Flow\Classes\TYPO3\Flow\Mvc\Controller\AbstractController.php
> on line 262
>

I guess building a unit test will be pretty difficult, you might want to 
write unit tests for the actual logic in your models for example.

> At some point, i want to start a session in the StandardController but
> then i get an error after executing phpunit:
> session_start(): Cannot send session cookie - headers already sent
>
> I didn't found usefull examples for tasks like these. So maybe you have
> one.
> This have to be tests on classes how extends RestController.

Additionally you might want to look into Behavior testing with Behat, 
that might also be the right thing for you. We have a package for that
[1] that we also use in Neos for testing, so for examples see the Neos 
package.

Cheers,
Christian

[1] https://github.com/Flowpack/Flowpack.Behat
-- 
Christian Müller
Flow / Neos Team Community Contact

TYPO3 .... inspiring people to share!
Get involved: http://typo3.org




More information about the Flow mailing list