浏览代码

add unit tests for exporter and encoder services

Olivier Massot 3 年之前
父节点
当前提交
7d7c8ac351
共有 2 个文件被更改,包括 196 次插入2 次删除
  1. 34 1
      tests/Service/Export/Encoder/PdfEncoderTest.php
  2. 162 1
      tests/Service/Export/LicenceCmfExporterTest.php

+ 34 - 1
tests/Service/Export/Encoder/PdfEncoderTest.php

@@ -1,6 +1,39 @@
 <?php
 
-class PdfEncoderTest
+use App\Service\Export\Encoder\PdfEncoder;
+use Knp\Snappy\Pdf;
+use PHPUnit\Framework\TestCase;
+
+class PdfEncoderTest extends TestCase
 {
+    public function testSupport() {
+        $mocker = $this->getMockBuilder(Pdf::class);
+        $knpSnappy = $mocker->getMock();
+        $encoder = new PdfEncoder($knpSnappy);
+
+        $this->assertTrue($encoder->support('pdf'));
+        $this->assertFalse($encoder->support('txt'));
+    }
+
+    public function testGetDefaultOptions() {
+        $mocker = $this->getMockBuilder(Pdf::class);
+        $knpSnappy = $mocker->getMock();
+        $encoder = new PdfEncoder($knpSnappy);
+
+        $this->assertIsArray($encoder->getDefaultOptions());
+    }
+
+    public function testEncode() {
+        $mocker = $this->getMockBuilder(Pdf::class);
+        $knpSnappy = $mocker->getMock();
+        $knpSnappy
+            ->expects(self::once())
+            ->method('getOutputFromHtml')
+            ->with('<div>content</div>')
+            ->willReturn('%%encoded%%');
+
+        $encoder = new PdfEncoder($knpSnappy);
 
+        $this->assertEquals('%%encoded%%', $encoder->encode('<div>content</div>'));
+    }
 }

+ 162 - 1
tests/Service/Export/LicenceCmfExporterTest.php

@@ -1,6 +1,167 @@
 <?php
 
