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