LicenceCmfExporterTest.php 11 KB

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