Skip to content

Commit

Permalink
[TASK] Replace prophecy calls in AdminPanelInitiatorTest
Browse files Browse the repository at this point in the history
Resolves: #98558
Releases: main, 11.5
Change-Id: Ic7bfd4812e49481f51603826a729423951275dbb
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76034
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
  • Loading branch information
maddy2101 authored and sbuerk committed Oct 9, 2022
1 parent 0032537 commit d05ada7
Showing 1 changed file with 24 additions and 41 deletions.
Expand Up @@ -17,9 +17,7 @@

namespace TYPO3\CMS\Adminpanel\Tests\Unit\Middleware;

use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -34,8 +32,6 @@
*/
class AdminPanelInitiatorTest extends UnitTestCase
{
use ProphecyTrait;

/**
* @var bool Reset singletons created by subject
*/
Expand All @@ -58,27 +54,20 @@ public function processCallsInitialize(): void
'display_top' => true,
],
];
$userAuthenticationProphecy = $this->prophesize(FrontendBackendUserAuthentication::class);
$userAuthenticationProphecy->getTSConfig()->willReturn($tsConfig);
$userAuthentication = $userAuthenticationProphecy->reveal();
$userAuthentication = $this->getMockBuilder(FrontendBackendUserAuthentication::class)->getMock();
$userAuthentication->expects(self::once())->method('getTSConfig')->willReturn($tsConfig);
$userAuthentication->uc = $uc;
$GLOBALS['BE_USER'] = $userAuthentication;

$controller = $this->prophesize(MainController::class);
GeneralUtility::setSingletonInstance(MainController::class, $controller->reveal());
$controller = $this->getMockBuilder(MainController::class)->disableOriginalConstructor()->getMock();
GeneralUtility::setSingletonInstance(MainController::class, $controller);
$handler = $this->prophesizeHandler();
$request = $this->prophesize(ServerRequestInterface::class);
$request->withAttribute(Argument::cetera())->willReturn($request);
$controller->initialize($request->reveal())->willReturn($request);
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
$request->expects(self::any())->method('withAttribute')->withAnyParameters()->willReturn($request);
$controller->expects(self::once())->method('initialize')->with($request)->willReturn($request);

// Act
$adminPanelInitiator = new AdminPanelInitiator();
$adminPanelInitiator->process(
$request->reveal(),
$handler->reveal()
);
// Assert
$controller->initialize(Argument::any())->shouldHaveBeenCalled();
$adminPanelInitiator->process($request, $handler);
}

/**
Expand Down Expand Up @@ -123,37 +112,31 @@ public function processDoesNotCallInitializeIfNoAdminPanelModuleIsEnabled(): voi
*/
protected function checkAdminPanelDoesNotCallInitialize(array $tsConfig, array $uc): void
{
$userAuthenticationProphecy = $this->prophesize(FrontendBackendUserAuthentication::class);
$userAuthenticationProphecy->getTSConfig()->willReturn($tsConfig);
$userAuthentication = $userAuthenticationProphecy->reveal();
$userAuthentication = $this->getMockBuilder(FrontendBackendUserAuthentication::class)->getMock();
$userAuthentication->expects(self::once())->method('getTSConfig')->willReturn($tsConfig);
$userAuthentication->uc = $uc;
$GLOBALS['BE_USER'] = $userAuthentication;

$controller = $this->prophesize(MainController::class);
GeneralUtility::setSingletonInstance(MainController::class, $controller->reveal());
$controller = $this->getMockBuilder(MainController::class)->disableOriginalConstructor()->getMock();
GeneralUtility::setSingletonInstance(MainController::class, $controller);
$handler = $this->prophesizeHandler();
$request = $this->prophesize(ServerRequestInterface::class);
// Act
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();

$adminPanelInitiator = new AdminPanelInitiator();
$adminPanelInitiator->process(
$request->reveal(),
$handler->reveal()
);
// Assert
$controller->initialize(Argument::any())->shouldNotHaveBeenCalled();
$adminPanelInitiator->process($request, $handler);

$controller->expects(self::never())->method('initialize');
}

/**
* @return ObjectProphecy<RequestHandlerInterface>
* @return RequestHandlerInterface&MockObject
*/
protected function prophesizeHandler(): ObjectProphecy
protected function prophesizeHandler(): MockObject
{
$handler = $this->prophesize(RequestHandlerInterface::class);
$handler
->handle(Argument::any())
->willReturn(
$this->prophesize(ResponseInterface::class)->reveal()
);
$response = $this->getMockBuilder(ResponseInterface::class)->getMock();

$handler = $this->getMockBuilder(RequestHandlerInterface::class)->onlyMethods(['handle'])->getMock();
$handler->expects(self::any())->method('handle')->withAnyParameters()->willReturn($response);
return $handler;
}
}

0 comments on commit d05ada7

Please sign in to comment.