Skip to content

Commit

Permalink
fix repositories in context of issue llaville/php-compatinfo#372
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Mar 18, 2024
1 parent c9b25e2 commit 7618387
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Entity\Class_ as ClassEntity;
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Hydrator\ClassHydrator;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;

use function version_compare;
use const PHP_VERSION;

/**
* @since Release 3.2.0
* @author Laurent Laville
Expand Down Expand Up @@ -52,7 +56,19 @@ public function getAll(): array
*/
public function getClassByName(string $name, bool $isInterface): ?Class_
{
$entity = $this->repository->findOneBy(['name' => $name, 'isInterface' => $isInterface]);
$criteria = new Criteria();
$criteria->where(Criteria::expr()->eq('name', $name));
$criteria->andWhere(Criteria::expr()->eq('isInterface', $isInterface));
$criteria->orderBy(['phpMin' => 'desc']);

$collection = $this->repository->matching($criteria);

$entity = $collection->isEmpty()
? null
: $collection->filter(
fn($function) => version_compare($function->getPhpMin(), PHP_VERSION, 'le')
)->first()
;

if (null === $entity) {
// class does not exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Entity\Constant_ as ConstantEntity;
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Hydrator\ConstantHydrator;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;

use function version_compare;
use const PHP_VERSION;

/**
* @since Release 3.2.0
* @author Laurent Laville
Expand Down Expand Up @@ -52,17 +56,26 @@ public function getAll(): array
*/
public function getConstantByName(string $name, ?string $declaringClass): ?Constant_
{
$criteria = new Criteria();

if (strpos($name, '\\') === false) {
// standard constant should be uppercase in database
$criteria = ['name' => strtoupper($name)];
$criteria->where(Criteria::expr()->eq('name', strtoupper($name)));
} else {
// special case for constants that have namespace like in ast extension
$criteria = ['name' => $name];
}
if ($declaringClass !== null) {
$criteria['declaringClass'] = $declaringClass;
$criteria->where(Criteria::expr()->eq('name', $name));
}
$entity = $this->repository->findOneBy($criteria);
$criteria->andWhere(Criteria::expr()->eq('declaringClass', $declaringClass));
$criteria->orderBy(['phpMin' => 'desc']);

$collection = $this->repository->matching($criteria);

$entity = $collection->isEmpty()
? null
: $collection->filter(
fn($function) => version_compare($function->getPhpMin(), PHP_VERSION, 'le')
)->first()
;

if (null === $entity) {
// function does not exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Entity\Function_ as FunctionEntity;
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Hydrator\FunctionHydrator;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;

use function version_compare;
use const PHP_VERSION;

/**
* @since Release 3.2.0
* @author Laurent Laville
Expand Down Expand Up @@ -52,7 +56,19 @@ public function getAll(): array
*/
public function getFunctionByName(string $name, ?string $declaringClass): ?Function_
{
$entity = $this->repository->findOneBy(['name' => $name, 'declaringClass' => $declaringClass]);
$criteria = new Criteria();
$criteria->where(Criteria::expr()->eq('name', $name));
$criteria->andWhere(Criteria::expr()->eq('declaringClass', $declaringClass));
$criteria->orderBy(['phpMin' => 'desc']);

$collection = $this->repository->matching($criteria);

$entity = $collection->isEmpty()
? null
: $collection->filter(
fn($function) => version_compare($function->getPhpMin(), PHP_VERSION, 'le')
)->first()
;

if (null === $entity) {
// function does not exists
Expand Down
58 changes: 58 additions & 0 deletions tests/Database/ClassRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);
/**
* This file is part of the PHP_CompatInfoDB package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\CompatInfoDb\Tests\Database;

use Bartlett\CompatInfoDb\Application\Kernel\ConsoleKernel;
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Repository\ClassRepository;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Component\Console\Input\ArrayInput;

use function version_compare;
use const PHP_VERSION;

/**
* Database functional tests for PHP_CompatInfo_Db.
*
* @since Release 6.4.1
* @author Laurent Laville
*/
class ClassRepositoryTest extends \PHPUnit\Framework\TestCase
{
private EntityManagerInterface $entityManager;
private ClassRepository $classRepository;

protected function setUp(): void
{
$kernel = new ConsoleKernel($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'tests', false);

$container = $kernel->createFromInput(new ArrayInput([]));

$this->entityManager = $container->get(EntityManagerInterface::class);

$this->classRepository = new ClassRepository($this->entityManager);
}

protected function tearDown(): void
{
$this->entityManager->close();
}

public function testGetClassByName(): void
{
$class = $this->classRepository->getClassByName('AMQPException', false);

if (version_compare(PHP_VERSION, '7.4.0', 'ge')) {
$expectedVersion = '7.4.0';
} else {
$expectedVersion = '5.2.0';
}
$this->assertSame($expectedVersion, $class->getPhpMin());
}
}
58 changes: 58 additions & 0 deletions tests/Database/ConstantRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);
/**
* This file is part of the PHP_CompatInfoDB package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\CompatInfoDb\Tests\Database;

use Bartlett\CompatInfoDb\Application\Kernel\ConsoleKernel;
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Repository\ConstantRepository;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Component\Console\Input\ArrayInput;

use function version_compare;
use const PHP_VERSION;

/**
* Database functional tests for PHP_CompatInfo_Db.
*
* @since Release 6.4.1
* @author Laurent Laville
*/
class ConstantRepositoryTest extends \PHPUnit\Framework\TestCase
{
private EntityManagerInterface $entityManager;
private ConstantRepository $constantRepository;

protected function setUp(): void
{
$kernel = new ConsoleKernel($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'tests', false);

$container = $kernel->createFromInput(new ArrayInput([]));

$this->entityManager = $container->get(EntityManagerInterface::class);

$this->constantRepository = new ConstantRepository($this->entityManager);
}

protected function tearDown(): void
{
$this->entityManager->close();
}

public function testGetConstantByName(): void
{
$constant = $this->constantRepository->getConstantByName('T_BAD_CHARACTER', null);

if (version_compare(PHP_VERSION, '7.4.0', 'ge')) {
$expectedVersion = '7.4.0beta1';
} else {
$expectedVersion = '4.2.0';
}
$this->assertSame($expectedVersion, $constant->getPhpMin());
}
}
58 changes: 58 additions & 0 deletions tests/Database/FunctionRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);
/**
* This file is part of the PHP_CompatInfoDB package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\CompatInfoDb\Tests\Database;

use Bartlett\CompatInfoDb\Application\Kernel\ConsoleKernel;
use Bartlett\CompatInfoDb\Infrastructure\Persistence\Doctrine\Repository\FunctionRepository;

use Doctrine\ORM\EntityManagerInterface;

use Symfony\Component\Console\Input\ArrayInput;

use function version_compare;
use const PHP_VERSION;

/**
* Database functional tests for PHP_CompatInfo_Db.
*
* @since Release 6.4.1
* @author Laurent Laville
*/
class FunctionRepositoryTest extends \PHPUnit\Framework\TestCase
{
private EntityManagerInterface $entityManager;
private FunctionRepository $functionRepository;

protected function setUp(): void
{
$kernel = new ConsoleKernel($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'tests', false);

$container = $kernel->createFromInput(new ArrayInput([]));

$this->entityManager = $container->get(EntityManagerInterface::class);

$this->functionRepository = new FunctionRepository($this->entityManager);
}

protected function tearDown(): void
{
$this->entityManager->close();
}

public function testGetFunctionByName(): void
{
$constant = $this->functionRepository->getFunctionByName('random_int', null);

if (version_compare(PHP_VERSION, '8.2.0', 'ge')) {
$expectedVersion = '8.2.0';
} else {
$expectedVersion = '7.0.2';
}
$this->assertSame($expectedVersion, $constant->getPhpMin());
}
}

0 comments on commit 7618387

Please sign in to comment.