ModuleTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Test\Service\Security;
  3. use App\Entity\Organization\Organization;
  4. use App\Entity\Organization\Settings;
  5. use App\Service\Utils\Parser;
  6. use App\Service\Utils\Reflection;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use PHPUnit\Framework\TestCase;
  9. use App\Service\Security\Module;
  10. use App\Service\Cotisation\Utils as CotisationUtils;
  11. class ModuleTest extends TestCase
  12. {
  13. private const OPENTALENT_CONFIG = __DIR__.'/../../../config/opentalent';
  14. private Reflection $reflectionMock;
  15. private Parser $parser;
  16. public function setUp():void
  17. {
  18. $this->reflectionMock = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock();
  19. $this->parser = new Parser();
  20. }
  21. public function testGetOrganizationModules(): void
  22. {
  23. $module = $this->getMockBuilder(Module::class)
  24. ->setConstructorArgs([$this->reflectionMock, $this->parser, self::OPENTALENT_CONFIG])
  25. ->setMethodsExcept(['getOrganizationModules'])
  26. ->getMock();
  27. $module->expects(self::once())->method('getModuleBySettings')->willReturn(['Sms']);
  28. $module->expects(self::once())->method('getModulesByConditions')->willReturn(['CotisationCall']);
  29. $settings = $this->getMockBuilder(Settings::class)->disableOriginalConstructor()->getMock();
  30. $settings->expects(self::once())->method('getProduct')->willReturn('school');
  31. $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
  32. $organization->expects(self::once())->method('getSettings')->willReturn($settings);
  33. $module->expects(self::once())->method('getModulesByProductConfiguration')->willReturn(['Notification']);
  34. $this->assertEqualsCanonicalizing(
  35. ['Sms', 'CotisationCall', 'Notification'],
  36. $module->getOrganizationModules($organization)
  37. );
  38. }
  39. /**
  40. * @see Module::getModuleBySettings()
  41. */
  42. public function testGetModuleBySettings(): void
  43. {
  44. $module = $this->getMockBuilder(Module::class)
  45. ->setConstructorArgs([$this->reflectionMock, $this->parser, self::OPENTALENT_CONFIG])
  46. ->setMethodsExcept(['getModuleBySettings'])
  47. ->getMock();
  48. $settingsMock = $this->getMockBuilder(Settings::class)->getMock();
  49. $settingsMock
  50. ->expects($this->once())
  51. ->method('getModules')
  52. ->willReturn(["Sms" => true]);
  53. $organizationMock = $this->getMockBuilder(Organization::class)->getMock();
  54. $organizationMock
  55. ->expects($this->once())
  56. ->method('getSettings')
  57. ->willReturn($settingsMock);
  58. $value = "Sms";
  59. // assert function to test whether 'value' is a value of array
  60. $this->assertContains($value, $module->getModuleBySettings($organizationMock)) ;
  61. }
  62. /**
  63. * @see Module::getModulesByConditions()
  64. */
  65. public function testGetModulesByConditions(): void
  66. {
  67. $module = $this->getMockBuilder(Module::class)
  68. ->setConstructorArgs([$this->reflectionMock, $this->parser, self::OPENTALENT_CONFIG])
  69. ->setMethodsExcept(['getModulesByConditions'])
  70. ->getMock();
  71. $organizationMock = $this->getMockBuilder(Organization::class)->getMock();
  72. $this->reflectionMock
  73. ->method('dynamicInvokeServiceWithArgsAndMethod')
  74. ->withConsecutive(
  75. [CotisationUtils::class, 'isLastParentAndCMF', array($organizationMock)]
  76. )
  77. ->willReturnOnConsecutiveCalls(
  78. [true]
  79. )
  80. ;
  81. $value = "CotisationCall";
  82. // assert function to test whether 'value' is a value of array
  83. $this->assertContains($value, $module->getModulesByConditions($organizationMock)) ;
  84. }
  85. /**
  86. * @see Module::getModulesByProductConfiguration()
  87. */
  88. public function testGetModulesByProductConfiguration()
  89. {
  90. $module = $this->getMockBuilder(Module::class)
  91. ->setConstructorArgs([$this->reflectionMock, $this->parser, self::OPENTALENT_CONFIG])
  92. ->setMethodsExcept(['getModulesByProductConfiguration'])
  93. ->getMock();
  94. $value = "MessagesAdvanced";
  95. // assert function to test whether 'value' is a value of array
  96. $this->assertContains($value, $module->getModulesByProductConfiguration('artist-premium')) ;
  97. }
  98. }