Browse Source

fix unit tests coverage and add new tests

Olivier Massot 3 years ago
parent
commit
fbda2f17e5

+ 27 - 35
phpunit.xml.dist

@@ -1,38 +1,30 @@
 <?xml version="1.0" encoding="UTF-8"?>
-
 <!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
-<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:noNamespaceSchemaLocation="bin/.phpunit/phpunit.xsd"
-         backupGlobals="false"
-         colors="true"
-         bootstrap="tests/bootstrap.php"
->
-    <php>
-        <ini name="error_reporting" value="-1" />
-        <server name="APP_ENV" value="test" force="true" />
-        <server name="SHELL_VERBOSITY" value="-1" />
-        <server name="SYMFONY_PHPUNIT_REMOVE" value="" />
-        <server name="SYMFONY_PHPUNIT_VERSION" value="8.5" />
-    </php>
-
-    <testsuites>
-        <testsuite name="Project Test Suite">
-            <directory>tests</directory>
-        </testsuite>
-    </testsuites>
-
-    <filter>
-        <whitelist processUncoveredFilesFromWhitelist="true">
-            <directory suffix=".php">src</directory>
-        </whitelist>
-    </filter>
-
-    <listeners>
-        <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
-    </listeners>
-
-    <logging>
-        <log type="coverage-html" target="./coverage" lowUpperBound="35" highLowerBound="70"/>
-        <log type="junit" target="./coverage/junit-report.xml"/>
-    </logging>
+<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="tests/bootstrap.php">
+  <coverage processUncoveredFiles="true">
+    <include>
+      <directory suffix=".php">src</directory>
+    </include>
+    <report>
+      <html outputDirectory="./coverage" lowUpperBound="35" highLowerBound="70"/>
+    </report>
+  </coverage>
+  <php>
+    <ini name="error_reporting" value="-1"/>
+    <server name="APP_ENV" value="test" force="true"/>
+    <server name="SHELL_VERBOSITY" value="-1"/>
+    <server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
+    <server name="SYMFONY_PHPUNIT_VERSION" value="9.4"/>
+  </php>
+  <testsuites>
+    <testsuite name="Project Test Suite">
+      <directory>tests</directory>
+    </testsuite>
+  </testsuites>
+  <listeners>
+    <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
+  </listeners>
+  <logging>
+    <junit outputFile="./coverage/junit-report.xml"/>
+  </logging>
 </phpunit>

+ 2 - 0
src/Service/ServiceIterator/CurrentAccessExtensionIterator.php

@@ -5,6 +5,7 @@ namespace App\Service\ServiceIterator;
 
 use App\Doctrine\Access\AccessExtensionInterface;
 use Doctrine\ORM\QueryBuilder;
+use Exception;
 
 class CurrentAccessExtensionIterator{
     public function __construct(private iterable $extensions)
@@ -16,5 +17,6 @@ class CurrentAccessExtensionIterator{
             if($extension->support($operationName))
                 return $extension->addWhere($queryBuilder);
         }
+        throw new Exception('no extension found for this operation');
     }
 }

+ 1 - 1
src/Service/ServiceIterator/OptionalsRolesIterator.php

@@ -6,7 +6,7 @@ namespace App\Service\ServiceIterator;
 use App\Entity\Access\Access;
 use App\Service\Access\OptionalsRolesInterface;
 
