LicenceCmfExporterTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  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\Export\Model\ExportModelInterface;
  16. use App\Service\Export\Model\LicenceCmf;
  17. use App\Service\Export\Model\LicenceCmfCollection;
  18. use App\Service\ServiceIterator\EncoderIterator;
  19. use App\Service\Storage\TemporaryFileStorage;
  20. use App\Service\Storage\UploadStorage;
  21. use App\Tests\TestToolsTrait;
  22. use Doctrine\Common\Collections\Collection;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use PHPUnit\Framework\MockObject\MockObject;
  25. use PHPUnit\Framework\TestCase;
  26. use Twig\Environment;
  27. class LicenceCmfExporterTest extends TestCase
  28. {
  29. use TestToolsTrait;
  30. private MockObject | LicenceCmfOrganizationER $exportRequest;
  31. private MockObject | AccessRepository $accessRepo;
  32. private MockObject | Environment $twig;
  33. private MockObject | EncoderIterator $encoderIterator;
  34. private MockObject | EntityManagerInterface $em;
  35. private MockObject | TemporaryFileStorage $storage;
  36. private MockObject | OrganizationRepository $organizationRepo;
  37. private MockObject | UploadStorage $uploadStorage;
  38. private MockObject | Access $access;
  39. private MockObject | Organization $organization;
  40. private MockObject | Organization $cmf;
  41. private MockObject | NetworkOrganization $networkOrgs;
  42. private MockObject | Collection $collection;
  43. private MockObject | Organization $parent;
  44. private MockObject | File $logo;
  45. private MockObject | Access $presidentAccess;
  46. private MockObject | Person $president;
  47. private MockObject | Parameters $cmfParameters;
  48. private MockObject | File $qrCode;
  49. private MockObject | PdfEncoder $encoder;
  50. public function setUp(): void {
  51. $this->exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
  52. $this->accessRepo = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  53. $this->twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
  54. $this->encoderIterator = $this->getMockBuilder(EncoderIterator::class)->disableOriginalConstructor()->getMock();
  55. $this->encoder = $this->getMockBuilder(PdfEncoder::class)->disableOriginalConstructor()->getMock();
  56. $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  57. $this->storage = $this->getMockBuilder(TemporaryFileStorage::class)->disableOriginalConstructor()->getMock();
  58. $this->organizationRepo = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
  59. $this->uploadStorage = $this->getMockBuilder(UploadStorage::class)->disableOriginalConstructor()->getMock();
  60. $this->access = $this->getMockBuilder(Access::class)->getMock();
  61. $this->organization = $this->getMockBuilder(Organization::class)->getMock();
  62. $this->cmf = $this->getMockBuilder(Organization::class)->getMock();
  63. $this->networkOrgs = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  64. $this->collection = $this->getMockBuilder(Collection::class)->getMock();
  65. $this->parent = $this->getMockBuilder(Organization::class)->getMock();
  66. $this->logo = $this->getMockBuilder(File::class)->getMock();
  67. $this->presidentAccess = $this->getMockBuilder(Access::class)->getMock();
  68. $this->president = $this->getMockBuilder(Person::class)->getMock();
  69. $this->cmfParameters = $this->getMockBuilder(Parameters::class)->getMock();
  70. $this->qrCode = $this->getMockBuilder(File::class)->getMock();
  71. }
  72. private function makeExporterMock(string $methodUnderTest): LicenceCmfExporter | MockObject
  73. {
  74. $exporter = $this->getMockBuilder(LicenceCmfExporter::class)
  75. ->setConstructorArgs([$this->organizationRepo, $this->uploadStorage])
  76. ->setMethodsExcept(['setAccessRepository', 'setTwig', 'setEncoderIterator',
  77. 'setEntityManager', 'setEntityManager', 'setStorage', $methodUnderTest])
  78. ->getMock();
  79. $exporter->setAccessRepository($this->accessRepo);
  80. $exporter->setTwig($this->twig);
  81. $exporter->setEncoderIterator($this->encoderIterator);
  82. $exporter->setEntityManager($this->em);
  83. $exporter->setStorage($this->storage);
  84. return $exporter;
  85. }
  86. public function testSupport(): void
  87. {
  88. $exporter = $this->makeExporterMock('support');
  89. $unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock();
  90. $this->assertTrue($exporter->support($this->exportRequest));
  91. $this->assertFalse($exporter->support($unsupportedExportRequest));
  92. }
  93. private function prepareModelBuilding() {
  94. $this->exportRequest->method('getRequesterId')->willReturn(1);
  95. $this->exportRequest->method('getYear')->willReturn(2020);
  96. $this->exportRequest->method('getFormat')->willReturn('pdf');
  97. $this->accessRepo->method('find')->with(1)->willReturn($this->access);
  98. $this->access->method('getOrganization')->willReturn($this->organization);
  99. $this->organization->method('getId')->willReturn(1);
  100. $this->organization->method('getName')->willReturn('my_organization');
  101. $this->organization->method('getIdentifier')->willReturn('org1');
  102. $this->organization->method('getNetworkOrganizations')->willReturn($this->collection);
  103. $this->collection->method('get')->willReturn($this->networkOrgs);
  104. $this->networkOrgs->expects(self::once())->method('getParent')->willReturn($this->parent);
  105. $this->parent->expects(self::once())->method('getName')->willReturn('my_network');
  106. $this->organization->method('getLogo')->willReturn($this->logo);
  107. $this->logo->method('getId')->willReturn(1);
  108. $this->uploadStorage->method('getUri')->willReturn('http:://foo.bar/1');
  109. $this->president->method('getId')->willReturn(1);
  110. $this->president->method('getGender')->willReturn('M');
  111. $this->president->method('getGivenName')->willReturn('Joe');
  112. $this->president->method('getName')->willReturn('Dalton');
  113. $this->accessRepo
  114. ->expects(self::once())
  115. ->method('findByOrganizationAndMission')
  116. ->with($this->organization, 'PRESIDENT')
  117. ->willReturn([$this->presidentAccess]);
  118. $this->presidentAccess->method('getPerson')->willReturn($this->president);
  119. $this->cmf->method('getParameters')->willReturn($this->cmfParameters);
  120. $this->cmfParameters->expects(self::once())->method('getQrCode')->willReturn($this->qrCode);
  121. $this->qrCode->method('getId')->willReturn(1);
  122. $this->organizationRepo->expects(self::once())->method('find')->with(12097)->willReturn($this->cmf);
  123. }
  124. public function testBuildModel(): void
  125. {
  126. $exporter = $this->makeExporterMock('buildModel');
  127. $this->prepareModelBuilding();
  128. $modelCollection = $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
  129. $licence = $modelCollection->getLicences()[0];
  130. $this->assertEquals('my_network', $licence->getFederationName());
  131. $this->assertEquals(true, $licence->isOrganizationLicence());
  132. $this->assertEquals(2020, $licence->getYear());
  133. $this->assertEquals('931572', $licence->getColor());
  134. $this->assertEquals('http:://foo.bar/1', $licence->getQrCodeUri());
  135. $this->assertEquals('http:://foo.bar/1', $licence->getLogoUri());
  136. }
  137. /**
  138. * If the organization cannot be determined from the exportRequest author, an error should be thrown
  139. */
  140. public function testBuildModelWithoutOrganization(): void
  141. {
  142. $exporter = $this->makeExporterMock('buildModel');
  143. $this->exportRequest->method('getRequesterId')->willReturn(1);
  144. $this->accessRepo->method('find')->with(1)->willReturn(null);
  145. $this->expectException(RuntimeException::class);
  146. $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
  147. }
  148. public function testGetFileBasename(): void
  149. {
  150. $exporter = $this->makeExporterMock('getFileBasename');
  151. $licence = $this->getMockBuilder(LicenceCmf::class)->getMock();
  152. $licence->method('getYear')->willReturn(2020);
  153. $model = $this->getMockBuilder(LicenceCmfCollection::class)->getMock();
  154. $model->method('getLicences')->willReturn([$licence]);
  155. $result = $this->invokeMethod($exporter, 'getFileBasename', [$model]);
  156. $this->assertEquals(
  157. 'licence_cmf_2020.pdf',
  158. $result
  159. );
  160. }
  161. public function testGetFileType(): void
  162. {
  163. $exporter = $this->makeExporterMock('getFileType');
  164. $this->assertEquals(
  165. 'LICENCE_CMF',
  166. $this->invokeMethod($exporter, 'getFileType')
  167. );
  168. }
  169. /**
  170. * Given year is before the start year, first color is used
  171. */
  172. public function testGetLicenceColorPastYear(): void
  173. {
  174. $exporter = $this->makeExporterMock('getLicenceColor');
  175. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR - 10;
  176. $this->assertEquals(
  177. LicenceCmfExporter::LICENCE_CMF_COLOR[0],
  178. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  179. );
  180. }
  181. /**
  182. * Given year is within the 5 first years
  183. */
  184. public function testGetLicenceColorFirstYears(): void
  185. {
  186. $exporter = $this->makeExporterMock('getLicenceColor');
  187. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 3;
  188. $this->assertEquals(
  189. LicenceCmfExporter::LICENCE_CMF_COLOR[3],
  190. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  191. );
  192. }
  193. /**
  194. * Given year is beyond the 5 first years
  195. */
  196. public function testGetLicenceColorNExtYears(): void
  197. {
  198. $exporter = $this->makeExporterMock('getLicenceColor');
  199. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 8;
  200. $this->assertEquals(
  201. LicenceCmfExporter::LICENCE_CMF_COLOR[3],
  202. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  203. );
  204. }
  205. }