OrganizationProfileCreatorTest.php 9.5 KB

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