<commit_before><?php
namespace pdt256\article\RepositoryHydration\Repository;

use pdt256\article\RepositoryHydration\Entity\Company;
use pdt256\article\RepositoryHydration\Entity\Employee;
use pdt256\article\tests\Helper\TestCase\RepositoryTestCase;

abstract class AbstractDoctrineCompanyRepositoryTest extends RepositoryTestCase
{
    protected $metaDataClassNames = [
        Company::class,
        Employee::class,
    ];

    /** @var CompanyRepositoryInterface */
    protected $companyRepository;

    public function setUp()
    {
        parent::setUp();
        $this->companyRepository = $this->getCompanyRepository();
    }

    abstract protected function getCompanyRepository(): CompanyRepositoryInterface;

    public function testGetCompanyStats()
    {
        $company = $this->getDummyActiveCompany();

        $employee1 = $this->getDummyActiveEmployee(1, $company);
        $employee2 = $this->getDummyActiveEmployee(2, $company);
        $employee3 = $this->getDummyActiveEmployee(3, $company);
        $employee3->setIsActive(false);

        $this->entityManager->persist($company);
        $this->entityManager->persist($employee1);
        $this->entityManager->persist($employee2);
        $this->entityManager->persist($employee3);
        $this->entityManager->flush();
        $this->entityManager->clear();

        $companyStats = $this->companyRepository->getCompanyStats($company->getId());

        $this->assertSame(2, $companyStats->totalActiveEmployees());
        $this->assertSame(1, $companyStats->totalInactiveEmployees());
    }

    public function testGetCompanyStatsNoEmployees()
    {
        $company = $this->getDummyActiveCompany();

        $this->entityManager->persist($company);
        $this->entityManager->flush();
        $this->entityManager->clear();

        $companyStats = $this->companyRepository->getCompanyStats($company->getId());

        $this->assertSame(0, $companyStats->totalActiveEmployees());
        $this->assertSame(0, $companyStats->totalInactiveEmployees());
    }

    private function getDummyActiveCompany(): Company
    {
        $company = new Company;
        $company->setName('Acme');
        $company->setIsActive(true);

        return $company;
    }

    private function getDummyActiveEmployee(int $num = 1, Company $company = null): Employee
    {
        if ($company === null) {
            $company = $this->getDummyActiveCompany();
        }

        $employee = new Employee;
        $employee->setName('<NAME> ' . $num);
        $employee->setIsActive(true);
        $employee->setCompany($company);

        return $employee;
    }
}<commit_msg>Refactoring test for company stats.<commit_after><?php
namespace pdt256\article\RepositoryHydration\Repository;

use pdt256\article\RepositoryHydration\Entity\Company;
use pdt256\article\RepositoryHydration\Entity\Employee;
use pdt256\article\tests\Helper\TestCase\RepositoryTestCase;

abstract class AbstractDoctrineCompanyRepositoryTest extends RepositoryTestCase
{
    protected $metaDataClassNames = [
        Company::class,
        Employee::class,
    ];

    /** @var CompanyRepositoryInterface */
    protected $companyRepository;

    public function setUp()
    {
        parent::setUp();
        $this->companyRepository = $this->getCompanyRepository();
    }

    abstract protected function getCompanyRepository(): CompanyRepositoryInterface;

    public function testGetCompanyStats()
    {
        $company = $this->getDummyActiveCompany();

        $employee1 = $this->getDummyActiveEmployee($company, 1);
        $employee2 = $this->getDummyActiveEmployee($company, 2);
        $employee3 = $this->getDummyInactiveEmployee($company, 3);

        $this->entityManager->persist($company);
        $this->entityManager->persist($employee1);
        $this->entityManager->persist($employee2);
        $this->entityManager->persist($employee3);
        $this->entityManager->flush();
        $this->entityManager->clear();

        $companyStats = $this->companyRepository->getCompanyStats($company->getId());

        $this->assertSame(2, $companyStats->totalActiveEmployees());
        $this->assertSame(1, $companyStats->totalInactiveEmployees());
    }

    public function testGetCompanyStatsNoEmployees()
    {
        $company = $this->getDummyActiveCompany();

        $this->entityManager->persist($company);
        $this->entityManager->flush();
        $this->entityManager->clear();

        $companyStats = $this->companyRepository->getCompanyStats($company->getId());

        $this->assertSame(0, $companyStats->totalActiveEmployees());
        $this->assertSame(0, $companyStats->totalInactiveEmployees());
    }

    private function getDummyActiveCompany(): Company
    {
        $company = new Company;
        $company->setName('Acme');
        $company->setIsActive(true);

        return $company;
    }

    private function getDummyActiveEmployee(Company $company, int $num = 1): Employee
    {
        $employee = $this->getDummyEmployee($company, $num);
        $employee->setIsActive(true);

        return $employee;
    }

    private function getDummyInactiveEmployee(Company $company, int $num = 1): Employee
    {
        $employee = $this->getDummyEmployee($company, $num);
        $employee->setIsActive(false);

        return $employee;
    }

    private function getDummyEmployee(Company $company, int $num = 1): Employee
    {
        $employee = new Employee;
        $employee->setName('<NAME> ' . $num);
        $employee->setCompany($company);

        return $employee;
    }
}
{'fraction_non_alphanumeric': 0.1354110693175712, 'fraction_numerical': 0.009851334408024359, 'mean_word_length': 3.386488609583661, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 98, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3838401', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1482, 'n_words': 240}