Browse Source

fix unit tests

Olivier Massot 1 year ago
parent
commit
f55c238c7e

+ 1 - 1
ot_admin/Classes/Command/UpdateSiteCommand.php

@@ -115,7 +115,7 @@ class UpdateSiteCommand extends Command
                     $this->siteController->updateSiteAction($org_id);
                 } catch (NoSuchOrganizationException $e) {
                     if ($delete) {
-                        $siteController->deleteSiteAction($org_id);
+                        $this->siteController->deleteSiteAction($org_id);
                     } else {
                         throw $e;
                     }

+ 17 - 1
ot_core/Classes/Controller/SelectedSiteController.php

@@ -8,6 +8,7 @@ use Opentalent\OtCore\Exception\NoSuchWebsiteException;
 use Opentalent\OtCore\Website\OtPageRepository;
 use Psr\Http\Message\ResponseInterface;
 use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
 use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
 use TYPO3\CMS\Extbase\Mvc\RequestInterface;
 use TYPO3\CMS\Extbase\Object\ObjectManager;
@@ -71,7 +72,7 @@ class SelectedSiteController extends ActionController
         }
 
         // [TESTS ONLY]
-        if ($this->preventPropagation) { return parent::callActionMethod($request); }
+        if ($this->preventPropagation) { return $this->callParentActionMethod($request); }
 
         // No site is selected, redirect to the warning page
         if ($this->actionMethodName != 'displayNoSelectedPageWarningAction' && $this->currentRootUid == null) {
@@ -80,8 +81,23 @@ class SelectedSiteController extends ActionController
             // $this->forward('displayNoSelectedPageWarning', 'SelectedSite', 'OtCore');
         }
 
+        return $this->callParentActionMethod($request);
+    }
+
+    protected function callParentActionMethod(RequestInterface $request): ResponseInterface
+    {
         return parent::callActionMethod($request);
     }
 
     protected function displayNoSelectedPageWarningAction() {}
+
+//    /**
+//     * [TESTS ONLY]
+//     * @param Arguments $arguments
+//     * @return void
+//     */
+//    public function injectArguments(Arguments $arguments): void
+//    {
+//        $this->arguments = $arguments;
+//    }
 }

File diff suppressed because it is too large
+ 0 - 0
ot_core/Tests/Build/.phpunit.result.cache


+ 112 - 44
ot_core/Tests/Unit/Controller/SelectedSiteControllerTest.php

@@ -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);
     }
 }

+ 4 - 0
ot_core/Tests/Unit/Domain/Repository/AbstractApiRepositoryTestCase.php

@@ -8,6 +8,7 @@ use Opentalent\OtCore\Domain\Repository\BaseApiRepository;
 use Opentalent\OtCore\Service\OpentalentApiService;
 use Opentalent\OtCore\Tests\Unit\Fixtures\ApiResponseFixtures;
 use Prophecy\Argument;
+use Prophecy\PhpUnit\ProphecyTrait;
 use ReflectionClass;
 use TYPO3\CMS\Core\Core\ApplicationContext;
 
@@ -18,6 +19,8 @@ use TYPO3\CMS\Core\Core\ApplicationContext;
  */
 abstract class AbstractApiRepositoryTestCase extends UnitTestCase
 {
+    use ProphecyTrait;
+
     const TESTED_CLASS = '';
 
     protected $context;
@@ -62,6 +65,7 @@ abstract class AbstractApiRepositoryTestCase extends UnitTestCase
     protected function injectClientFor($uri, $http_method='GET') {
         // mock the Guzzle client
         $willReturn = $this->fixture->get($uri);
+
         $client = $this->prophesize(Client::class);
         $client->request($http_method, $uri, [])
             ->shouldBeCalled()

+ 3 - 7
ot_core/Tests/Unit/Domain/Repository/EventRepositoryTest.php

@@ -2,13 +2,9 @@
 
 namespace Opentalent\OtCore\Tests\Unit\Domain\Repository;
 
-use Prophecy\PhpUnit\ProphecyTrait;
-
 
 class EventRepositoryTest extends AbstractApiRepositoryTestCase
 {
-    use ProphecyTrait;
-
     const TESTED_CLASS = "Opentalent\OtCore\Domain\Repository\EventRepository";
 
     /**
@@ -51,7 +47,7 @@ class EventRepositoryTest extends AbstractApiRepositoryTestCase
     public function findByOrganizationIdWithParams() {
         $organization_id = 1;
 
-        $expected_uri = "api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeStart%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC";
+        $expected_uri = "api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC";
         $this->injectClientFor($expected_uri);
 
         $actual = $this->repository->findByOrganizationId(
@@ -89,7 +85,7 @@ class EventRepositoryTest extends AbstractApiRepositoryTestCase
     public function findParentsByOrganizationIdWithParams() {
         $organization_id = 1;
 
-        $expected_uri = "api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeStart%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&parent=1";
+        $expected_uri = "api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&parent=1";
         $this->injectClientFor($expected_uri);
 
         $actual = $this->repository->findParentsByOrganizationId(
@@ -127,7 +123,7 @@ class EventRepositoryTest extends AbstractApiRepositoryTestCase
     public function findChildrenByOrganizationIdWithParams() {
         $organization_id = 1;
 
-        $expected_uri = "api/public/events?_format=json&organizationId=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeStart%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&children=1";
+        $expected_uri = "api/public/events?_format=json&organizationId=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&children=1";
         $this->injectClientFor($expected_uri);
 
         $actual = $this->repository->findChildrenByOrganizationId(

+ 3 - 3
ot_core/Tests/Unit/Fixtures/ApiResponseFixtures.php

@@ -177,9 +177,9 @@ class ApiResponseFixtures
         'api/public/events?_format=json&organizationId=1&page=1&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&parent=1&itemsPerPage=8' => 'event',
         'api/public/events?_format=json&organizationId=1&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&children=1&itemsPerPage=8' => 'event',
         'api/public/events?_format=json&organizationId=1&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&filter%5Bwhere%5D%5Bid%5D=1&itemsPerPage=8' => 'event',
-        'api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeStart%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC' => 'event',
-        'api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeStart%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&parent=1' => 'event',
-        'api/public/events?_format=json&organizationId=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeStart%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&children=1' => 'event',
+        'api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC' => 'event',
+        'api/public/events?_format=json&organizationId=1&page=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&parent=1' => 'event',
+        'api/public/events?_format=json&organizationId=1&itemsPerPage=1&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Bgte%5D=2021-01-01T00%3A00%3A00%2B00%3A00&filter%5Bwhere%5D%5BdatetimeEnd%5D%5Blte%5D=2021-01-31T00%3A00%3A00%2B00%3A00&filter%5Border%5D%5B0%5D%5BdatetimeStart%5D=ASC&children=1' => 'event',
         'api/public/donors?_format=json&organizationId=1&page=1&itemsPerPage=99' => 'donor',
         'api/public/donors?_format=json&organizationId=1&parent=1&page=1&itemsPerPage=99' => 'donor',
         'api/public/members?_format=json&filter%5Bwhere%5D%5BorganizationId%5D=1&itemsPerPage=20000' => 'member',

Some files were not shown because too many files changed in this diff