ModuleTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. ['CotisationCall' => [
  104. 'roles' => ['ROLE_COTISATION'],
  105. 'conditions' => [
  106. 'service' => [
  107. 'name' => CotisationUtils::class,
  108. 'function' => 'isLastParentAndCMF',
  109. ],
  110. ],
  111. ]]
  112. );
  113. $organization = $this->getMockBuilder(Organization::class)->getMock();
  114. $this->reflection
  115. ->method('dynamicInvokeServiceWithArgsAndMethod')
  116. ->with(CotisationUtils::class, 'isLastParentAndCMF', [$organization])
  117. ->willThrowException(new \LogicException());
  118. $this->expectException(\LogicException::class);
  119. $module->getModulesByConditions($organization);
  120. }
  121. /**
  122. * @see Module::getModulesByProductConfiguration()
  123. */
  124. public function testGetModulesByProductConfiguration(): void
  125. {
  126. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  127. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  128. ['artist' => ['modules' => ['foo']]]
  129. );
  130. $this->assertEquals(['foo'], $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST));
  131. }
  132. /**
  133. * @see Module::getModulesByProductConfiguration()
  134. */
  135. public function testGetModulesByProductConfigurationExtend(): void
  136. {
  137. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  138. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  139. [
  140. 'artist' => ['modules' => ['foo']],
  141. 'artist-premium' => ['extend' => 'artist', 'modules' => ['bar']],
  142. ]
  143. );
  144. $this->assertEqualsCanonicalizing(
  145. ['foo', 'bar'],
  146. $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST_PREMIUM)
  147. );
  148. }
  149. /**
  150. * @see Module::getModulesByProductConfiguration()
  151. */
  152. public function testGetModulesByProductConfigurationAccessDenied(): void
  153. {
  154. $module = $this->getMockBuilder(TestableModule::class);
  155. $module = $this->getMockForMethod('getModulesByProductConfiguration');
  156. $this->parameterBag->method('get')->with('opentalent.products')->willReturn(
  157. ['artist' => ['modules' => ['foo']]]
  158. );
  159. $this->expectException(AccessDeniedHttpException::class);
  160. $this->expectExceptionMessage('The product artist-premium does not exist !');
  161. $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST_PREMIUM);
  162. }
  163. /**
  164. * @see Module::getModuleByResourceName()
  165. */
  166. public function testGetModuleByResourceName(): void
  167. {
  168. $module = $this->getMockForMethod('getModuleByResourceName');
  169. $this->parameterBag->method('get')->with('opentalent.modules')->willReturn(
  170. ['Core' => ['entities' => ['foo', 'bar']]]
  171. );
  172. $this->assertEquals('Core', $module->getModuleByResourceName('foo'));
  173. }
  174. /**
  175. * @see Module::getModuleByResourceName()
  176. */
  177. public function testGetModuleByResourceNameNotFound(): void
  178. {
  179. $module = $this->getMockForMethod('getModuleByResourceName');
  180. $this->parameterBag->method('get')->with('opentalent.modules')->willReturn(
  181. ['Core' => ['entities' => ['bar']]]
  182. );
  183. $this->assertNull($module->getModuleByResourceName('foo'));
  184. }
  185. }