Migrate mocking to prophesize for simplicity

This commit is contained in:
Daniel Siepmann 2020-01-17 16:03:05 +01:00
parent 55f72aac20
commit 143d686551
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 19 additions and 44 deletions

View file

@ -21,8 +21,8 @@ namespace Codappix\TestingTalk\Tests\Unit\Controller;
*/ */
use Codappix\TestingTalk\Controller\FrontendUserController; use Codappix\TestingTalk\Controller\FrontendUserController;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use TYPO3\CMS\Extbase\Domain\Model\FrontendUser; use TYPO3\CMS\Extbase\Domain\Model\FrontendUser;
use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository; use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
@ -36,15 +36,15 @@ class FrontendUserControllerTest extends TestCase
protected $subject; protected $subject;
/** /**
* @var MockObject * @var ObjectProphecy
*/ */
protected $viewMock; protected $view;
public function setUp(): void public function setUp(): void
{ {
$this->subject = new FrontendUserController(); $this->subject = new FrontendUserController();
$this->viewMock = $this->getMockBuilder(ViewInterface::class)->getMock(); $this->view = $this->prophesize(ViewInterface::class);
ObjectAccess::setProperty($this->subject, 'view', $this->viewMock, true); ObjectAccess::setProperty($this->subject, 'view', $this->view->reveal(), true);
} }
/** /**
@ -52,13 +52,13 @@ class FrontendUserControllerTest extends TestCase
*/ */
public function providedFrontendUserIsAssignedToViewInShowAction() public function providedFrontendUserIsAssignedToViewInShowAction()
{ {
$frontendUserMock = $this->getMockBuilder(FrontendUser::class)->getMock(); $frontendUser = $this->prophesize(FrontendUser::class);
$this->viewMock->expects($this->once()) $this->view
->method('assign') ->assign('frontendUser', $frontendUser->reveal())
->with('frontendUser', $frontendUserMock); ->shouldBeCalled();
$this->subject->showAction($frontendUserMock); $this->subject->showAction($frontendUser->reveal());
} }
/** /**
@ -66,17 +66,16 @@ class FrontendUserControllerTest extends TestCase
*/ */
public function fetchedFrontendUsersAreAssignedToViewInIndexAction() public function fetchedFrontendUsersAreAssignedToViewInIndexAction()
{ {
$frontendUserRepositoryMock = $this->getMockBuilder(FrontendUserRepository::class) $frontendUserRepository = $this->prophesize(FrontendUserRepository::class);
->disableOriginalConstructor() ObjectAccess::setProperty($this->subject, 'frontendUserRepository', $frontendUserRepository->reveal(), true);
->getMock(); $frontendUserRepository
ObjectAccess::setProperty($this->subject, 'frontendUserRepository', $frontendUserRepositoryMock, true); ->findAll()
$frontendUserRepositoryMock->expects($this->any()) ->willReturn(['user1' => 'test'])
->method('findAll') ->shouldBeCalled();
->willReturn(['user1' => 'test']);
$this->viewMock->expects($this->once()) $this->view
->method('assign') ->assign('frontendUsers', ['user1' => 'test'])
->with('frontendUsers', ['user1' => 'test']); ->shouldBeCalled();
$this->subject->indexAction(); $this->subject->indexAction();
} }

View file

@ -119,30 +119,6 @@ What is mocking, or a mock?
* https://en.wikipedia.org/wiki/Mock_object * https://en.wikipedia.org/wiki/Mock_object
Example mock:
.. code-block:: php
<?php
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
class Test extends TestCase
{
public function someTest()
{
$viewMock = $this->getMockBuilder(ViewInterface::class)->getMock();
$viewMock->expects($this->once())
->method('assign')
->with('frontendUser', $frontendUserMock);
}
}
?>
Add the test Add the test
^^^^^^^^^^^^ ^^^^^^^^^^^^