LicenceCmfExporterTest.php 10 KB

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