ModuleTest.php 7.5 KB

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