|
|
@@ -5,55 +5,55 @@ namespace Opentalent\OtCore\Tests\Unit\Controller;
|
|
|
use Nimut\TestingFramework\TestCase\UnitTestCase;
|
|
|
use Opentalent\OtCore\Controller\SelectedSiteController;
|
|
|
use Opentalent\OtCore\Exception\NoSiteSelected;
|
|
|
-use Opentalent\OtCore\Exception\NoSuchWebsiteException;
|
|
|
use Opentalent\OtCore\Website\OtPageRepository;
|
|
|
use Opentalent\OtCore\Website\OtWebsiteRepository;
|
|
|
-use Prophecy\Argument;
|
|
|
+use PHPUnit\Framework\MockObject\MockObject;
|
|
|
+use Psr\Http\Message\ResponseInterface;
|
|
|
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
|
|
|
|
|
|
-class SelectedSiteControllerTest extends UnitTestCase
|
|
|
-{
|
|
|
- private $controller;
|
|
|
+class TestableSelectedSiteController extends SelectedSiteController {
|
|
|
+ public $currentWebsite;
|
|
|
+ public $currentRootUid;
|
|
|
|
|
|
- public function setUp(): void
|
|
|
+ public function callActionMethod(RequestInterface $request): \Psr\Http\Message\ResponseInterface
|
|
|
{
|
|
|
- $this->controller = new SelectedSiteController();
|
|
|
+ return parent::callActionMethod($request);
|
|
|
}
|
|
|
|
|
|
- private function injectPageRepositoryWithSelectedUidAndMountpoints(?int $selectedUid, array $mountpoints = []) {
|
|
|
- $otPageRepository = $this->prophesize(OtPageRepository::class);
|
|
|
-
|
|
|
- if ($selectedUid != null) {
|
|
|
- $otPageRepository->getCurrentBeRootUid()->willReturn($selectedUid);
|
|
|
- } else {
|
|
|
- $otPageRepository->getCurrentBeRootUid()->willThrow(new NoSiteSelected());
|
|
|
- }
|
|
|
- $otPageRepository->getCurrentBeUserMountpoints()->shouldBeCalled()->willReturn($mountpoints);
|
|
|
- $this->controller->injectOtPageRepository($otPageRepository->reveal());
|
|
|
-
|
|
|
- $otWebsiteRepository = $this->prophesize(OtWebsiteRepository::class);
|
|
|
- $otWebsiteRepository->getWebsiteByPageUid(Argument::type('integer'))->willReturn(['uid' => 1, 'title' => 'Foo']);
|
|
|
- $this->controller->injectOtWebsiteRepository($otWebsiteRepository->reveal());
|
|
|
-
|
|
|
+ public function callParentActionMethod(RequestInterface $request): \Psr\Http\Message\ResponseInterface
|
|
|
+ {
|
|
|
+ return parent::callParentActionMethod($request);
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- private function callActionMethodProxy() {
|
|
|
- $reflection = new \ReflectionObject($this->controller);
|
|
|
- $method = $reflection->getMethod('callActionMethod');
|
|
|
- $method->setAccessible(true);
|
|
|
-
|
|
|
- $request = $this->getMockBuilder(RequestInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+class SelectedSiteControllerTest extends UnitTestCase
|
|
|
+{
|
|
|
+ protected MockObject | OtPageRepository $otPageRepository;
|
|
|
+ protected MockObject | OtWebsiteRepository $otWebsiteRepository;
|
|
|
|
|
|
- $argumentsProperty = $reflection->getProperty('preventPropagation');
|
|
|
- $argumentsProperty->setAccessible(true);;
|
|
|
- $argumentsProperty->setValue($this->controller, true);
|
|
|
+ public function setUp(): void
|
|
|
+ {
|
|
|
+ $this->otPageRepository = $this
|
|
|
+ ->getMockBuilder(OtPageRepository::class)
|
|
|
+ ->disableOriginalConstructor()
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $this->otWebsiteRepository = $this
|
|
|
+ ->getMockBuilder(OtWebsiteRepository::class)
|
|
|
+ ->disableOriginalConstructor()
|
|
|
+ ->getMock();
|
|
|
+ }
|
|
|
|
|
|
- $method->invokeArgs($this->controller, [$request]);
|
|
|
+ public function getMockForMethod(string $methodName): MockObject | TestableSelectedSiteController
|
|
|
+ {
|
|
|
+ $controller = $this->getMockBuilder(TestableSelectedSiteController::class)
|
|
|
+ ->setMethodsExcept([$methodName, 'injectOtPageRepository', 'injectOtWebsiteRepository'])
|
|
|
+ ->getMock();
|
|
|
|
|
|
- $currentRootUidProperty = $reflection->getProperty('currentRootUid');
|
|
|
- $currentRootUidProperty->setAccessible(true);
|
|
|
+ $controller->injectOtPageRepository($this->otPageRepository);
|
|
|
+ $controller->injectOtWebsiteRepository($this->otWebsiteRepository);
|
|
|
|
|
|
- return $currentRootUidProperty->getValue($this->controller);
|
|
|
+ return $controller;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -63,12 +63,38 @@ class SelectedSiteControllerTest extends UnitTestCase
|
|
|
* @test
|
|
|
*/
|
|
|
public function callActionMethodForSingleSite() {
|
|
|
+ $controller = $this->getMockForMethod('callActionMethod');
|
|
|
+
|
|
|
+ $this->otPageRepository->method('getCurrentBeRootUid')->willThrowException(new NoSiteSelected());
|
|
|
|
|
|
- $this->injectPageRepositoryWithSelectedUidAndMountpoints(null, [1]);
|
|
|
+ $this->otPageRepository
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('getCurrentBeUserMountpoints')
|
|
|
+ ->willReturn([1]);
|
|
|
|
|
|
- $currentRootUid = $this->callActionMethodProxy();
|
|
|
+ $this->otWebsiteRepository
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('getWebsiteByPageUid')
|
|
|
+ ->with(1)
|
|
|
+ ->willReturn(['uid' => 1, 'title' => 'Foo']);
|
|
|
|
|
|
- $this->assertEquals(1, $currentRootUid);
|
|
|
+ $response = $this
|
|
|
+ ->getMockBuilder(ResponseInterface::class)
|
|
|
+ ->disableOriginalConstructor()
|
|
|
+ ->getMock();
|
|
|
+
|
|
|
+ $controller
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('callParentActionMethod')
|
|
|
+ ->willReturn($response);
|
|
|
+
|
|
|
+ $request = $this->getMockBuilder(RequestInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $result = $controller->callActionMethod($request);
|
|
|
+
|
|
|
+ $this->assertEquals($response, $result);
|
|
|
+ $this->assertEquals($controller->currentRootUid, 1);
|
|
|
+ $this->assertEquals($controller->currentWebsite, ['uid' => 1, 'title' => 'Foo']);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -78,12 +104,37 @@ class SelectedSiteControllerTest extends UnitTestCase
|
|
|
* @test
|
|
|
*/
|
|
|
public function callActionMethodForMultiSite() {
|
|
|
+ $controller = $this->getMockForMethod('callActionMethod');
|
|
|
+
|
|
|
+ $this->otPageRepository->method('getCurrentBeRootUid')->willReturn(2);
|
|
|
+
|
|
|
+ $this->otPageRepository
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('getCurrentBeUserMountpoints')
|
|
|
+ ->willReturn([1, 2]);
|
|
|
+
|
|
|
+ $this->otWebsiteRepository
|
|
|
+ ->method('getWebsiteByPageUid')
|
|
|
+ ->with(2, true)
|
|
|
+ ->willReturn(['uid' => 2, 'title' => 'Bar']);
|
|
|
|
|
|
- $this->injectPageRepositoryWithSelectedUidAndMountpoints(2, [1, 2]);
|
|
|
+ $request = $this->getMockBuilder(RequestInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
+
|
|
|
+ $response = $this
|
|
|
+ ->getMockBuilder(ResponseInterface::class)
|
|
|
+ ->disableOriginalConstructor()
|
|
|
+ ->getMock();
|
|
|
|
|
|
- $currentRootUid = $this->callActionMethodProxy();
|
|
|
+ $controller
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('callParentActionMethod')
|
|
|
+ ->willReturn($response);
|
|
|
|
|
|
- $this->assertEquals(2, $currentRootUid);
|
|
|
+ $result = $controller->callActionMethod($request);
|
|
|
+
|
|
|
+ $this->assertEquals($response, $result);
|
|
|
+ $this->assertEquals($controller->currentRootUid, 2);
|
|
|
+ $this->assertEquals($controller->currentWebsite, ['uid' => 2, 'title' => 'Bar']);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -93,11 +144,28 @@ class SelectedSiteControllerTest extends UnitTestCase
|
|
|
* @test
|
|
|
*/
|
|
|
public function callActionMethodForNoSite() {
|
|
|
+ $controller = $this->getMockForMethod('callActionMethod');
|
|
|
+
|
|
|
+ $this->otPageRepository->method('getCurrentBeRootUid')->willThrowException(new NoSiteSelected());
|
|
|
|
|
|
- $this->injectPageRepositoryWithSelectedUidAndMountpoints(null, [1, 2]);
|
|
|
+ $this->otPageRepository
|
|
|
+ ->expects(self::once())
|
|
|
+ ->method('getCurrentBeUserMountpoints')
|
|
|
+ ->willReturn([1, 2]);
|
|
|
+
|
|
|
+ $this->otWebsiteRepository
|
|
|
+ ->expects(self::never())
|
|
|
+ ->method('getWebsiteByPageUid');
|
|
|
+
|
|
|
+ $controller
|
|
|
+ ->expects(self::never())
|
|
|
+ ->method('callParentActionMethod');
|
|
|
+
|
|
|
+ $request = $this->getMockBuilder(RequestInterface::class)->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
- $currentRootUid = $this->callActionMethodProxy();
|
|
|
+ $this->expectException(\Exception::class);
|
|
|
+ $this->expectExceptionMessage("No website selected");
|
|
|
|
|
|
- $this->assertNull($currentRootUid);
|
|
|
+ $controller->callActionMethod($request);
|
|
|
}
|
|
|
}
|