mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 11:17:58 +00:00 
			
		
		
		
	Optimized membership attribute
This commit is contained in:
		@@ -1,5 +1,4 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Entity\Source;
 | 
			
		||||
 | 
			
		||||
use Doctrine\ORM\Mapping as ORM;
 | 
			
		||||
@@ -20,8 +19,9 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 | 
			
		||||
use Symfony\Component\Validator\Constraints as Assert;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 *
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 *        
 | 
			
		||||
 * @ORM\Entity
 | 
			
		||||
 * @ORM\Table(name="source")
 | 
			
		||||
 * @ORM\InheritanceType("JOINED")
 | 
			
		||||
@@ -57,6 +57,7 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
 | 
			
		||||
    protected $slug;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @var RelationInterface
 | 
			
		||||
     * @ORM\OneToOne(targetEntity="App\Entity\Meta\Relation",cascade={"persist", "remove"})
 | 
			
		||||
     * @ORM\JoinColumn(name="relation_id", referencedColumnName="id", onDelete="CASCADE")
 | 
			
		||||
@@ -65,15 +66,25 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
 | 
			
		||||
    protected $relation;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @todo Implement that just one table on database level is needed!
 | 
			
		||||
     * @todo Rename table to use the right schema
 | 
			
		||||
     * Many Sources have many Source Members
 | 
			
		||||
     * @var Collection|SourceInterface[]
 | 
			
		||||
     * @ORM\ManyToMany(targetEntity="AbstractSource", inversedBy="members",cascade={"persist"})
 | 
			
		||||
     * @ORM\JoinTable(name="source_members",
 | 
			
		||||
     *      joinColumns={@ORM\JoinColumn(name="source_id", referencedColumnName="id")},
 | 
			
		||||
     *      inverseJoinColumns={@ORM\JoinColumn(name="member_id", referencedColumnName="id")}
 | 
			
		||||
     *      )
 | 
			
		||||
     */
 | 
			
		||||
    protected $members;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @var Collection|TreeCollectionSourceInterface[]
 | 
			
		||||
     * @ORM\ManyToMany(targetEntity="App\Entity\Source\Complex\Collection\TreeCollectionSource",mappedBy="collection")
 | 
			
		||||
     * @var Collection|SourceInterface[]
 | 
			
		||||
     * @ORM\ManyToMany(targetEntity="AbstractSource",mappedBy="members",cascade={"persist"})
 | 
			
		||||
     */
 | 
			
		||||
    protected $memberships;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @ORM\OneToOne(targetEntity="App\Entity\Meta\Law",cascade={"persist", "remove"})
 | 
			
		||||
     * @ORM\JoinColumn(name="law_id", referencedColumnName="id")
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								application/src/Repository/UserSourceRepository.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								application/src/Repository/UserSourceRepository.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Repository;
 | 
			
		||||
 | 
			
		||||
use Doctrine\ORM\EntityRepository;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
class UserSourceRepository extends EntityRepository
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
@@ -7,7 +7,7 @@ use Doctrine\ORM\EntityManager;
 | 
			
		||||
use App\Repository\UserRepository;
 | 
			
		||||
use App\Entity\User;
 | 
			
		||||
use App\Entity\UserInterface;
 | 
			
		||||
use App\Entity\Source\Combination\PersonIdentitySourceInterface;
 | 
			
		||||
use App\Entity\Source\Complex\PersonIdentitySourceInterface;
 | 
			
		||||
use App\Entity\Source\Complex\PersonIdentitySource;
 | 
			
		||||
 | 
			
		||||
class UserRepositoryTest extends KernelTestCase
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										103
									
								
								application/tests/Unit/Repository/UserSourceRepositoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								application/tests/Unit/Repository/UserSourceRepositoryTest.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,103 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace tests\Unit\Repository;
 | 
			
		||||
 | 
			
		||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
 | 
			
		||||
use Doctrine\ORM\EntityManager;
 | 
			
		||||
use App\Entity\UserInterface;
 | 
			
		||||
use App\Repository\UserSourceRepository;
 | 
			
		||||
use App\Entity\Source\Complex\UserSourceInterface;
 | 
			
		||||
use App\Entity\Source\Complex\UserSource;
 | 
			
		||||
 | 
			
		||||
class UserSourceRepositoryTest extends KernelTestCase
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @var EntityManager
 | 
			
		||||
     */
 | 
			
		||||
    protected $entityManager;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @var UserSourceRepository
 | 
			
		||||
     */
 | 
			
		||||
    protected $userSourceRepository;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @var UserSourceInterface
 | 
			
		||||
     */
 | 
			
		||||
    protected $loadedUserSource;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @var UserInterface
 | 
			
		||||
     */
 | 
			
		||||
    protected $user;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @var UserSourceInterface
 | 
			
		||||
     */
 | 
			
		||||
    protected $userSource;
 | 
			
		||||
 | 
			
		||||
    public function setUp(): void
 | 
			
		||||
    {
 | 
			
		||||
        $kernel = self::bootKernel();
 | 
			
		||||
        $this->entityManager = $kernel->getContainer()
 | 
			
		||||
            ->get('doctrine')
 | 
			
		||||
            ->getManager();
 | 
			
		||||
        $this->userSourceRepository = $this->entityManager->getRepository(UserSource::class);
 | 
			
		||||
        $this->userSource = new UserSource();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testMemberships(): void
 | 
			
		||||
    {
 | 
			
		||||
        $userSource2 = new UserSource();
 | 
			
		||||
        $this->userSource->getMemberships()->add($userSource2);
 | 
			
		||||
        $this->entityManager->persist($this->userSource);
 | 
			
		||||
        $this->entityManager->flush();
 | 
			
		||||
        $userSource2Id = $userSource2->getId();
 | 
			
		||||
        $userSourceId = $this->userSource->getId();
 | 
			
		||||
        $this->loadedUserSource = $this->userSourceRepository->find($userSourceId);
 | 
			
		||||
        $this->assertGreaterThan(0, $userSource2Id);
 | 
			
		||||
        $this->assertEquals($userSource2, $this->loadedUserSource->getMemberships()->get(0));
 | 
			
		||||
        $this->deleteUserSource();
 | 
			
		||||
        $this->assertGreaterThan(0, $userSource2Id);
 | 
			
		||||
        $this->entityManager->remove($userSource2);
 | 
			
		||||
        $this->entityManager->flush();
 | 
			
		||||
        $this->assertNull($this->userSourceRepository->find($userSource2Id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testCreation(): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->entityManager->persist($this->userSource);
 | 
			
		||||
        $this->entityManager->flush();
 | 
			
		||||
        $userSourceId = $this->userSource->getId();
 | 
			
		||||
        /*
 | 
			
		||||
         * @var UserSourceInterface
 | 
			
		||||
         */
 | 
			
		||||
        $this->loadedUserSource = $this->userSourceRepository->find($userSourceId);
 | 
			
		||||
        $this->assertEquals($userSourceId, $this->loadedUserSource->getId());
 | 
			
		||||
        $this->deleteUserSource();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function deleteUserSource(): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->entityManager->remove($this->loadedUserSource);
 | 
			
		||||
        $this->entityManager->flush();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     *
 | 
			
		||||
     * @see \Symfony\Bundle\FrameworkBundle\Test\KernelTestCase::tearDown()
 | 
			
		||||
     */
 | 
			
		||||
    protected function tearDown(): void
 | 
			
		||||
    {
 | 
			
		||||
        parent::tearDown();
 | 
			
		||||
        $this->entityManager->close();
 | 
			
		||||
        $this->entityManager = null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user