OrganizationProfileCreatorTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php /** @noinspection DuplicatedCode */
  2. namespace App\Test\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. */
  94. public function testCreateOrganizationProfileWithoutAdherentListBecauseOfPrincipalType(): void
  95. {
  96. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  97. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils])
  98. ->setMethodsExcept(['createCompleteOrganizationProfile'])
  99. ->getMock();
  100. $organization = $this->getMockBuilder(Organization::class)->getMock();
  101. $organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  102. $organizationProfileCreator->method('createLightOrganizationProfile')->with($organization)->willReturn($organizationProfile);
  103. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  104. $organization->method('getParameters')->willReturn($parameters);
  105. $parameters->method('getShowAdherentList')->willReturn(true);
  106. $organization->method('getPrincipalType')->willReturn(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY()->getValue());
  107. $organizationProfile->expects(self::once())->method('setShowAdherentList')->with(false);
  108. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([]));
  109. $this->tree->method('findAllParentsAndSortByType')->willReturn([]);
  110. $organizationProfileCreator->createCompleteOrganizationProfile($organization);
  111. }
  112. /**
  113. * If the parameters::showAdherentList is false, then organizationProfile::showAdherentList should be set to false
  114. */
  115. public function testCreateOrganizationProfileWithoutAdherentList(): void
  116. {
  117. $organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)
  118. ->setConstructorArgs([$this->module, $this->tree, $this->organizationUtils])
  119. ->setMethodsExcept(['createCompleteOrganizationProfile'])
  120. ->getMock();
  121. $organization = $this->getMockBuilder(Organization::class)->getMock();
  122. $organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
  123. $organizationProfileCreator->method('createLightOrganizationProfile')->with($organization)->willReturn($organizationProfile);
  124. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  125. $organization->method('getParameters')->willReturn($parameters);
  126. $parameters->method('getShowAdherentList')->willReturn(false);
  127. $organization->method('getPrincipalType')->willReturn(PrincipalTypeEnum::LOCAL_FEDERATION()->getValue());
  128. $organizationProfile->expects(self::once())->method('setShowAdherentList')->with(false);
  129. $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([]));
  130. $this->tree->method('findAllParentsAndSortByType')->willReturn([]);
  131. $organizationProfileCreator->createCompleteOrganizationProfile($organization);
  132. }
  133. }