ModuleTest.php 7.6 KB

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