Vincent 11 kuukautta sitten
vanhempi
commit
ddfd03077d
1 muutettua tiedostoa jossa 46 lisäystä ja 5 poistoa
  1. 46 5
      tests/Unit/Service/Access/AccessProfileCreatorTest.php

+ 46 - 5
tests/Unit/Service/Access/AccessProfileCreatorTest.php

@@ -5,6 +5,7 @@ namespace App\Tests\Unit\Service\Access;
 use App\ApiResources\Profile\AccessProfile;
 use App\ApiResources\Profile\OrganizationProfile;
 use App\Entity\Access\Access;
+use App\Entity\Access\AccessPreference;
 use App\Entity\Core\File;
 use App\Entity\Organization\Organization;
 use App\Entity\Person\Person;
@@ -15,6 +16,7 @@ use App\Service\Access\Utils as AccessUtils;
 use App\Service\Organization\OrganizationProfileCreator;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Collection;
+use Doctrine\ORM\EntityManagerInterface;
 use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -26,17 +28,20 @@ class AccessProfileCreatorTest extends TestCase
     private MockObject|AccessUtils $accessUtils;
     private MockObject|AccessProfileCreator $accessProfileCreator;
     private MockObject|AccessProfile $accessProfile;
+    private MockObject|AccessPreference $accessPreference;
     private MockObject|Collection $emptyCollection;
     private MockObject|Collection $nonEmptyCollection;
     private MockObject|Organization $organization;
     private MockObject|Access $access;
     private MockObject|OrganizationProfile $organizationProfile;
+    private MockObject|EntityManagerInterface $entityManager;
 
     public function setUp(): void
     {
         $this->organizationProfileCreator = $this->getMockBuilder(OrganizationProfileCreator::class)->disableOriginalConstructor()->getMock();
         $this->accessRepository = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
         $this->accessUtils = $this->getMockBuilder(AccessUtils::class)->disableOriginalConstructor()->getMock();
+        $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
 
         $this->emptyCollection = $this->getMockBuilder(Collection::class)->getMock();
         $this->emptyCollection->method('isEmpty')->willReturn(true);
@@ -48,6 +53,7 @@ class AccessProfileCreatorTest extends TestCase
         $this->access = $this->getMockBuilder(Access::class)->getMock();
         $this->organizationProfile = $this->getMockBuilder(OrganizationProfile::class)->getMock();
         $this->accessProfile = $this->getMockBuilder(AccessProfile::class)->getMock();
+        $this->accessPreference = $this->getMockBuilder(AccessPreference::class)->getMock();
     }
 
     /**
@@ -57,7 +63,7 @@ class AccessProfileCreatorTest extends TestCase
     {
         $this->accessProfileCreator = $this
             ->getMockBuilder(AccessProfileCreator::class)
-            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
+            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
             ->setMethodsExcept(['getAccessProfile'])
             ->getMock();
 
@@ -79,7 +85,7 @@ class AccessProfileCreatorTest extends TestCase
     {
         $accessProfileCreator = $this
             ->getMockBuilder(AccessProfileCreator::class)
-            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
+            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
             ->setMethodsExcept(['getAccessProfile'])
             ->getMock();
 
@@ -92,6 +98,17 @@ class AccessProfileCreatorTest extends TestCase
             ->with($this->access)
             ->willReturn([$this->access, $otherAccess1, $otherAccess2]);
 
+        // Find valid accesses
+        $this->access
+            ->method('getPreferences')
+            ->willReturn(null);
+
+        // create the profile
+        $accessProfileCreator
+            ->expects(self::once())
+            ->method('createAccessPrefence')
+            ->with($this->access);
+
         // create the profile
         $accessProfileCreator
             ->expects(self::once())
@@ -169,7 +186,7 @@ class AccessProfileCreatorTest extends TestCase
     {
         $accessProfileCreator = $this
             ->getMockBuilder(AccessProfileCreator::class)
-            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
+            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
             ->setMethodsExcept(['getAccessProfile'])
             ->getMock();
 
@@ -215,7 +232,7 @@ class AccessProfileCreatorTest extends TestCase
     {
         $this->accessProfileCreator = $this
             ->getMockBuilder(AccessProfileCreator::class)
-            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
+            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
             ->setMethodsExcept(['createCompleteAccessProfile'])
             ->getMock();
 
@@ -352,7 +369,7 @@ class AccessProfileCreatorTest extends TestCase
     {
         $accessProfileCreator = $this
             ->getMockBuilder(AccessProfileCreator::class)
-            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils])
+            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
             ->setMethodsExcept(['createLightAccessProfile'])
             ->getMock();
 
@@ -365,9 +382,13 @@ class AccessProfileCreatorTest extends TestCase
         $person->expects(self::once())->method('getGender')->willReturn(GenderEnum::MISTER);
         $person->expects(self::once())->method('getImage')->willReturn($image);
 
+        $accessPreference = $this->getMockBuilder(AccessPreference::class)->getMock();
+        $accessPreference->expects(self::once())->method('getId')->willReturn(1);
+
         $this->access->expects(self::once())->method('getId')->willReturn(1);
         $this->access->method('getOrganization')->willReturn($this->organization);
         $this->access->method('getPerson')->willReturn($person);
+        $this->access->expects(self::once())->method('getPreferences')->willReturn($accessPreference);
         $this->access->expects(self::once())->method('getActivityYear')->willReturn(2020);
         $this->access->expects(self::once())->method('getSuperAdminAccess')->willReturn(false);
 
@@ -387,5 +408,25 @@ class AccessProfileCreatorTest extends TestCase
         $this->assertEquals(123, $accessProfile->getAvatarId());
         $this->assertFalse($accessProfile->getIsSuperAdminAccess());
         $this->assertEquals($this->organizationProfile, $accessProfile->getOrganization());
+        $this->assertEquals(1, $accessProfile->getAccessPreferenceId());
+    }
+
+    /**
+     * If the access has no billingPayers, has AccessIntangible but also have children, isPayor shall be set to false.
+     *
+     * @see AccessProfileCreator::createAccessPrefence()
+     */
+    public function testCreateAccessPreference(): void
+    {
+        $accessProfileCreator = $this
+            ->getMockBuilder(AccessProfileCreator::class)
+            ->setConstructorArgs([$this->organizationProfileCreator, $this->accessRepository, $this->accessUtils, $this->entityManager])
+            ->setMethodsExcept(['createAccessPrefence'])
+            ->getMock();
+
+        $this->access->expects(self::once())->method('setPreferences');
+        $this->entityManager->expects(self::once())->method('flush')->with($this->access);
+
+        $accessProfileCreator->createAccessPrefence($this->access);
     }
 }