Skip to content

Commit

Permalink
[TEST] Add more tests for the ForumController
Browse files Browse the repository at this point in the history
  • Loading branch information
smichaelsen committed Jul 11, 2015
1 parent 05d8cf0 commit a3c4ad7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
51 changes: 51 additions & 0 deletions Tests/Unit/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
namespace Mittwald\Typo3Forum\Tests\Unit;

use Mittwald\Typo3Forum\Service\Authentication\AuthenticationService;
use Mittwald\Typo3Forum\Domain\Repository\User\FrontendUserRepository;
use TYPO3\CMS\Core\Tests\UnitTestCase;
use TYPO3\CMS\Extbase\Mvc\Web\Request;
use TYPO3\CMS\Extbase\Mvc\Web\Response;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\TemplateView;

abstract class AbstractControllerTest extends UnitTestCase {
Expand All @@ -13,6 +18,31 @@ abstract class AbstractControllerTest extends UnitTestCase {
*/
protected $authenticationServiceMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|FrontendUserRepository
*/
protected $frontendUserRepositoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|ObjectManager
*/
protected $objectManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|Request
*/
protected $requestMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|Response
*/
protected $responseMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|UriBuilder
*/
protected $uriBuilderMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|TemplateView
*/
Expand All @@ -23,6 +53,27 @@ abstract class AbstractControllerTest extends UnitTestCase {
*/
public function setUp() {
$this->authenticationServiceMock = $this->getMock('Mittwald\\Typo3Forum\\Service\\Authentication\\AuthenticationService');
$this->frontendUserRepositoryMock = $this->getMock(
'Mittwald\\Typo3Forum\\Domain\\Repository\\User\\FrontendUserRepository',
[],
[$this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManager')]
);
$this->objectManagerMock = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$this->objectManagerMock->expects($this->any())
->method('get')
->will($this->returnCallback(function($className) {
return $this->getMock($className);
}));
$this->requestMock = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
$this->responseMock = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response');
$this->responseMock->expects($this->any())->method('shutdown');
$this->uriBuilderMock = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder');
$this->uriBuilderMock->expects($this->any())
->method('reset')
->will($this->returnValue($this->uriBuilderMock));
$this->uriBuilderMock->expects($this->any())
->method('setTargetPageUid')
->will($this->returnValue($this->uriBuilderMock));
$this->viewMock = $this->getMockBuilder('TYPO3\\CMS\\Fluid\\View\\TemplateView')
->setMethods(['__construct', 'assign', 'assignMultiple'])
->disableOriginalConstructor()
Expand Down
72 changes: 71 additions & 1 deletion Tests/Unit/ForumControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function setUp() {
$this->forumController = new ForumController();

$this->inject($this->forumController, 'authenticationService', $this->authenticationServiceMock);
$this->inject($this->forumController, 'frontendUserRepository', $this->frontendUserRepositoryMock);
$this->inject($this->forumController, 'objectManager', $this->objectManagerMock);
$this->inject($this->forumController, 'request', $this->requestMock);
$this->inject($this->forumController, 'response', $this->responseMock);
$this->inject($this->forumController, 'uriBuilder', $this->uriBuilderMock);
$this->inject($this->forumController, 'view', $this->viewMock);

// inject root forum mock
Expand All @@ -52,7 +57,7 @@ public function setUp() {
);
$this->inject($this->forumController, 'forumRepository', $this->forumRepositoryMock);

// inject forum repository mock
// inject topic repository mock
$this->topicRepositoryMock = $this->getMock(
'Mittwald\\Typo3Forum\\Domain\\Repository\\Forum\\TopicRepository',
[],
Expand Down Expand Up @@ -113,6 +118,71 @@ public function showActionAssignsForumAndFoundTopicsToView() {
$this->forumController->showAction($forum);
}

/**
* @test
* @expectedException \Mittwald\Typo3Forum\Domain\Exception\Authentication\NotLoggedInException
* @expectedExceptionCode 1288084981
*/
public function markReadActionThrowsExceptionWhenNotLoggedIn() {
/** @var Forum $forum */
$forum = $this->getMock('Mittwald\\Typo3Forum\\Domain\\Model\\Forum\\Forum');
$this->forumController->markReadAction($forum);
}

/**
* @test
* @expectedException \Mittwald\Typo3Forum\Domain\Exception\Authentication\NotLoggedInException
* @expectedExceptionCode 1288084981
*/
public function markReadActionThrowsExceptionWhenAnonymous() {
/** @var Forum $forum */
$forum = $this->getMock('Mittwald\\Typo3Forum\\Domain\\Model\\Forum\\Forum');
$anonymousFrontendUserMock = $this->getMock('Mittwald\\Typo3Forum\\Domain\\Model\\User\\AnonymousFrontendUser');
$anonymousFrontendUserMock->expects($this->once())
->method('isAnonymous')
->will($this->returnValue(TRUE));
$this->frontendUserRepositoryMock->expects($this->once())
->method('findCurrent')
->will($this->returnValue($anonymousFrontendUserMock));
$this->forumController->markReadAction($forum);
}

/**
* @test
* @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
*/
public function markReadActionRedirectsToShowAction() {
/** @var \PHPUnit_Framework_MockObject_MockObject|Forum $forum */
$forum = $this->getMock('Mittwald\\Typo3Forum\\Domain\\Model\\Forum\\Forum');
$forum->expects($this->once())
->method('getChildren')
->will($this->returnValue(new ObjectStorage()));
$forum->expects($this->once())
->method('getTopics')
->will($this->returnValue(new ObjectStorage()));
$frontendUserMock = $this->getMock('Mittwald\\Typo3Forum\\Domain\\Model\\User\\FrontendUser');
$frontendUserMock->expects($this->once())
->method('isAnonymous')
->will($this->returnValue(FALSE));
$this->frontendUserRepositoryMock->expects($this->once())
->method('findCurrent')
->will($this->returnValue($frontendUserMock));
$this->requestMock->expects($this->once())
->method('getFormat')
->will($this->returnValue('html'));
$this->uriBuilderMock->expects($this->once())
->method('uriFor')
->will($this->returnCallback(function($action) {
return 'url/to/' . $action;
}));
$this->responseMock->expects($this->once())
->method('setHeader')
->with($this->equalTo('Location'), $this->callback(function($url){
return array_pop(explode('/', $url)) === 'show';
}));
$this->forumController->markReadAction($forum);
}

/**
* @param Forum $forum
*/
Expand Down

0 comments on commit a3c4ad7

Please sign in to comment.