-class OptionalsRolesIterator{
+class OptionalsRolesIterator {
 
     public function __construct(private iterable $optionalsRoles)
     { }

+ 1 - 1
tests/Service/Dolibarr/DolibarrServiceTest.php

@@ -64,7 +64,7 @@ class DolibarrServiceTest extends TestCase
         $this->client
             ->expects($this->once())
             ->method('request')
-            ->with("GET", "contracts?limit=1&sqlfilters=statut%3D1&thirdparty_ids%3D1")
+            ->with("GET", "contracts?limit=1&sqlfilters=statut%3D1&thirdparty_ids=1")
             ->willReturn($response);
 
         $this->assertEquals(

+ 55 - 0
tests/Service/ServiceIterator/CurrentAccessExtensionIteratorTest.php

@@ -0,0 +1,55 @@
+<?php
+
+use App\Doctrine\Access\AccessExtensionInterface;
+use App\Service\ServiceIterator\CurrentAccessExtensionIterator;
+use Doctrine\ORM\QueryBuilder;
+use PHPUnit\Framework\TestCase;
+
+class CurrentAccessExtensionIteratorTest extends TestCase
+{
+    public function testAddWhere() {
+        $queryBuilder = $this->getMockBuilder(QueryBuilder::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $mocker = $this->getMockBuilder(AccessExtensionInterface::class);
+
+        $ext1 = $mocker->getMock();
+        $ext1->method('support')->willReturn(false);
+
+        $ext2 = $mocker->getMock();
+        $ext2->method('support')->with('foo')->willReturn(true);
+        $ext2->expects($this->once())->method('addWhere')->with($queryBuilder)->willReturn(true);
+
+        $ext3 = $mocker->getMock();
+        $ext3->method('support')->willReturn(false);
+
+        $extensions = [$ext1, $ext2, $ext3];
+
+        $iterator = new CurrentAccessExtensionIterator($extensions);
+        $actualExt = $iterator->addWhere($queryBuilder, 'foo');
+
+        $this->assertEquals(true, $actualExt);
+    }
+
+    public function testAddWhereError() {
+        $queryBuilder = $this->getMockBuilder(QueryBuilder::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $mocker = $this->getMockBuilder(AccessExtensionInterface::class);
+
+        $ext1 = $mocker->getMock();
+        $ext1->method('support')->willReturn(false);
+
+        $ext2 = $mocker->getMock();
+        $ext2->method('support')->willReturn(false);
+
+        $extensions = [$ext1, $ext2];
+
+        $iterator = new CurrentAccessExtensionIterator($extensions);
+
+        $this->expectException(Exception::class);
+        $iterator->addWhere($queryBuilder, 'foo');
+    }
+}

+ 45 - 0
tests/Service/ServiceIterator/EncoderIteratorTest.php

@@ -0,0 +1,45 @@
+<?php
+
+use App\Service\Export\Encoder\EncoderInterface;
+use App\Service\ServiceIterator\EncoderIterator;
+use PHPUnit\Framework\TestCase;
+
+class EncoderIteratorTest extends TestCase
+{
+    public function testGetEncoderFor() {
+        $mocker = $this->getMockBuilder(EncoderInterface::class);
+
+        $encoder1 = $mocker->getMock();
+        $encoder1->method('support')->willReturn(false);
+
+        $encoder2 = $mocker->getMock();
+        $encoder2->expects($this->once())->method('support')->with('pdf')->willReturn(true);
+
+        $encoder3 = $mocker->getMock();
+        $encoder3->method('support')->willReturn(false);
+
+        $encoders = [$encoder1, $encoder2, $encoder3];
+
+        $iterator = new EncoderIterator($encoders);
+        $actualEncoder = $iterator->getEncoderFor('pdf');
+
+        $this->assertEquals($encoder2, $actualEncoder);
+    }
+
+    public function testGetEncoderForError() {
+        $mocker = $this->getMockBuilder(EncoderInterface::class);
+
+        $encoder1 = $mocker->getMock();
+        $encoder1->method('support')->willReturn(false);
+
+        $encoder2 = $mocker->getMock();
+        $encoder2->method('support')->willReturn(false);
+
+        $encoders = [$encoder1, $encoder2];
+
+        $iterator = new EncoderIterator($encoders);
+
+        $this->expectException(Exception::class);
+        $iterator->getEncoderFor('gif');
+    }
+}

+ 56 - 0
tests/Service/ServiceIterator/ExporterIteratorTest.php

@@ -0,0 +1,56 @@
+<?php
+
+use App\ApiResources\Export\ExportRequest;
+use App\Service\Export\Encoder\EncoderInterface;
+use App\Service\Export\ExporterInterface;
+use App\Service\ServiceIterator\EncoderIterator;
+use App\Service\ServiceIterator\ExporterIterator;
+use PHPUnit\Framework\TestCase;
+
+class ExporterIteratorTest extends TestCase
+{
+    public function testGetExporterFor() {
+        $exportRequest = $this->getMockBuilder(ExportRequest::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $mocker = $this->getMockBuilder(ExporterInterface::class);
+
+        $exporter1 = $mocker->getMock();
+        $exporter1->method('support')->willReturn(false);
+
+        $exporter2 = $mocker->getMock();
+        $exporter2->expects($this->once())->method('support')->with($exportRequest)->willReturn(true);
+
+        $exporter3 = $mocker->getMock();
+        $exporter3->method('support')->willReturn(false);
+
+        $exporters = [$exporter1, $exporter2, $exporter3];
+
+        $iterator = new ExporterIterator($exporters);
+        $actualExporter = $iterator->getExporterFor($exportRequest);
+
+        $this->assertEquals($exporter2, $actualExporter);
+    }
+
+    public function testGetExporterForError() {
+        $exportRequest = $this->getMockBuilder(ExportRequest::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $mocker = $this->getMockBuilder(ExporterInterface::class);
+
+        $exporter1 = $mocker->getMock();
+        $exporter1->method('support')->willReturn(false);
+
+        $exporter2 = $mocker->getMock();
+        $exporter2->method('support')->willReturn(false);
+
+        $exporters = [$exporter1, $exporter2];
+
+        $iterator = new ExporterIterator($exporters);
+
+        $this->expectException(Exception::class);
+        $iterator->getExporterFor($exportRequest);
+    }
+}

+ 45 - 0
tests/Service/ServiceIterator/OptionalsRolesIteratorTest.php

@@ -0,0 +1,45 @@
+<?php
+
+use App\Entity\Access\Access;
+use App\Service\Access\OptionalsRolesInterface;
+use App\Service\ServiceIterator\OptionalsRolesIterator;
+use PHPUnit\Framework\TestCase;
+
+class OptionalsRolesIteratorTest extends TestCase
+{
+    public function testAddWhere() {
+        $access = $this->getMockBuilder(Access::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $mocker = $this->getMockBuilder(OptionalsRolesInterface::class);
+
+        $role1 = $mocker->getMock();
+        $role1->method('support')->willReturn(false);
+
+        $role2 = $mocker->getMock();
+        $role2->method('support')->with($access)->willReturn(true);
+        $role2->expects($this->once())->method('getRole')->willReturn('ROLE2');
+
+        $role3 = $mocker->getMock();
+        $role3->method('support')->willReturn(false);
+
+        $role4 = $mocker->getMock();
+        $role4->method('support')->with($access)->willReturn(true);
+        $role4->expects($this->once())->method('getRole')->willReturn('ROLE4');
+
+        $roles = [$role1, $role2, $role3, $role4];
+
+        $iterator = new OptionalsRolesIterator($roles);
+        $actualRoles = $iterator->getOptionalsRoles($access);
+
+        $this->assertEquals(['ROLE2', 'ROLE4'], $actualRoles);
+
+        $roles = [$role1, $role3];
+
+        $iterator = new OptionalsRolesIterator($roles);
+        $actualRoles = $iterator->getOptionalsRoles($access);
+
+        $this->assertEquals([], $actualRoles);
+    }
+}

+ 34 - 0
tests/Service/Storage/TemporaryFileStorageTest.php

@@ -0,0 +1,34 @@
+<?php
+
+use App\Service\Storage\TemporaryFileStorage;
+use Gaufrette\Adapter\Local;
+use Gaufrette\Filesystem;
+use Knp\Bundle\GaufretteBundle\FilesystemMap;
+use PHPUnit\Framework\TestCase;
+
+class TemporaryFileStorageTest extends TestCase
+{
+    public function testGetStorageBaseDir() {
+        $fileSystemMap = $this->getMockBuilder(FilesystemMap::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $fileSystem = $this->getMockBuilder(Filesystem::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $adapter = $this->getMockBuilder(Local::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $fileSystemMap->method('get')->willReturn($fileSystem);
+        $fileSystem->method('getAdapter')->willReturn($adapter);
+        $adapter->expects($this->once())->method('write')->willReturnSelf();
+
+        $storage = new TemporaryFileStorage($fileSystemMap);
+        $path = $storage->write('my_file.txt', 'some content');
+
+        $this->assertMatchesRegularExpression(
+            '/temp\/\d{8}_\d{6}_[\w\d]{8}\/my_file.txt/',
+            $path
+        );
+    }
+}

+ 31 - 0
tests/Service/Utils/PathTest.php

@@ -0,0 +1,31 @@
+<?php
+namespace App\Tests\Service\Utils;
+
+use App\Service\Utils\Path;
+use PHPUnit\Framework\TestCase;
+
+class PathTest extends TestCase
+{
+    /**
+     * @see Path::getProjectDir()
+     */
+    public function testGetProjectDir():void
+    {
+        $this->assertFileExists(Path::getProjectDir() . '/phpunit.xml.dist');
+    }
+
+    /**
+     * @see Path::join()
+     */
+    public function testJoin(): void {
+        $this->assertEquals("", Path::join("", ""));
+        $this->assertEquals("/", Path::join("", "/"));
+        $this->assertEquals("/a", Path::join("/", "a"));
+        $this->assertEquals("/a", Path::join("/", "/a"));
+        $this->assertEquals("abc/def", Path::join("abc", "def"));
+        $this->assertEquals("abc/def", Path::join("abc", "/def"));
+        $this->assertEquals("/abc/def", Path::join("/abc", "def"));
+        $this->assertEquals("foo.jpg", Path::join("", "foo.jpg"));
+        $this->assertEquals("dir/0/a.jpg", Path::join("dir", "0", "a.jpg"));
+    }
+}

+ 10 - 1
tests/Service/Utils/StringsUtilsTest.php

@@ -13,4 +13,13 @@ class StringsUtilsTest extends TestCase
     {
         $this->assertEquals("foo", StringsUtils::unquote("'foo"));
     }
-}
+
+    /**
+     * @see StringsUtils::camelToSnake()
+     */
+    public function testCamelToSnake(): void {
+        $this->assertEquals("foo_bar", StringsUtils::camelToSnake("FooBar"));
+        $this->assertEquals("foo-bar", StringsUtils::camelToSnake("FooBar", '-'));
+        $this->assertEquals("foo_bar", StringsUtils::camelToSnake("fooBar"));
+    }
+}