LicenceCmfExporterTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. use App\ApiResources\Export\ExportRequest;
  3. use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Core\File;
  6. use App\Entity\Network\Network;
  7. use App\Entity\Network\NetworkOrganization;
  8. use App\Entity\Organization\Organization;
  9. use App\Entity\Organization\Parameters;
  10. use App\Entity\Person\Person;
  11. use App\Repository\Access\AccessRepository;
  12. use App\Repository\Organization\OrganizationRepository;
  13. use App\Service\Export\Encoder\PdfEncoder;
  14. use App\Service\Export\LicenceCmfExporter;
  15. use App\Service\ServiceIterator\EncoderIterator;
  16. use App\Service\Storage\TemporaryFileStorage;
  17. use App\Service\Storage\UploadStorage;
  18. use Doctrine\Common\Collections\Collection;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use PHPUnit\Framework\TestCase;
  21. use Twig\Environment;
  22. class LicenceCmfExporterTest extends TestCase
  23. {
  24. private mixed $exportRequest;
  25. private mixed $accessRepo;
  26. private mixed $twig;
  27. private mixed $encoderIterator;
  28. private mixed $em;
  29. private mixed $storage;
  30. private mixed $organizationRepo;
  31. private mixed $uploadStorage;
  32. private mixed $access;
  33. private mixed $organization;
  34. private mixed $cmf;
  35. private mixed $networkOrgs;
  36. private mixed $collection;
  37. private mixed $parent;
  38. private mixed $logo;
  39. private mixed $presidentAccess;
  40. private mixed $president;
  41. private mixed $cmfParameters;
  42. private mixed $qrCode;
  43. private LicenceCmfExporter $exporter;
  44. private mixed $encoder;
  45. public function setUp(): void {
  46. $this->exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
  47. $this->accessRepo = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  48. $this->twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
  49. $this->encoderIterator = $this->getMockBuilder(EncoderIterator::class)->disableOriginalConstructor()->getMock();
  50. $this->encoder = $this->getMockBuilder(PdfEncoder::class)->disableOriginalConstructor()->getMock();
  51. $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  52. $this->storage = $this->getMockBuilder(TemporaryFileStorage::class)->disableOriginalConstructor()->getMock();
  53. $this->organizationRepo = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
  54. $this->uploadStorage = $this->getMockBuilder(UploadStorage::class)->disableOriginalConstructor()->getMock();
  55. $this->access = $this->getMockBuilder(Access::class)->getMock();
  56. $this->organization = $this->getMockBuilder(Organization::class)->getMock();
  57. $this->cmf = $this->getMockBuilder(Organization::class)->getMock();
  58. $this->networkOrgs = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  59. $this->collection = $this->getMockBuilder(Collection::class)->getMock();
  60. $this->parent = $this->getMockBuilder(Organization::class)->getMock();
  61. $this->logo = $this->getMockBuilder(File::class)->getMock();
  62. $this->presidentAccess = $this->getMockBuilder(Access::class)->getMock();
  63. $this->president = $this->getMockBuilder(Person::class)->getMock();
  64. $this->cmfParameters = $this->getMockBuilder(Parameters::class)->getMock();
  65. $this->qrCode = $this->getMockBuilder(File::class)->getMock();
  66. $this->exporter = new LicenceCmfExporter($this->organizationRepo, $this->uploadStorage);
  67. $this->exporter->setAccessRepository($this->accessRepo);
  68. $this->exporter->setTwig($this->twig);
  69. $this->exporter->setEncoderIterator($this->encoderIterator);
  70. $this->exporter->setEntityManager($this->em);
  71. $this->exporter->setStorage($this->storage);
  72. }
  73. public function testSupport() {
  74. $unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock();
  75. $this->assertTrue($this->exporter->support($this->exportRequest));
  76. $this->assertFalse($this->exporter->support($unsupportedExportRequest));
  77. }
  78. private function prepareModelBuilding() {
  79. $this->exportRequest->method('getRequesterId')->willReturn(1);
  80. $this->exportRequest->method('getYear')->willReturn(2020);
  81. $this->exportRequest->method('getFormat')->willReturn('pdf');
  82. $this->accessRepo->method('find')->with(1)->willReturn($this->access);
  83. $this->access->method('getOrganization')->willReturn($this->organization);
  84. $this->organization->method('getId')->willReturn(1);
  85. $this->organization->method('getName')->willReturn('my_organization');
  86. $this->organization->method('getIdentifier')->willReturn('org1');
  87. $this->organization->method('getNetworkOrganizations')->willReturn($this->collection);
  88. $this->collection->method('get')->willReturn($this->networkOrgs);
  89. $this->networkOrgs->expects(self::once())->method('getParent')->willReturn($this->parent);
  90. $this->parent->expects(self::once())->method('getName')->willReturn('my_network');
  91. $this->organization->method('getLogo')->willReturn($this->logo);
  92. $this->logo->method('getId')->willReturn(1);
  93. $this->uploadStorage->method('getUri')->willReturn('http:://foo.bar/1');
  94. $this->president->method('getId')->willReturn(1);
  95. $this->president->method('getGender')->willReturn('M');
  96. $this->president->method('getGivenName')->willReturn('Joe');
  97. $this->president->method('getName')->willReturn('Dalton');
  98. $this->accessRepo
  99. ->expects(self::once())
  100. ->method('findByOrganizationAndMission')
  101. ->with($this->organization, 'PRESIDENT')
  102. ->willReturn([$this->presidentAccess]);
  103. $this->presidentAccess->method('getPerson')->willReturn($this->president);
  104. $this->cmf->method('getParameters')->willReturn($this->cmfParameters);
  105. $this->cmfParameters->expects(self::once())->method('getQrCode')->willReturn($this->qrCode);
  106. $this->qrCode->method('getId')->willReturn(1);
  107. $this->organizationRepo->expects(self::once())->method('find')->with(12097)->willReturn($this->cmf);
  108. }
  109. public function testBuildModel() {
  110. $this->prepareModelBuilding();
  111. $reflection = new ReflectionClass('App\Service\Export\LicenceCmfExporter');
  112. $this->buildModelMethod = $reflection->getMethod('buildModel');
  113. $this->buildModelMethod->setAccessible(true);
  114. $modelCollection = $this->buildModelMethod->invokeArgs($this->exporter, [$this->exportRequest]);
  115. $licence = $modelCollection->getLicences()[0];
  116. $this->assertEquals('my_network', $licence->getFederationName());
  117. $this->assertEquals(true, $licence->isOrganizationLicence());
  118. $this->assertEquals(2020, $licence->getYear());
  119. $this->assertEquals('931572', $licence->getColor());
  120. $this->assertEquals('http:://foo.bar/1', $licence->getQrCodeUri());
  121. $this->assertEquals('http:://foo.bar/1', $licence->getLogoUri());
  122. }
  123. public function testExport() {
  124. $this->prepareModelBuilding();
  125. $this->twig
  126. ->expects(self::once())
  127. ->method('render')
  128. ->with('@templates/export/licence_cmf.html.twig')
  129. ->willReturn('<div>rendered html</div>');
  130. $this->encoderIterator
  131. ->expects(self::once())
  132. ->method('getEncoderFor')
  133. ->with('pdf')
  134. ->willReturn($this->encoder);
  135. $this->encoder
  136. ->expects(self::once())
  137. ->method('encode')
  138. ->with('<div>rendered html</div>')
  139. ->willReturn('%%encoded%%');
  140. $this->storage
  141. ->expects(self::once())
  142. ->method('write')
  143. ->willReturn('/temp/abcd/licence_cmf_2020.pdf');
  144. $file = $this->exporter->export($this->exportRequest);
  145. $this->assertMatchesRegularExpression('/licence_cmf_\d{4}.pdf/', $file->getName());
  146. }
  147. }