From 143d6865515a8dfe45f324e6001e365f12546ffc Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 17 Jan 2020 16:03:05 +0100 Subject: [PATCH] Migrate mocking to prophesize for simplicity --- .../Controller/FrontendUserControllerTest.php | 39 +++++++++---------- readme.rst | 24 ------------ 2 files changed, 19 insertions(+), 44 deletions(-) diff --git a/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php b/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php index a9e33f1..7912629 100644 --- a/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php +++ b/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php @@ -21,8 +21,8 @@ namespace Codappix\TestingTalk\Tests\Unit\Controller; */ use Codappix\TestingTalk\Controller\FrontendUserController; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Prophecy\Prophecy\ObjectProphecy; use TYPO3\CMS\Extbase\Domain\Model\FrontendUser; use TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository; use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; @@ -36,15 +36,15 @@ class FrontendUserControllerTest extends TestCase protected $subject; /** - * @var MockObject + * @var ObjectProphecy */ - protected $viewMock; + protected $view; public function setUp(): void { $this->subject = new FrontendUserController(); - $this->viewMock = $this->getMockBuilder(ViewInterface::class)->getMock(); - ObjectAccess::setProperty($this->subject, 'view', $this->viewMock, true); + $this->view = $this->prophesize(ViewInterface::class); + ObjectAccess::setProperty($this->subject, 'view', $this->view->reveal(), true); } /** @@ -52,13 +52,13 @@ class FrontendUserControllerTest extends TestCase */ public function providedFrontendUserIsAssignedToViewInShowAction() { - $frontendUserMock = $this->getMockBuilder(FrontendUser::class)->getMock(); + $frontendUser = $this->prophesize(FrontendUser::class); - $this->viewMock->expects($this->once()) - ->method('assign') - ->with('frontendUser', $frontendUserMock); + $this->view + ->assign('frontendUser', $frontendUser->reveal()) + ->shouldBeCalled(); - $this->subject->showAction($frontendUserMock); + $this->subject->showAction($frontendUser->reveal()); } /** @@ -66,17 +66,16 @@ class FrontendUserControllerTest extends TestCase */ public function fetchedFrontendUsersAreAssignedToViewInIndexAction() { - $frontendUserRepositoryMock = $this->getMockBuilder(FrontendUserRepository::class) - ->disableOriginalConstructor() - ->getMock(); - ObjectAccess::setProperty($this->subject, 'frontendUserRepository', $frontendUserRepositoryMock, true); - $frontendUserRepositoryMock->expects($this->any()) - ->method('findAll') - ->willReturn(['user1' => 'test']); + $frontendUserRepository = $this->prophesize(FrontendUserRepository::class); + ObjectAccess::setProperty($this->subject, 'frontendUserRepository', $frontendUserRepository->reveal(), true); + $frontendUserRepository + ->findAll() + ->willReturn(['user1' => 'test']) + ->shouldBeCalled(); - $this->viewMock->expects($this->once()) - ->method('assign') - ->with('frontendUsers', ['user1' => 'test']); + $this->view + ->assign('frontendUsers', ['user1' => 'test']) + ->shouldBeCalled(); $this->subject->indexAction(); } diff --git a/readme.rst b/readme.rst index d1cb087..1b03b0e 100644 --- a/readme.rst +++ b/readme.rst @@ -119,30 +119,6 @@ What is mocking, or a mock? * https://en.wikipedia.org/wiki/Mock_object -Example mock: - -.. code-block:: php - - getMockBuilder(ViewInterface::class)->getMock(); - - $viewMock->expects($this->once()) - ->method('assign') - ->with('frontendUser', $frontendUserMock); - } - } - - ?> - Add the test ^^^^^^^^^^^^