ModuleTest.php 7.9 KB

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