OrganizationProfileCreatorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. * @see OrganizationProfileCreator::createCompleteOrganizationProfile()
  95. */
  96. public function testCreateOrganizationProfileWithoutAdherentListBecauseOfPrincipalType(): void
  97. {
  98. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  99. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils])
  100. ->setMethodsExcept(['createCompleteOrganizationProfile'])
  101. ->getMock();
  102. $organization = $this->getMockBuilder(Organization::class)->getMock();
  103. $organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  104. $organizationProfileCreator->method('createLightOrganizationProfile')->with($organization)->willReturn($organizationProfile);
  105. $settings = $this->getMockBuilder(Settings::class)->getMock();
  106. $settings->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  107. $organization->method('getSettings')->willReturn($settings);
  108. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  109. $organization->method('getParameters')->willReturn($parameters);
  110. $parameters->method('getShowAdherentList')->willReturn(true);
  111. $organization->method('getPrincipalType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY);
  112. $organizationProfile->expects(self::once())->method('setShowAdherentList')->with(false);
  113. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([]));
  114. $this->tree->method('findAllParentsAndSortByType')->willReturn([]);
  115. $organizationProfileCreator->createCompleteOrganizationProfile($organization);
  116. }
  117. /**
  118. * If the parameters::showAdherentList is false, then organizationProfile::showAdherentList should be set to false
  119. * @see OrganizationProfileCreator::createCompleteOrganizationProfile()
  120. */
  121. public function testCreateOrganizationProfileWithoutAdherentList(): void
  122. {
  123. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  124. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils])
  125. ->setMethodsExcept(['createCompleteOrganizationProfile'])
  126. ->getMock();
  127. $organization = $this->getMockBuilder(Organization::class)->getMock();
  128. $organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  129. $organizationProfileCreator->method('createLightOrganizationProfile')->with($organization)->willReturn($organizationProfile);
  130. $settings = $this->getMockBuilder(Settings::class)->getMock();
  131. $settings->method('getProduct')->willReturn(SettingsProductEnum::ARTIST);
  132. $organization->method('getSettings')->willReturn($settings);
  133. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  134. $organization->method('getParameters')->willReturn($parameters);
  135. $parameters->method('getShowAdherentList')->willReturn(false);
  136. $organization->method('getPrincipalType')->willReturn(PrincipalTypeEnum::LOCAL_FEDERATION);
  137. $organizationProfile->expects(self::once())->method('setShowAdherentList')->with(false);
  138. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([]));
  139. $this->tree->method('findAllParentsAndSortByType')->willReturn([]);
  140. $organizationProfileCreator->createCompleteOrganizationProfile($organization);
  141. }
  142. /**
  143. * @see OrganizationProfileCreator::createLightOrganizationProfile()
  144. */
  145. public function testCreateLightOrganizationProfile(): void {
  146. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  147. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils])
  148. ->setMethodsExcept(['createLightOrganizationProfile'])
  149. ->getMock();
  150. $organization = $this->getMockBuilder(Organization::class)->getMock();
  151. $organization->method('getId')->willReturn(123);
  152. $organization->method('getName')->willReturn('Foo');
  153. $this->organizationUtils->method('getOrganizationWebsite')->with($organization)->willReturn('foo.net');
  154. $result = $organizationProfileCreator->createLightOrganizationProfile($organization);
  155. $this->assertEquals(123, $result->getId());
  156. $this->assertEquals('Foo', $result->getName());
  157. $this->assertEquals('foo.net', $result->getWebsite());
  158. }
  159. }