OrganizationProfileCreatorTest.php 10 KB

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