LicenceCmfExporterTest.php 11 KB

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