Parcourir la source

implements a basic unittesting structure

Olivier Massot il y a 4 ans
Parent
commit
cb2ae16be0

+ 5 - 0
ot_core/.gitignore

@@ -0,0 +1,5 @@
+.Build/
+.idea/
+Build/testing-docker/.env
+composer.lock
+/var/

+ 7 - 0
ot_core/Readme.md

@@ -16,3 +16,10 @@ Cette extension fournit de nombreuses classes et ressources communes aux autres
 * Cette extension ne doit dépendre d'aucune autre pour fonctionner, c'est à dire que son fonctionnement ne doit 
 pas être modifié si aucune autre extension n'est installée
 * Cette extension est aussi la seule dont les autres extensions doivent pouvoir dépendre
+
+
+### Pour lancer les tests unitaires:
+
+Lancer dans la console:
+
+    .Build/bin/phpunit --coverage-clover=unittest-coverage.clover -c Tests/Build/UnitTests.xml

+ 27 - 0
ot_core/Tests/Build/UnitTests.xml

@@ -0,0 +1,27 @@
+<phpunit
+        backupGlobals="true"
+        backupStaticAttributes="false"
+        bootstrap="../../.Build/vendor/nimut/testing-framework/res/Configuration/UnitTestsBootstrap.php"
+        colors="true"
+        convertErrorsToExceptions="true"
+        convertWarningsToExceptions="false"
+        convertDeprecationsToExceptions="false"
+        forceCoversAnnotation="false"
+        processIsolation="false"
+        stopOnError="false"
+        stopOnFailure="false"
+        stopOnIncomplete="false"
+        stopOnSkipped="false"
+        verbose="false"
+>
+    <testsuites>
+        <testsuite name="Base tests">
+            <directory>../Unit</directory>
+        </testsuite>
+    </testsuites>
+
+    <php>
+        <!-- suppress deprecation warnings - set error reporting to: E_ALL & ~E_NOTICE & ~E_USER_ERROR & ~E_USER_WARNING & ~E_USER_NOTICE & ~E_STRICT & ~E_USER_DEPRECATED -->
+        <ini name="error_reporting" value="12535"/>
+    </php>
+</phpunit>

+ 81 - 0
ot_core/Tests/Unit/Page/OtPageRepositoryTest.php

@@ -0,0 +1,81 @@
+<?php
+
+use Opentalent\OtCore\Page\OtPageRepository;
+use Nimut\TestingFramework\TestCase\UnitTestCase;
+
+
+class OtPageRepositoryTest extends UnitTestCase
+{
+    protected $pageService;
+    protected $pageRepository;
+    protected $otPageRepository;
+
+    protected $rootLine = [
+        0 => ['uid' => 1, 'pid' => 0, 'title' => 'Folder', 'hidden' => 0, 'doktype' => 254, 'is_siteroot' => 0, 'nav_hide' => 0],
+        1 => ['uid' => 2, 'pid' => 1, 'title' => 'Rootpage', 'hidden' => 0, 'doktype' => 1, 'is_siteroot' => 1, 'nav_hide' => 0],
+        2 => ['uid' => 3, 'pid' => 2, 'title' => 'Page', 'hidden' => 0, 'doktype' => 1, 'is_siteroot' => 0, 'nav_hide' => 0],
+        3 => ['uid' => 4, 'pid' => 3, 'title' => 'Subpage', 'hidden' => 0, 'doktype' => 1, 'is_siteroot' => 0, 'nav_hide' => 0],
+    ];
+
+    public function setUp() {
+        $this->pageService = $this->prophesize(\FluidTYPO3\Vhs\Service\PageService::class);
+        $this->pageRepository = $this->prophesize(\TYPO3\CMS\Frontend\Page\PageRepository::class);
+        $this->otPageRepository = new OtPageRepository();
+    }
+
+    protected function injectProphecies() {
+        $this->otPageRepository->injectPageService($this->pageService->reveal());
+        $this->otPageRepository->injectPageRepository($this->pageRepository->reveal());
+    }
+
+    /**
+     * If the given page is a subpage of a root page, the the root page is returned
+     *
+     * @test
+     */
+    public function getRootPageForSiteSubpage() {
+
+        $this->pageService->getRootLine(4)->shouldBeCalled()->willReturn($this->rootLine);
+        $this->pageRepository->getPage(2)->shouldBeCalled()->willReturn($this->rootLine[1]);
+        $this->injectProphecies();
+
+        $this->assertEquals(
+            $this->rootLine[1],
+            $this->otPageRepository->getRootPageFor(4)
+        );
+    }
+
+    /**
+     * If the given page is already a root page, it is returned as it is
+     *
+     * @test
+     */
+    public function getRootPageForSiteRootpage() {
+
+        $this->pageService->getRootLine(2)->shouldBeCalled()->willReturn(array_slice($this->rootLine, 0, 2, true));
+        $this->pageRepository->getPage(2)->shouldBeCalled()->willReturn($this->rootLine[1]);
+        $this->injectProphecies();
+
+        $this->assertEquals(
+            $this->rootLine[1],
+            $this->otPageRepository->getRootPageFor(2)
+        );
+    }
+
+    /**
+     * If the given page is not a subpage of a root page, an empty array is returned
+     *
+     * @test
+     */
+    public function getRootPageForNonSitePage() {
+        $this->pageService->getRootLine(1)->shouldBeCalled()->willReturn(array_slice($this->rootLine, 0, 1, true));
+        $this->injectProphecies();
+
+        $this->assertEquals(
+            [],
+            $this->otPageRepository->getRootPageFor(1)
+        );
+    }
+
+
+}

+ 25 - 3
ot_core/composer.json

@@ -13,13 +13,18 @@
         "fluidtypo3/vhs": "^6",
         "fluidtypo3/flux": "^9",
         "fluidtypo3/fluidpages": "^5",
-        "co-stack/logs": "",
-        "guzzlehttp/guzzle": "^6"
+        "guzzlehttp/guzzle": "^6",
+        "nimut/typo3-complete": "9.5",
+        "nimut/testing-framework": "^5.2"
     },
     "replace": {
-        "ot_core": "self.version",
+        "opentalent/ot_core": "self.version",
         "typo3-ter/ot_core": "self.version"
     },
+    "config": {
+        "vendor-dir": ".Build/vendor",
+        "bin-dir": ".Build/bin"
+    },
     "autoload": {
         "psr-4": {
             "Opentalent\\OtCore\\": "Classes"
@@ -29,5 +34,22 @@
         "psr-4": {
             "Opentalent\\OtCore\\Tests\\": "Tests"
         }
+    },
+    "require-dev": {
+    },
+    "scripts": {
+        "post-autoload-dump": [
+            "@prepare-extension-test-structure"
+        ],
+        "prepare-extension-test-structure": [
+            "Nimut\\TestingFramework\\Composer\\ExtensionTestEnvironment::prepare"
+        ]
+    },
+    "extra": {
+        "typo3/cms": {
+            "cms-package-dir": "{$vendor-dir}/typo3/cms",
+            "web-dir": ".Build/Web",
+            "extension-key": "ot_core"
+        }
     }
 }