OrganizationProfileCreatorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Tests\Unit\Service\Organization;
  3. use App\ApiResources\Profile\OrganizationProfile;
  4. use App\Entity\Network\Network;
  5. use App\Entity\Network\NetworkOrganization;
  6. use App\Entity\Organization\Organization;
  7. use App\Entity\Organization\Parameters;
  8. use App\Entity\Organization\Settings;
  9. use App\Enum\Organization\LegalEnum;
  10. use App\Enum\Organization\PrincipalTypeEnum;
  11. use App\Enum\Organization\SettingsProductEnum;
  12. use App\Service\Network\Tree;
  13. use App\Service\Organization\OrganizationProfileCreator;
  14. use App\Service\Organization\Utils as OrganizationUtils;
  15. use App\Service\Security\Module;
  16. use App\Service\Shop\Trial;
  17. use Doctrine\Common\Collections\ArrayCollection;
  18. use PHPUnit\Framework\TestCase;
  19. class OrganizationProfileCreatorTest extends TestCase
  20. {
  21. private Module $module;
  22. private Tree $tree;
  23. private OrganizationUtils $organizationUtils;
  24. private Trial $trialService;
  25. public function setUp(): void
  26. {
  27. $this->module = $this->getMockBuilder(Module::class)->disableOriginalConstructor()->getMock();
  28. $this->tree = $this->getMockBuilder(Tree::class)->disableOriginalConstructor()->getMock();
  29. $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
  30. $this->trialService = $this->getMockBuilder(Trial::class)->disableOriginalConstructor()->getMock();
  31. }
  32. /**
  33. * @see OrganizationProfileCreator::createCompleteOrganizationProfile()
  34. */
  35. public function testCreateCompleteOrganizationProfile(): void
  36. {
  37. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  38. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils, $this->trialService])
  39. ->setMethodsExcept(['createCompleteOrganizationProfile'])
  40. ->getMock();
  41. $organization = $this->getMockBuilder(Organization::class)->getMock();
  42. $organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  43. $modules = ['1'];
  44. $this->module->method('getOrganizationModules')->with($organization)->willReturn($modules);
  45. $organizationProfile->expects(self::once())->method('setModules')->with($modules);
  46. $settings = $this->getMockBuilder(Settings::class)->getMock();
  47. $settings->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  48. $organization->method('getSettings')->willReturn($settings);
  49. $organizationProfile->expects(self::once())->method('setProduct')->with(SettingsProductEnum::ARTIST);
  50. $parametersId = 101;
  51. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  52. $parameters->method('getId')->willReturn($parametersId);
  53. $organization->method('getParameters')->willReturn($parameters);
  54. $organizationProfile->expects(self::once())->method('setParametersId')->with($parametersId);
  55. $organization->method('getLegalStatus')->willReturn(LegalEnum::ASSOCIATION_LAW_1901);
  56. $organizationProfile->expects(self::once())->method('setLegalStatus')->with(LegalEnum::ASSOCIATION_LAW_1901);
  57. $organization->method('getNetworkOrganizationChildren')->willReturn(new ArrayCollection([1, 2, 3]));
  58. $organizationProfile->expects(self::once())->method('setHasChildren')->with(true);
  59. $parameters->method('getShowAdherentList')->willReturn(true);
  60. $organization->method('getPrincipalType')->willReturn(PrincipalTypeEnum::LOCAL_FEDERATION);
  61. $organizationProfile->expects(self::once())->method('setShowAdherentList')->with(true);
  62. $network1 = $this->getMockBuilder(Network::class)->getMock();
  63. $network1->method('getName')->willReturn('Net 1');
  64. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  65. $networkOrganization1->method('getNetwork')->willReturn($network1);
  66. $network2 = $this->getMockBuilder(Network::class)->getMock();
  67. $network2->method('getName')->willReturn('Net 2');
  68. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  69. $networkOrganization2->method('getNetwork')->willReturn($network2);
  70. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2]));
  71. $organizationProfile->expects(self::exactly(2))->method('addNetwork')->withConsecutive(['Net 1'], ['Net 2']);
  72. $parent1 = $this->getMockBuilder(Organization::class)->getMock();
  73. $parent2 = $this->getMockBuilder(Organization::class)->getMock();
  74. $this->tree->method('findAllParentsAndSortByType')->willReturn([$parent1, $parent2]);
  75. $parentProfile1 = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  76. $parentProfile2 = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  77. // Called 3 times, once for the organization, two for its parents
  78. $organizationProfileCreator
  79. ->expects(self::exactly(3))
  80. ->method('createLightOrganizationProfile')
  81. ->withConsecutive([$organization], [$parent1], [$parent2])
  82. ->willReturnOnConsecutiveCalls($organizationProfile, $parentProfile1, $parentProfile2);
  83. $organizationProfile
  84. ->expects(self::exactly(2))
  85. ->method('addParent')
  86. ->withConsecutive([$parentProfile1], [$parentProfile2]);
  87. $this->organizationUtils->method('getOrganizationCurrentActivityYear')->with($organization)->willReturn(2022);
  88. $organizationProfile->expects(self::once())->method('setCurrentYear')->with();
  89. $returned = $organizationProfileCreator->createCompleteOrganizationProfile($organization);
  90. $this->assertSame(
  91. $organizationProfile,
  92. $returned
  93. );
  94. }
  95. /**
  96. * If the organization principal type is ARTISTIC_EDUCATION_ONLY, then showAdherentList should be set to false.
  97. *
  98. * @see OrganizationProfileCreator::createCompleteOrganizationProfile()
  99. */
  100. public function testCreateOrganizationProfileWithoutAdherentListBecauseOfPrincipalType(): void
  101. {
  102. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  103. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils, $this->trialService])
  104. ->setMethodsExcept(['createCompleteOrganizationProfile'])
  105. ->getMock();
  106. $organization = $this->getMockBuilder(Organization::class)->getMock();
  107. $organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  108. $organizationProfileCreator->method('createLightOrganizationProfile')->with($organization)->willReturn($organizationProfile);
  109. $settings = $this->getMockBuilder(Settings::class)->getMock();
  110. $settings->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  111. $organization->method('getSettings')->willReturn($settings);
  112. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  113. $organization->method('getParameters')->willReturn($parameters);
  114. $parameters->method('getShowAdherentList')->willReturn(true);
  115. $organization->method('getPrincipalType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY);
  116. $organizationProfile->expects(self::once())->method('setShowAdherentList')->with(false);
  117. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([]));
  118. $this->tree->method('findAllParentsAndSortByType')->willReturn([]);
  119. $organizationProfileCreator->createCompleteOrganizationProfile($organization);
  120. }
  121. /**
  122. * If the parameters::showAdherentList is false, then organizationProfile::showAdherentList should be set to false.
  123. *
  124. * @see OrganizationProfileCreator::createCompleteOrganizationProfile()
  125. */
  126. public function testCreateOrganizationProfileWithoutAdherentList(): void
  127. {
  128. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  129. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils, $this->trialService])
  130. ->setMethodsExcept(['createCompleteOrganizationProfile'])
  131. ->getMock();
  132. $organization = $this->getMockBuilder(Organization::class)->getMock();
  133. $organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  134. $organizationProfileCreator->method('createLightOrganizationProfile')->with($organization)->willReturn($organizationProfile);
  135. $settings = $this->getMockBuilder(Settings::class)->getMock();
  136. $settings->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  137. $organization->method('getSettings')->willReturn($settings);
  138. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  139. $organization->method('getParameters')->willReturn($parameters);
  140. $parameters->method('getShowAdherentList')->willReturn(false);
  141. $organization->method('getPrincipalType')->willReturn(PrincipalTypeEnum::LOCAL_FEDERATION);
  142. $organizationProfile->expects(self::once())->method('setShowAdherentList')->with(false);
  143. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([]));
  144. $this->tree->method('findAllParentsAndSortByType')->willReturn([]);
  145. $organizationProfileCreator->createCompleteOrganizationProfile($organization);
  146. }
  147. /**
  148. * @see OrganizationProfileCreator::createLightOrganizationProfile()
  149. */
  150. public function testCreateLightOrganizationProfile(): void
  151. {
  152. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  153. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils, $this->trialService])
  154. ->setMethodsExcept(['createLightOrganizationProfile'])
  155. ->getMock();
  156. $organization = $this->getMockBuilder(Organization::class)->getMock();
  157. $organization->method('getId')->willReturn(123);
  158. $organization->method('getName')->willReturn('Foo');
  159. $this->organizationUtils->method('getOrganizationWebsite')->with($organization)->willReturn('foo.net');
  160. $result = $organizationProfileCreator->createLightOrganizationProfile($organization);
  161. $this->assertEquals(123, $result->getId());
  162. $this->assertEquals('Foo', $result->getName());
  163. $this->assertEquals('foo.net', $result->getWebsite());
  164. }
  165. }