-class LicenceCmfExporterTest
+use App\ApiResources\Export\ExportRequest;
+use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
+use App\Entity\Access\Access;
+use App\Entity\Core\File;
+use App\Entity\Network\Network;
+use App\Entity\Network\NetworkOrganization;
+use App\Entity\Organization\Organization;
+use App\Entity\Organization\Parameters;
+use App\Entity\Person\Person;
+use App\Repository\Access\AccessRepository;
+use App\Repository\Organization\OrganizationRepository;
+use App\Service\Export\Encoder\PdfEncoder;
+use App\Service\Export\LicenceCmfExporter;
+use App\Service\ServiceIterator\EncoderIterator;
+use App\Service\Storage\TemporaryFileStorage;
+use App\Service\Storage\UploadStorage;
+use Doctrine\Common\Collections\Collection;
+use Doctrine\ORM\EntityManagerInterface;
+use PHPUnit\Framework\TestCase;
+use Twig\Environment;
+
+class LicenceCmfExporterTest extends TestCase
 {
+    private mixed $exportRequest;
+    private mixed $accessRepo;
+    private mixed $twig;
+    private mixed $encoderIterator;
+    private mixed $em;
+    private mixed $storage;
+    private mixed $organizationRepo;
+    private mixed $uploadStorage;
+    private mixed $access;
+    private mixed $organization;
+    private mixed $cmf;
+    private mixed $networkOrgs;
+    private mixed $collection;
+    private mixed $parent;
+    private mixed $logo;
+    private mixed $presidentAccess;
+    private mixed $president;
+    private mixed $cmfParameters;
+    private mixed $qrCode;
+    private LicenceCmfExporter $exporter;
+    private mixed $encoder;
+
+    public function setUp(): void {
+        $this->exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
+        $this->accessRepo = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
+        $this->twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
+        $this->encoderIterator = $this->getMockBuilder(EncoderIterator::class)->disableOriginalConstructor()->getMock();
+        $this->encoder = $this->getMockBuilder(PdfEncoder::class)->disableOriginalConstructor()->getMock();
+        $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
+        $this->storage = $this->getMockBuilder(TemporaryFileStorage::class)->disableOriginalConstructor()->getMock();
+        $this->organizationRepo = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
+        $this->uploadStorage = $this->getMockBuilder(UploadStorage::class)->disableOriginalConstructor()->getMock();
+        $this->access = $this->getMockBuilder(Access::class)->getMock();
+        $this->organization = $this->getMockBuilder(Organization::class)->getMock();
+        $this->cmf = $this->getMockBuilder(Organization::class)->getMock();
+        $this->networkOrgs = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $this->collection = $this->getMockBuilder(Collection::class)->getMock();
+        $this->parent = $this->getMockBuilder(Organization::class)->getMock();
+        $this->logo = $this->getMockBuilder(File::class)->getMock();
+        $this->presidentAccess = $this->getMockBuilder(Access::class)->getMock();
+        $this->president = $this->getMockBuilder(Person::class)->getMock();
+        $this->cmfParameters = $this->getMockBuilder(Parameters::class)->getMock();
+        $this->qrCode = $this->getMockBuilder(File::class)->getMock();
+
+        $this->exporter = new LicenceCmfExporter($this->organizationRepo, $this->uploadStorage);
+        $this->exporter->setAccessRepository($this->accessRepo);
+        $this->exporter->setTwig($this->twig);
+        $this->exporter->setEncoderIterator($this->encoderIterator);
+        $this->exporter->setEntityManager($this->em);
+        $this->exporter->setStorage($this->storage);
+    }
+
+    public function testSupport() {
+        $unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock();
+
+        $this->assertTrue($this->exporter->support($this->exportRequest));
+        $this->assertFalse($this->exporter->support($unsupportedExportRequest));
+    }
+
+    private function prepareModelBuilding() {
+        $this->exportRequest->method('getRequesterId')->willReturn(1);
+        $this->exportRequest->method('getYear')->willReturn(2020);
+        $this->exportRequest->method('getFormat')->willReturn('pdf');
+
+        $this->accessRepo->method('find')->with(1)->willReturn($this->access);
+        $this->access->method('getOrganization')->willReturn($this->organization);
+        $this->organization->method('getId')->willReturn(1);
+        $this->organization->method('getName')->willReturn('my_organization');
+        $this->organization->method('getIdentifier')->willReturn('org1');
+        $this->organization->method('getNetworkOrganizations')->willReturn($this->collection);
+        $this->collection->method('get')->willReturn($this->networkOrgs);
+        $this->networkOrgs->expects(self::once())->method('getParent')->willReturn($this->parent);
+        $this->parent->expects(self::once())->method('getName')->willReturn('my_network');
+        $this->organization->method('getLogo')->willReturn($this->logo);
+        $this->logo->method('getId')->willReturn(1);
+        $this->uploadStorage->method('getUri')->willReturn('http:://foo.bar/1');
+        $this->president->method('getId')->willReturn(1);
+        $this->president->method('getGender')->willReturn('M');
+        $this->president->method('getGivenName')->willReturn('Joe');
+        $this->president->method('getName')->willReturn('Dalton');
+        $this->accessRepo
+            ->expects(self::once())
+            ->method('findByOrganizationAndMission')
+            ->with($this->organization, 'PRESIDENT')
+            ->willReturn([$this->presidentAccess]);
+        $this->presidentAccess->method('getPerson')->willReturn($this->president);
+        $this->cmf->method('getParameters')->willReturn($this->cmfParameters);
+        $this->cmfParameters->expects(self::once())->method('getQrCode')->willReturn($this->qrCode);
+        $this->qrCode->method('getId')->willReturn(1);
+        $this->organizationRepo->expects(self::once())->method('find')->with(12097)->willReturn($this->cmf);
+    }
+
+    public function testBuildModel() {
+        $this->prepareModelBuilding();
+
+        $reflection = new ReflectionClass('App\Service\Export\LicenceCmfExporter');
+        $this->buildModelMethod = $reflection->getMethod('buildModel');
+        $this->buildModelMethod->setAccessible(true);
+
+        $modelCollection = $this->buildModelMethod->invokeArgs($this->exporter, [$this->exportRequest]);
+        $licence = $modelCollection->getLicences()[0];
+
+        $this->assertEquals('my_network', $licence->getFederationName());
+        $this->assertEquals(true, $licence->isOrganizationLicence());
+        $this->assertEquals(2020, $licence->getYear());
+        $this->assertEquals('931572', $licence->getColor());
+        $this->assertEquals('http:://foo.bar/1', $licence->getQrCodeUri());
+        $this->assertEquals('http:://foo.bar/1', $licence->getLogoUri());
+    }
+
+    public function testExport() {
+        $this->prepareModelBuilding();
+
+        $this->twig
+            ->expects(self::once())
+            ->method('render')
+            ->with('@templates/export/licence_cmf.html.twig')
+            ->willReturn('<div>rendered html</div>');
+
+        $this->encoderIterator
+            ->expects(self::once())
+            ->method('getEncoderFor')
+            ->with('pdf')
+            ->willReturn($this->encoder);
+
+        $this->encoder
+            ->expects(self::once())
+            ->method('encode')
+            ->with('<div>rendered html</div>')
+            ->willReturn('%%encoded%%');
+
+        $this->storage
+            ->expects(self::once())
+            ->method('write')
+            ->willReturn('/temp/abcd/licence_cmf_2020.pdf');
+
+        $file = $this->exporter->export($this->exportRequest);
+
+        $this->assertMatchesRegularExpression('/licence_cmf_\d{4}.pdf/', $file->getName());
+    }
 
 }