LicenceCmfExporterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App\Tests\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\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\LicenceCmf;
  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. /**
  82. * @see LicenceCmfExporter::support()
  83. */
  84. public function testSupport(): void
  85. {
  86. $exporter = $this->makeExporterMock('support');
  87. $unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock();
  88. $this->assertTrue($exporter->support($this->exportRequest));
  89. $this->assertFalse($exporter->support($unsupportedExportRequest));
  90. }
  91. private function prepareModelBuilding(): void
  92. {
  93. $this->exportRequest->method('getRequesterId')->willReturn(1);
  94. $this->exportRequest->method('getYear')->willReturn(2020);
  95. $this->exportRequest->method('getFormat')->willReturn('pdf');
  96. $this->accessRepo->method('find')->with(1)->willReturn($this->access);
  97. $this->access->method('getOrganization')->willReturn($this->organization);
  98. $this->organization->method('getId')->willReturn(1);
  99. $this->organization->method('getName')->willReturn('my_organization');
  100. $this->organization->method('getIdentifier')->willReturn('org1');
  101. $this->organization->method('getNetworkOrganizations')->willReturn($this->collection);
  102. $this->collection->method('get')->willReturn($this->networkOrgs);
  103. $this->networkOrgs->expects(self::once())->method('getParent')->willReturn($this->parent);
  104. $this->parent->expects(self::once())->method('getName')->willReturn('my_network');
  105. $this->organization->method('getLogo')->willReturn($this->logo);
  106. $this->logo->method('getId')->willReturn(1);
  107. $this->storage->method('getDownloadIri')->willReturn('http:://foo.bar/1');
  108. $this->president->method('getId')->willReturn(1);
  109. $this->president->method('getGender')->willReturn('M');
  110. $this->president->method('getGivenName')->willReturn('Joe');
  111. $this->president->method('getName')->willReturn('Dalton');
  112. $this->accessRepo
  113. ->expects(self::once())
  114. ->method('findByOrganizationAndMission')
  115. ->with($this->organization, 'PRESIDENT')
  116. ->willReturn([$this->presidentAccess]);
  117. $this->presidentAccess->method('getPerson')->willReturn($this->president);
  118. $this->cmf->method('getParameters')->willReturn($this->cmfParameters);
  119. $this->cmfParameters->expects(self::once())->method('getQrCode')->willReturn($this->qrCode);
  120. $this->qrCode->method('getId')->willReturn(1);
  121. $this->organizationRepo->expects(self::once())->method('find')->with(12097)->willReturn($this->cmf);
  122. }
  123. /**
  124. * @see LicenceCmfExporter::buildModel()
  125. */
  126. public function testBuildModel(): void
  127. {
  128. $exporter = $this->makeExporterMock('buildModel');
  129. $this->prepareModelBuilding();
  130. $modelCollection = $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
  131. $licence = $modelCollection->getLicences()[0];
  132. $this->assertEquals('my_network', $licence->getFederationName());
  133. $this->assertEquals(true, $licence->isOrganizationLicence());
  134. $this->assertEquals(2020, $licence->getYear());
  135. $this->assertEquals('931572', $licence->getColor());
  136. $this->assertEquals('http:://foo.bar/1', $licence->getQrCodeUri());
  137. $this->assertEquals('http:://foo.bar/1', $licence->getLogoUri());
  138. }
  139. /**
  140. * If the organization cannot be determined from the exportRequest author, an error should be thrown
  141. * @see LicenceCmfExporter::buildModel()
  142. */
  143. public function testBuildModelWithoutOrganization(): void
  144. {
  145. $exporter = $this->makeExporterMock('buildModel');
  146. $this->exportRequest->method('getRequesterId')->willReturn(1);
  147. $this->accessRepo->method('find')->with(1)->willReturn(null);
  148. $this->expectException(RuntimeException::class);
  149. $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
  150. }
  151. /**
  152. * @see LicenceCmfExporter::getFileBasename()
  153. */
  154. public function testGetFileBasename(): void
  155. {
  156. $exporter = $this->makeExporterMock('getFileBasename');
  157. $licence = $this->getMockBuilder(LicenceCmf::class)->getMock();
  158. $licence->method('getYear')->willReturn(2020);
  159. $exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
  160. $exportRequest->method('getYear')->willReturn(2020);
  161. $result = $this->invokeMethod($exporter, 'getFileBasename', [$exportRequest]);
  162. $this->assertEquals(
  163. 'licence_cmf_2020.pdf',
  164. $result
  165. );
  166. }
  167. /**
  168. * @see LicenceCmfExporter::getFileType()
  169. */
  170. public function testGetFileType(): void
  171. {
  172. $exporter = $this->makeExporterMock('getFileType');
  173. $this->assertEquals(
  174. 'LICENCE_CMF',
  175. $this->invokeMethod($exporter, 'getFileType')
  176. );
  177. }
  178. /**
  179. * Given year is before the start year, first color is used
  180. * @see LicenceCmfExporter::getLicenceColor()
  181. */
  182. public function testGetLicenceColorPastYear(): void
  183. {
  184. $exporter = $this->makeExporterMock('getLicenceColor');
  185. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR - 10;
  186. $this->assertEquals(
  187. LicenceCmfExporter::LICENCE_CMF_COLOR[0],
  188. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  189. );
  190. }
  191. /**
  192. * Given year is within the 5 first years
  193. * @see LicenceCmfExporter::getLicenceColor()
  194. */
  195. public function testGetLicenceColorFirstYears(): void
  196. {
  197. $exporter = $this->makeExporterMock('getLicenceColor');
  198. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 3;
  199. $this->assertEquals(
  200. LicenceCmfExporter::LICENCE_CMF_COLOR[3],
  201. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  202. );
  203. }
  204. /**
  205. * Given year is beyond the 5 first years
  206. * @see LicenceCmfExporter::getLicenceColor()
  207. */
  208. public function testGetLicenceColorNExtYears(): void
  209. {
  210. $exporter = $this->makeExporterMock('getLicenceColor');
  211. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 8;
  212. $this->assertEquals(
  213. LicenceCmfExporter::LICENCE_CMF_COLOR[3],
  214. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  215. );
  216. }
  217. }