[TYPO3-mvc] Re: HowTo write UnitTest for Controller?
Heinz Schilling
webdesign at webtekk.ch
Fri Mar 22 13:57:55 CET 2013
One step further I make this Controller Test class with some explanation I found on internet. This example doesn't work yet.
<?php
namespace <Vendor>\<Extension>\Tests;
/**
* Test case for class ClientController.
*
* @package <Vendor>
* @subpackage Ereport
*/
class ClientControllerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
/**
* @var \<Vendor>\<Extension>\Controller\ClientController
*/
protected $accessibleFixture;
/**
* @var \<Vendor>\<Extension>\Domain\Repository\ClientRepository
*/
protected $mockClientRepository;
/**
* Set up
*/
public function setUp() {
// Mock of the controller
//
// Some variables like $view are protected in the controller classes of extbase
// and are not accessible.
// We need a accessible mock of the controller to replace the protected methods
// and variables with public ones.
//
// getAccessibleMock extends given class with
// _set, _setRef, _setStatic, _get and _getStatic methods
// With this methods it is possible to access the before protected methods and variables
$this->accessibleFixture = $this->getAccessibleMock(
'<Vendor>\\<Extension>\\Controller\\ClientController',
// We don't want PHPUnit replaces all methods with stubs
// In this case would no own function tested
// We have to use a dummy function
array('dummy'),
array(),
'',
FALSE
);
// Mock of the view
$this->accessibleFixture->_set(
'view',
$this->getMock('TYPO3\\Fluid\\View\\TemplateView'),
array(),
array(),
'',
FALSE
);
// Mock of the repository
$this->mockClientRepository = $this->getMock(
'<Vendor>\\<Extension>\\Domain\\Repository\\ClientRepository',
array(),
array(),
'',
FALSE
);
}
/**
* Tear down
*/
public function tearDown() {
unset($this->accessibleFixture, $this->mockClientRepository);
}
/**
* Test for listing all clients
*
* @test
*/
public function listActionFindAllClients() {
$mockQueryResult = $this->getMock(
'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult',
array(),
array(),
'',
FALSE
);
$this->accessibleFixture->_get('clientRepository')->expects($this->once())
->method('listAction')
->will($this->returnValue($mockQueryResult));
$this->accessibleFixture->_get('view')->expects($this->once())
->method('assign')
->with($this->equalTo('clients'),$this->equalTo($mockQueryResult)
);
$this->fixture->listAction();
}
}
?>
I get PHP Fatal error: Call to a member function expects() on a non-object in /.../Tests/Unit/Controller/ClientControllerTest.php on line 99
This is $this->accessibleFixture->_get('mockClientRepository')->expects($this->once())
Any tips how to solve this problem?
I try this in setUp()
$this->accessibleFixture->injectClientRepository($this->mockClientRepository);
Thank you for helping.
Greetings, Heinz
More information about the TYPO3-project-typo3v4mvc
mailing list