|
@@ -1,4 +1,4 @@
|
|
|
-<?php
|
|
|
|
|
|
|
+<?php /** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
|
|
|
|
use App\ApiResources\Export\ExportRequest;
|
|
use App\ApiResources\Export\ExportRequest;
|
|
|
use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
|
|
use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
|
|
@@ -13,37 +13,43 @@ use App\Repository\Access\AccessRepository;
|
|
|
use App\Repository\Organization\OrganizationRepository;
|
|
use App\Repository\Organization\OrganizationRepository;
|
|
|
use App\Service\Export\Encoder\PdfEncoder;
|
|
use App\Service\Export\Encoder\PdfEncoder;
|
|
|
use App\Service\Export\LicenceCmfExporter;
|
|
use App\Service\Export\LicenceCmfExporter;
|
|
|
|
|
+use App\Service\Export\Model\ExportModelInterface;
|
|
|
|
|
+use App\Service\Export\Model\LicenceCmf;
|
|
|
|
|
+use App\Service\Export\Model\LicenceCmfCollection;
|
|
|
use App\Service\ServiceIterator\EncoderIterator;
|
|
use App\Service\ServiceIterator\EncoderIterator;
|
|
|
use App\Service\Storage\TemporaryFileStorage;
|
|
use App\Service\Storage\TemporaryFileStorage;
|
|
|
use App\Service\Storage\UploadStorage;
|
|
use App\Service\Storage\UploadStorage;
|
|
|
|
|
+use App\Tests\TestToolsTrait;
|
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
+use PHPUnit\Framework\MockObject\MockObject;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
use Twig\Environment;
|
|
use Twig\Environment;
|
|
|
|
|
|
|
|
class LicenceCmfExporterTest extends TestCase
|
|
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;
|
|
|
|
|
|
|
+ use TestToolsTrait;
|
|
|
|
|
+
|
|
|
|
|
+ private MockObject | LicenceCmfOrganizationER $exportRequest;
|
|
|
|
|
+ private MockObject | AccessRepository $accessRepo;
|
|
|
|
|
+ private MockObject | Environment $twig;
|
|
|
|
|
+ private MockObject | EncoderIterator $encoderIterator;
|
|
|
|
|
+ private MockObject | EntityManagerInterface $em;
|
|
|
|
|
+ private MockObject | TemporaryFileStorage $storage;
|
|
|
|
|
+ private MockObject | OrganizationRepository $organizationRepo;
|
|
|
|
|
+ private MockObject | UploadStorage $uploadStorage;
|
|
|
|
|
+ private MockObject | Access $access;
|
|
|
|
|
+ private MockObject | Organization $organization;
|
|
|
|
|
+ private MockObject | Organization $cmf;
|
|
|
|
|
+ private MockObject | NetworkOrganization $networkOrgs;
|
|
|
|
|
+ private MockObject | Collection $collection;
|
|
|
|
|
+ private MockObject | Organization $parent;
|
|
|
|
|
+ private MockObject | File $logo;
|
|
|
|
|
+ private MockObject | Access $presidentAccess;
|
|
|
|
|
+ private MockObject | Person $president;
|
|
|
|
|
+ private MockObject | Parameters $cmfParameters;
|
|
|
|
|
+ private MockObject | File $qrCode;
|
|
|
|
|
+ private MockObject | PdfEncoder $encoder;
|
|
|
|
|
|
|
|
public function setUp(): void {
|
|
public function setUp(): void {
|
|
|
$this->exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
|
|
$this->exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
|
|
@@ -66,20 +72,33 @@ class LicenceCmfExporterTest extends TestCase
|
|
|
$this->president = $this->getMockBuilder(Person::class)->getMock();
|
|
$this->president = $this->getMockBuilder(Person::class)->getMock();
|
|
|
$this->cmfParameters = $this->getMockBuilder(Parameters::class)->getMock();
|
|
$this->cmfParameters = $this->getMockBuilder(Parameters::class)->getMock();
|
|
|
$this->qrCode = $this->getMockBuilder(File::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);
|
|
|
|
|
|
|
+ private function makeExporterMock(string $methodUnderTest): LicenceCmfExporter | MockObject
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->getMockBuilder(LicenceCmfExporter::class)
|
|
|
|
|
+ ->setConstructorArgs([$this->organizationRepo, $this->uploadStorage])
|
|
|
|
|
+ ->setMethodsExcept(['setAccessRepository', 'setTwig', 'setEncoderIterator',
|
|
|
|
|
+ 'setEntityManager', 'setEntityManager', 'setStorage', $methodUnderTest])
|
|
|
|
|
+ ->getMock();
|
|
|
|
|
+
|
|
|
|
|
+ $exporter->setAccessRepository($this->accessRepo);
|
|
|
|
|
+ $exporter->setTwig($this->twig);
|
|
|
|
|
+ $exporter->setEncoderIterator($this->encoderIterator);
|
|
|
|
|
+ $exporter->setEntityManager($this->em);
|
|
|
|
|
+ $exporter->setStorage($this->storage);
|
|
|
|
|
+
|
|
|
|
|
+ return $exporter;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function testSupport() {
|
|
|
|
|
|
|
+ public function testSupport(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('support');
|
|
|
|
|
+
|
|
|
$unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock();
|
|
$unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
- $this->assertTrue($this->exporter->support($this->exportRequest));
|
|
|
|
|
- $this->assertFalse($this->exporter->support($unsupportedExportRequest));
|
|
|
|
|
|
|
+ $this->assertTrue($exporter->support($this->exportRequest));
|
|
|
|
|
+ $this->assertFalse($exporter->support($unsupportedExportRequest));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private function prepareModelBuilding() {
|
|
private function prepareModelBuilding() {
|
|
@@ -115,14 +134,14 @@ class LicenceCmfExporterTest extends TestCase
|
|
|
$this->organizationRepo->expects(self::once())->method('find')->with(12097)->willReturn($this->cmf);
|
|
$this->organizationRepo->expects(self::once())->method('find')->with(12097)->willReturn($this->cmf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function testBuildModel() {
|
|
|
|
|
|
|
+ public function testBuildModel(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('buildModel');
|
|
|
|
|
+
|
|
|
$this->prepareModelBuilding();
|
|
$this->prepareModelBuilding();
|
|
|
|
|
|
|
|
- $reflection = new ReflectionClass('App\Service\Export\LicenceCmfExporter');
|
|
|
|
|
- $this->buildModelMethod = $reflection->getMethod('buildModel');
|
|
|
|
|
- $this->buildModelMethod->setAccessible(true);
|
|
|
|
|
|
|
+ $modelCollection = $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
|
|
|
|
|
|
|
|
- $modelCollection = $this->buildModelMethod->invokeArgs($this->exporter, [$this->exportRequest]);
|
|
|
|
|
$licence = $modelCollection->getLicences()[0];
|
|
$licence = $modelCollection->getLicences()[0];
|
|
|
|
|
|
|
|
$this->assertEquals('my_network', $licence->getFederationName());
|
|
$this->assertEquals('my_network', $licence->getFederationName());
|
|
@@ -133,35 +152,91 @@ class LicenceCmfExporterTest extends TestCase
|
|
|
$this->assertEquals('http:://foo.bar/1', $licence->getLogoUri());
|
|
$this->assertEquals('http:://foo.bar/1', $licence->getLogoUri());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function testExport() {
|
|
|
|
|
- $this->prepareModelBuilding();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * If the organization cannot be determined from the exportRequest author, an error should be thrown
|
|
|
|
|
+ */
|
|
|
|
|
+ public function testBuildModelWithoutOrganization(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('buildModel');
|
|
|
|
|
|
|
|
- $this->twig
|
|
|
|
|
- ->expects(self::once())
|
|
|
|
|
- ->method('render')
|
|
|
|
|
- ->with('@templates/export/licence_cmf.html.twig')
|
|
|
|
|
- ->willReturn('<div>rendered html</div>');
|
|
|
|
|
|
|
+ $this->exportRequest->method('getRequesterId')->willReturn(1);
|
|
|
|
|
+ $this->accessRepo->method('find')->with(1)->willReturn(null);
|
|
|
|
|
|
|
|
- $this->encoderIterator
|
|
|
|
|
- ->expects(self::once())
|
|
|
|
|
- ->method('getEncoderFor')
|
|
|
|
|
- ->with('pdf')
|
|
|
|
|
- ->willReturn($this->encoder);
|
|
|
|
|
|
|
+ $this->expectException(RuntimeException::class);
|
|
|
|
|
|
|
|
- $this->encoder
|
|
|
|
|
- ->expects(self::once())
|
|
|
|
|
- ->method('encode')
|
|
|
|
|
- ->with('<div>rendered html</div>')
|
|
|
|
|
- ->willReturn('%%encoded%%');
|
|
|
|
|
|
|
+ $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $this->storage
|
|
|
|
|
- ->expects(self::once())
|
|
|
|
|
- ->method('write')
|
|
|
|
|
- ->willReturn('/temp/abcd/licence_cmf_2020.pdf');
|
|
|
|
|
|
|
+ public function testGetFileBasename(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('getFileBasename');
|
|
|
|
|
+
|
|
|
|
|
+ $licence = $this->getMockBuilder(LicenceCmf::class)->getMock();
|
|
|
|
|
+ $licence->method('getYear')->willReturn(2020);
|
|
|
|
|
+
|
|
|
|
|
+ $model = $this->getMockBuilder(LicenceCmfCollection::class)->getMock();
|
|
|
|
|
+ $model->method('getLicences')->willReturn([$licence]);
|
|
|
|
|
+
|
|
|
|
|
+ $result = $this->invokeMethod($exporter, 'getFileBasename', [$model]);
|
|
|
|
|
|
|
|
- $file = $this->exporter->export($this->exportRequest);
|
|
|
|
|
|
|
+ $this->assertEquals(
|
|
|
|
|
+ 'licence_cmf_2020.pdf',
|
|
|
|
|
+ $result
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function testGetFileType(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('getFileType');
|
|
|
|
|
|
|
|
- $this->assertMatchesRegularExpression('/licence_cmf_\d{4}.pdf/', $file->getName());
|
|
|
|
|
|
|
+ $this->assertEquals(
|
|
|
|
|
+ 'LICENCE_CMF',
|
|
|
|
|
+ $this->invokeMethod($exporter, 'getFileType')
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Given year is before the start year, first color is used
|
|
|
|
|
+ */
|
|
|
|
|
+ public function testGetLicenceColorPastYear(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('getLicenceColor');
|
|
|
|
|
+
|
|
|
|
|
+ $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR - 10;
|
|
|
|
|
+
|
|
|
|
|
+ $this->assertEquals(
|
|
|
|
|
+ LicenceCmfExporter::LICENCE_CMF_COLOR[0],
|
|
|
|
|
+ $this->invokeMethod($exporter, 'getLicenceColor', [$year])
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Given year is within the 5 first years
|
|
|
|
|
+ */
|
|
|
|
|
+ public function testGetLicenceColorFirstYears(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('getLicenceColor');
|
|
|
|
|
+
|
|
|
|
|
+ $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 3;
|
|
|
|
|
+
|
|
|
|
|
+ $this->assertEquals(
|
|
|
|
|
+ LicenceCmfExporter::LICENCE_CMF_COLOR[3],
|
|
|
|
|
+ $this->invokeMethod($exporter, 'getLicenceColor', [$year])
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Given year is beyond the 5 first years
|
|
|
|
|
+ */
|
|
|
|
|
+ public function testGetLicenceColorNExtYears(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $exporter = $this->makeExporterMock('getLicenceColor');
|
|
|
|
|
+
|
|
|
|
|
+ $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 8;
|
|
|
|
|
+
|
|
|
|
|
+ $this->assertEquals(
|
|
|
|
|
+ LicenceCmfExporter::LICENCE_CMF_COLOR[3],
|
|
|
|
|
+ $this->invokeMethod($exporter, 'getLicenceColor', [$year])
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|