ModuleTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Tests\Unit\Service\Security;
  3. use App\Entity\Organization\Organization;
  4. use App\Entity\Organization\Settings;
  5. use App\Service\Cotisation\Utils as CotisationUtils;
  6. use App\Service\Security\Module;
  7. use App\Service\Utils\Reflection;
  8. use PHPUnit\Framework\MockObject\MockObject;
  9. use PHPUnit\Framework\TestCase;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. class TestableModule extends Module
  12. {
  13. public function getModuleByConditionsConfig(): array
  14. {
  15. return parent::getModuleByConditionsConfig();
  16. }
  17. class TestableModule extends Module
  18. {
  19. public function getModuleByConditionsConfig(): array
  20. {
  21. return parent::getModuleByConditionsConfig();
  22. }
  23. }
  24. class ModuleTest extends TestCase
  25. {
  26. private MockObject|Reflection $reflection;
  27. private MockObject|ParameterBagInterface $parameterBag;
  28. public function setUp(): void
  29. public function setUp(): void
  30. {
  31. $this->reflection = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock();
  32. $this->parameterBag = $this->getMockBuilder(ParameterBagInterface::class)->disableOriginalConstructor()->getMock();
  33. }
  34. public function getMockForMethod(string $methodName): MockObject|TestableModule
  35. {
  36. return $this->getMockBuilder(TestableModule::class)
  37. ->setConstructorArgs([$this->reflection, $this->parameterBag])
  38. ->setMethodsExcept([$methodName])
  39. ->getMock();
  40. }
  41. /**
  42. * @see Module::getOrganizationModules()
  43. */
  44. public function testGetOrganizationModules(): void
  45. {
  46. $module = $this->getMockForMethod('getOrganizationModules');
  47. $module->expects(self::once())->method('getModuleBySettings')->willReturn(['Sms']);
  48. $module->expects(self::once())->method('getModulesByConditions')->willReturn(['CotisationCall']);
  49. $settings = $this->getMockBuilder(Settings::class)->disableOriginalConstructor()->getMock();
  50. $settings->expects(self::once())->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL);
  51. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  52. $organization->expects(self::once())->method('getSettings')->willReturn($settings);
  53. $module->expects(self::once())->method('getModulesByProductConfiguration')->willReturn(['Notification']);
  54. $this->assertEqualsCanonicalizing(
  55. ['Sms', 'CotisationCall', 'Notification'],
  56. $module->getOrganizationModules($organization)
  57. );
  58. }
  59. /**
  60. * @see Module::getModuleBySettings()
  61. */
  62. public function testGetModuleBySettings(): void
  63. {
  64. $module = $this->getMockForMethod('getModuleBySettings');
  65. $settings = $this->getMockBuilder(Settings::class)->getMock();
  66. $settings
  67. ->expects($this->once())
  68. ->method('getModules')
  69. ->willReturn(['Sms' => true]);
  70. $organization = $this->getMockBuilder(Organization::class)->getMock();
  71. $organization
  72. ->expects($this->once())
  73. ->method('getSettings')
  74. ->willReturn($settings);
  75. $this->assertContains('Sms', $module->getModuleBySettings($organization));
  76. }
  77. /**
  78. * @see Module::getModulesByConditions()
  79. */
  80. public function testGetModulesByConditions(): void
  81. {
  82. $module = $this->getMockForMethod('getModulesByConditions');
  83. $this->parameterBag->method('get')->with('opentalent.modulesbyconditions')->willReturn(
  84. ['CotisationCall' => [
  85. 'roles' => ['ROLE_COTISATION'],
  86. 'conditions' => [
  87. 'service' => [
  88. 'name' => CotisationUtils::class,
  89. 'function' => 'isLastParentAndCMF',
  90. ],
  91. ],
  92. ]]
  93. );
  94. $organization = $this->getMockBuilder(Organization::class)->getMock();
  95. $this->reflection
  96. ->method('dynamicInvokeServiceWithArgsAndMethod')
  97. ->with(CotisationUtils::class, 'isLastParentAndCMF', [$organization])
  98. ->willReturn(true);
  99. $this->assertContains('CotisationCall', $module->getModulesByConditions($organization));
  100. }
  101. /**
  102. * @see Module::getModulesByConditions()
  103. */
  104. public function testGetModulesByConditionsLogicError(): void
  105. {
  106. $module = $this->getMockForMethod('getModulesByConditions');
  107. $this->parameterBag->method('get')->with('opentalent.modulesbyconditions')->willReturn(
  108. ['CotisationCall' => []]
  109. );
  110. $organization = $this->getMockBuilder(Organization::class)->getMock();
  111. $this->reflection
  112. ->method('dynamicInvokeServiceWithArgsAndMethod')
  113. ->with(CotisationUtils::class, 'isLastParentAndCMF', [$organization])
  114. ->willThrowException(new \Exception());
  115. $this->expectException(\LogicException::class);
  116. $module->getModulesByConditions($organization);
  117. }
  118. /**
  119. * @see Module::getModulesByProductConfiguration()
  120. */
  121. public function testGetModulesByProductConfiguration(): void
  122. {
  123. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  124. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  125. ['artist' => ['modules' => ['foo']]]
  126. );
  127. $this->assertEquals(['foo'], $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST));
  128. }
  129. /**
  130. * @see Module::getModulesByProductConfiguration()
  131. */
  132. public function testGetModulesByProductConfigurationExtend(): void
  133. {
  134. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  135. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  136. [
  137. 'artist' => ['modules' => ['foo']],
  138. 'artist-premium' => ['extend' => 'artist', 'modules' => ['bar']],
  139. ]
  140. );
  141. $this->assertEqualsCanonicalizing(
  142. ['foo', 'bar'],
  143. $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST_PREMIUM)
  144. );
  145. }
  146. /**
  147. * @see Module::getModulesByProductConfiguration()
  148. */
  149. public function testGetModulesByProductConfigurationAccessDenied(): void
  150. {
  151. $module = $this->getMockBuilder(TestableModule::class);
  152. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  153. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  154. ['artist' => ['modules' => ['foo']]]
  155. );
  156. $this->expectException(AccessDeniedHttpException::class);
  157. $this->expectExceptionMessage('The product artist-premium does not exist !');
  158. $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST_PREMIUM);
  159. }
  160. /**
  161. * @see Module::getModuleByResourceName()
  162. */
  163. public function testGetModuleByResourceName(): void
  164. {
  165. $module = $this->getMockForMethod('getModuleByResourceName');
  166. $this->parameterBag->method('get')->with('opentalent.modules')->willReturn(
  167. ['Core' => ['entities' => ['foo', 'bar']]]
  168. );
  169. $this->assertEquals('Core', $module->getModuleByResourceName('foo'));
  170. }
  171. /**
  172. * @see Module::getModuleByResourceName()
  173. */
  174. public function testGetModuleByResourceNameNotFound(): void
  175. {
  176. $module = $this->getMockForMethod('getModuleByResourceName');
  177. $this->parameterBag->method('get')->with('opentalent.modules')->willReturn(
  178. ['Core' => ['entities' => ['bar']]]
  179. );
  180. $this->assertNull($module->getModuleByResourceName('foo'));
  181. }
  182. }