LicenceCmfExporterTest.php 11 KB

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