brilo-example/tests/Db/Users/CompanyRepositoryTest.php

80 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Db\Users;
use App\Entity\Database\Users\Company;
use App\Repository\Users\CompanyRepository;
use App\Tests\Common\DatabaseTestTrait;
use App\Tests\Common\FakerTrait;
use App\Tests\Common\Generators\UserGeneratorTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class CompanyRepositoryTest extends KernelTestCase
{
use FakerTrait;
use DatabaseTestTrait;
use UserGeneratorTrait;
public function setUp(): void
{
parent::bootKernel();
$this->bootFaker();
$this->bootDatabase();
$this->getFaker()->unique();
}
public function testFindByNameWillReturnCompany(): void
{
$c1 = new Company(
1,
new ArrayCollection(),
$this->getFaker()->company(),
$this->getFaker()->text(50),
$this->getFaker()->text(100)
);
$this->getEntityManager()->persist($c1);
$this->getEntityManager()->persist(new Company(
2,
new ArrayCollection(),
$this->getFaker()->company(),
$this->getFaker()->text(50),
$this->getFaker()->text(100)
));
$this->getEntityManager()->flush();
/**
* @var CompanyRepository $repository
*/
$repository = parent::getContainer()->get(CompanyRepository::class);
$this->assertNotNull($repository->findByName($c1->name));
$this->assertNull($repository->findByName($this->getFaker()->text()));
}
public function testDeleteOrphanRecordsWillDeleteCompaniesWithoutUsers(): void
{
$c1 = new Company(
1,
new ArrayCollection(),
$this->getFaker()->company(),
$this->getFaker()->text(50),
$this->getFaker()->text(100)
);
$user = $this->createUser(1);
$this->getEntityManager()->persist($c1);
$this->getEntityManager()->persist($user->address);
$this->getEntityManager()->persist($user->company);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
/**
* @var CompanyRepository $repository
*/
$repository = $this->getContainer()->get(CompanyRepository::class);
$repository->deleteOrphanRecords();
$result = $repository->findAll();
$this->assertCount(1, $result);
$this->assertSame($user->company, $result[0]);
}
}