AccessProfileCreatorTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Test\Service\Access;
  3. use App\ApiResources\Profile\AccessProfile;
  4. use App\ApiResources\Profile\OrganizationProfile;
  5. use App\Entity\Access\Access;
  6. use App\Entity\Billing\AccessIntangible;
  7. use App\Entity\Billing\AccessPayer;
  8. use App\Entity\Organization\Organization;
  9. use App\Entity\Person\Person;
  10. use App\Repository\Access\AccessRepository;
  11. use App\Service\Access\AccessProfileCreator;
  12. use App\Service\Access\OptionalsRolesIterator;
  13. use App\Service\Access\Utils;
  14. use App\Service\Organization\OrganizationProfileCreator;
  15. use PHPUnit\Framework\TestCase;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Core\Role\RoleHierarchy;
  18. class AccessProfileCreatorTest extends TestCase
  19. {
  20. private Access $access;
  21. private AccessProfileCreator $accessProfileCreator;
  22. private mixed $accessRepositoryMock;
  23. public function setUp():void
  24. {
  25. $person = new Person();
  26. $person
  27. ->setName('Foo')
  28. ->setGivenName('Bar')
  29. ;
  30. $today = new \DateTime();
  31. $this->access = new Access();
  32. $this->access
  33. ->setAdminAccess(true)
  34. ->setPerson($person)
  35. ->setOrganization(new Organization())
  36. ->setActivityYear($today->format('Y'))
  37. ;
  38. $organizationProfileCreatorMock = $this->getMockBuilder(OrganizationProfileCreator::class)->disableOriginalConstructor()->getMock();
  39. $organizationProfileCreatorMock
  40. ->method('createCompleteOrganizationProfile')
  41. ->with($this->access->getOrganization())
  42. ->willReturn(new OrganizationProfile());
  43. $this->accessRepositoryMock = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
  44. $accessUtilsMock = $this->getMockBuilder(Utils::class)->disableOriginalConstructor()->getMock();
  45. $this->accessProfileCreator = new AccessProfileCreator(
  46. $organizationProfileCreatorMock,
  47. $this->accessRepositoryMock,
  48. $accessUtilsMock
  49. );
  50. }
  51. /**
  52. * @see AccessProfileCreator::getAccessProfile()
  53. */
  54. public function testGetAccessProfileFailed(){
  55. $this->accessRepositoryMock
  56. ->method('findAllValidAccesses')
  57. ->with($this->access)
  58. ->willReturn([]);
  59. $this->expectException(AuthenticationException::class);
  60. $this->accessProfileCreator->getAccessProfile($this->access);
  61. }
  62. /**
  63. * @see AccessProfileCreator::createCompleteAccessProfile()
  64. */
  65. public function testCreateCompleteAccessProfile(){
  66. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  67. $this->assertInstanceOf(AccessProfile::class, $accessProfile);
  68. }
  69. /**
  70. * @see AccessProfileCreator::createLightAccessProfile()
  71. */
  72. public function testCreateLightAccessProfile(){
  73. $accessProfile = $this->accessProfileCreator->createLightAccessProfile($this->access);
  74. $this->assertInstanceOf(AccessProfile::class, $accessProfile);
  75. }
  76. public function testIsGuardian(){
  77. $childrenMock = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  78. $this->access->addChild($childrenMock);
  79. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  80. $this->assertTrue($accessProfile->getIsGuardian());
  81. }
  82. public function testIsNotGuardian(){
  83. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  84. $this->assertFalse($accessProfile->getIsGuardian());
  85. }
  86. public function testIsPayorWithBillingPayer(){
  87. $accessPayerMock = $this->getMockBuilder(AccessPayer::class)->disableOriginalConstructor()->getMock();
  88. $this->access->addBillingPayer($accessPayerMock);
  89. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  90. $this->assertTrue($accessProfile->getIsPayor());
  91. }
  92. public function testIsPayorWithAccessIntangible(){
  93. $accessIntangibleMock = $this->getMockBuilder(AccessIntangible::class)->disableOriginalConstructor()->getMock();
  94. $this->access->addAccessIntangible($accessIntangibleMock);
  95. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  96. $this->assertTrue($accessProfile->getIsPayor());
  97. }
  98. public function testIsNotPayor(){
  99. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  100. $this->assertFalse($accessProfile->getIsPayor());
  101. }
  102. public function testIsNotPayorWithAccessIntangibleBecauseChildren(){
  103. $accessIntangibleMock = $this->getMockBuilder(AccessIntangible::class)->disableOriginalConstructor()->getMock();
  104. $this->access->addAccessIntangible($accessIntangibleMock);
  105. $childrenMock = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
  106. $this->access->addChild($childrenMock);
  107. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  108. $this->assertFalse($accessProfile->getIsPayor());
  109. }
  110. public function testIsNotPayorWithAccessIntangibleBecauseBillingReceivers(){
  111. $accessIntangibleMock = $this->getMockBuilder(AccessIntangible::class)->disableOriginalConstructor()->getMock();
  112. $this->access->addAccessIntangible($accessIntangibleMock);
  113. $accessPayerMock = $this->getMockBuilder(AccessPayer::class)->disableOriginalConstructor()->getMock();
  114. $this->access->addBillingReceiver($accessPayerMock);
  115. $accessProfile = $this->accessProfileCreator->createCompleteAccessProfile($this->access);
  116. $this->assertFalse($accessProfile->getIsPayor());
  117. }
  118. }