mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 03:07:58 +00:00 
			
		
		
		
	Optimized AbstractSource entity for members
This commit is contained in:
		@@ -4,11 +4,13 @@ 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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @todo refactor this to an integration test!
 | 
			
		||||
 *
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
class UserSourceRepositoryTest extends KernelTestCase
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
@@ -16,70 +18,51 @@ class UserSourceRepositoryTest extends KernelTestCase
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
    public function testAddAndRemoveMember(): void
 | 
			
		||||
    {
 | 
			
		||||
        $userSource2 = new UserSource();
 | 
			
		||||
        $this->userSource->getMemberships()->add($userSource2);
 | 
			
		||||
        $this->entityManager->persist($this->userSource);
 | 
			
		||||
        $insertSource = new UserSource();
 | 
			
		||||
        $origineSource = new UserSource();
 | 
			
		||||
        $origineSource->addMember($insertSource);
 | 
			
		||||
        $this->entityManager->persist($insertSource);
 | 
			
		||||
        $this->entityManager->persist($origineSource);
 | 
			
		||||
        $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->assertGreaterThan(0, $insertSource->getId());
 | 
			
		||||
        $this->assertGreaterThan(0, $origineSource->getId());
 | 
			
		||||
        $this->assertEquals($insertSource, $origineSource->getMembers()
 | 
			
		||||
            ->get(0));
 | 
			
		||||
        $this->assertEquals($origineSource, $insertSource->getMemberships()
 | 
			
		||||
            ->get(0));
 | 
			
		||||
        $this->assertNull($origineSource->removeMember($insertSource));
 | 
			
		||||
        $this->assertEquals(0, $origineSource->getMembers()
 | 
			
		||||
            ->count());
 | 
			
		||||
        $this->assertEquals(0, $insertSource->getMemberships()
 | 
			
		||||
            ->count());
 | 
			
		||||
        $this->entityManager->remove($origineSource);
 | 
			
		||||
        $this->entityManager->flush();
 | 
			
		||||
        $this->assertNull($this->userSourceRepository->find($userSource2Id));
 | 
			
		||||
        $this->assertGreaterThan(0, $insertSource->getId());
 | 
			
		||||
        $this->entityManager->remove($insertSource);
 | 
			
		||||
        $this->entityManager->flush();
 | 
			
		||||
        $this->expectException(\TypeError::class);
 | 
			
		||||
        $insertSource->getId();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testCreation(): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->entityManager->persist($this->userSource);
 | 
			
		||||
        $userSource = new UserSource();
 | 
			
		||||
        $this->entityManager->persist($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);
 | 
			
		||||
        $userSourceId = $userSource->getId();
 | 
			
		||||
        $loadedUserSource = $this->entityManager->getRepository(UserSource::class)->find($userSourceId);
 | 
			
		||||
        $this->assertEquals($userSourceId, $loadedUserSource->getId());
 | 
			
		||||
        $this->entityManager->remove($loadedUserSource);
 | 
			
		||||
        $this->entityManager->flush();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user