LicenceCmfExporterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\Manager\AbstractFileManager;
  18. use App\Service\File\Storage\LocalStorage;
  19. use App\Service\ServiceIterator\EncoderIterator;
  20. use App\Tests\Unit\TestToolsTrait;
  21. use Doctrine\Common\Collections\Collection;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use PHPUnit\Framework\MockObject\MockObject;
  24. use PHPUnit\Framework\TestCase;
  25. use RuntimeException;
  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 | LocalStorage $localStorage;
  36. private MockObject | AbstractFileManager $fileManager;
  37. private MockObject | OrganizationRepository $organizationRepo;
  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->localStorage = $this->getMockBuilder(LocalStorage::class)->disableOriginalConstructor()->getMock();
  58. $this->fileManager = $this->getMockBuilder(AbstractFileManager::class)->disableOriginalConstructor()->getMock();
  59. $this->organizationRepo = $this->getMockBuilder(OrganizationRepository::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])
  76. ->setMethodsExcept(['setAccessRepository', 'setTwig', 'setEncoderIterator',
  77. 'setEntityManager', 'setEntityManager', 'setLocalStorage', 'setFileManager', $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->setLocalStorage($this->localStorage);
  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('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('M');
  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()->getValue())
  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. * @see LicenceCmfExporter::buildModel()
  150. */
  151. public function testBuildModelWithoutOrganization(): void
  152. {
  153. $exporter = $this->makeExporterMock('buildModel');
  154. $this->exportRequest->method('getRequesterId')->willReturn(1);
  155. $this->accessRepo->method('find')->with(1)->willReturn(null);
  156. $this->expectException(RuntimeException::class);
  157. $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
  158. }
  159. /**
  160. * @see LicenceCmfExporter::getFileBasename()
  161. */
  162. public function testGetFileBasename(): void
  163. {
  164. $exporter = $this->makeExporterMock('getFileBasename');
  165. $licence = $this->getMockBuilder(LicenceCmf::class)->getMock();
  166. $licence->method('getYear')->willReturn(2020);
  167. $exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
  168. $exportRequest->method('getYear')->willReturn(2020);
  169. $result = $this->invokeMethod($exporter, 'getFileBasename', [$exportRequest]);
  170. $this->assertEquals(
  171. 'licence_cmf_2020.pdf',
  172. $result
  173. );
  174. }
  175. /**
  176. * @see LicenceCmfExporter::getFileType()
  177. */
  178. public function testGetFileType(): void
  179. {
  180. $exporter = $this->makeExporterMock('getFileType');
  181. $this->assertEquals(
  182. 'LICENCE_CMF',
  183. $this->invokeMethod($exporter, 'getFileType')
  184. );
  185. }
  186. /**
  187. * Given year is before the start year, first color is used
  188. * @see LicenceCmfExporter::getLicenceColor()
  189. */
  190. public function testGetLicenceColorPastYear(): void
  191. {
  192. $exporter = $this->makeExporterMock('getLicenceColor');
  193. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR - 10;
  194. $this->assertEquals(
  195. LicenceCmfExporter::LICENCE_CMF_COLOR[0],
  196. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  197. );
  198. }
  199. /**
  200. * Given year is within the 5 first years
  201. * @see LicenceCmfExporter::getLicenceColor()
  202. */
  203. public function testGetLicenceColorFirstYears(): void
  204. {
  205. $exporter = $this->makeExporterMock('getLicenceColor');
  206. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 3;
  207. $this->assertEquals(
  208. LicenceCmfExporter::LICENCE_CMF_COLOR[3],
  209. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  210. );
  211. }
  212. /**
  213. * Given year is beyond the 5 first years
  214. * @see LicenceCmfExporter::getLicenceColor()
  215. */
  216. public function testGetLicenceColorNExtYears(): void
  217. {
  218. $exporter = $this->makeExporterMock('getLicenceColor');
  219. $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 8;
  220. $this->assertEquals(
  221. LicenceCmfExporter::LICENCE_CMF_COLOR[3],
  222. $this->invokeMethod($exporter, 'getLicenceColor', [$year])
  223. );
  224. }
  225. }