Skip to content

Commit

Permalink
feat(chore): refactor some stuff for better output and implement test
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisdugue committed Dec 5, 2024
1 parent 83a7265 commit fe90047
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 15 deletions.
10 changes: 5 additions & 5 deletions Entity/LibrariesLanguagesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class LibrariesLanguagesRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
public function __construct(ManagerRegistry $registry, private readonly DoctrineParser $parser)
{
parent::__construct($registry, LibrariesLanguages::class);
}
Expand All @@ -23,15 +23,15 @@ public function findForLibrary($machineName, $majorVersion, $minorVersion, $lang
->select('ll.languageJson')
->join('ll.library', 'l', 'WITH', 'l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion')
->where('ll.languageCode = :languageCode')
->setParameters(DoctrineParser::buildParams([
->setParameters($this->parser->buildParams([
'majorVersion' => $majorVersion,
'machineName' => $machineName,
'minorVersion' => $minorVersion,
'languageCode' => $languageCode
]));
try {
$result = $qb->getQuery()->getSingleResult();
} catch (NoResultException $e) {
} catch (NoResultException) {
return null;
}
return $result['languageJson'] ? $result['languageJson'] : null;
Expand All @@ -41,14 +41,14 @@ public function findForLibraryAllLanguages($machineName, $majorVersion, $minorVe
$qb = $this->createQueryBuilder('ll')
->select('ll.languageCode')
->join('ll.library', 'l', 'WITH', 'l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion')
->setParameters(DoctrineParser::buildParams([
->setParameters($this->parser->buildParams([
'majorVersion' => $majorVersion,
'machineName' => $machineName,
'minorVersion' => $minorVersion
]));
try {
$results = $qb->getQuery()->getArrayResult();
} catch (NoResultException $e) {
} catch (NoResultException) {
return null;
}
$codes = array('en'); // Semantics is 'en' by default.
Expand Down
16 changes: 10 additions & 6 deletions Entity/LibraryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
* This class was generated by the PhpStorm "Php Annotations" Plugin. Add your own custom
* repository methods below.
*/

class LibraryRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
public function __construct(ManagerRegistry $registry, private readonly DoctrineParser $parser)
{
parent::__construct($registry, Library::class);
}
Expand Down Expand Up @@ -91,12 +90,13 @@ public function findLatestLibraryVersions(): array
}
return $libraryVersions;
}

public function findHasSemantics($machineName, $majorVersion, $minorVersion)
{
$qb = $this->createQueryBuilder('l')
->select('l')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.semantics is not null')
->setParameters(DoctrineParser::buildParams([
->setParameters($this->parser->buildParams([
'machineName' => $machineName,
'majorVersion' => $majorVersion,
'minorVersion' => $minorVersion
Expand All @@ -108,6 +108,7 @@ public function findHasSemantics($machineName, $majorVersion, $minorVersion)
}
return (object)$library;
}

public function findAllRunnableWithSemantics()
{
$qb = $this->createQueryBuilder('l')
Expand All @@ -120,19 +121,21 @@ public function findAllRunnableWithSemantics()
}
return $libraries;
}

public function findOneArrayBy($parameters)
{
$qb = $this->createQueryBuilder('l')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion')
->setParameters(DoctrineParser::buildParams($parameters));
->setParameters($this->parser->buildParams($parameters));
return $qb->getQuery()->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY);
}

public function findIdBy($machineName, $majorVersion, $minorVersion)
{
$qb = $this->createQueryBuilder('l')
->select('l.id')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.semantics is not null')
->setParameters(DoctrineParser::buildParams([
->setParameters($this->parser->buildParams([
'machineName' => $machineName,
'majorVersion' => $majorVersion,
'minorVersion' => $minorVersion
Expand All @@ -143,12 +146,13 @@ public function findIdBy($machineName, $majorVersion, $minorVersion)
return null;
}
}

public function isPatched($library): bool
{
$qb = $this->createQueryBuilder('l')
->select('COUNT(l)')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.patchVersion < :patchVersion')
->setParameters(DoctrineParser::buildParams([
->setParameters($this->parser->buildParams([
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
Expand Down
16 changes: 12 additions & 4 deletions Service/DoctrineParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Composer\InstalledVersions;
use Doctrine\Common\Collections\ArrayCollection;
use Studit\H5PBundle\Utils\VersionORM;

/**
* This class exists to prevent breaking changes when working with different versions of Doctrine ORM.
Expand All @@ -12,21 +13,28 @@
*/
class DoctrineParser
{
private VersionORM $versionORM;

public function __construct(VersionORM $versionORM)
{
$this->versionORM = $versionORM;
}

/**
* This method converts parameters to an ArrayCollection for ORM v3.
* If using ORM v2, it simply returns the received parameters as is.
*
* @param array $params The input parameters to process.
* @return ArrayCollection|array Returns an ArrayCollection for ORM v3 or the original parameters for ORM v2.
*/
public static function buildParams(array $params): ArrayCollection|array
public function buildParams(array $params): ArrayCollection|array
{
$doctrineVersion = InstalledVersions::getVersion('doctrine/orm');
$doctrineVersion = $this->versionORM->getDoctrineVersion();
if ($doctrineVersion !== null && str_starts_with($doctrineVersion, '3')) {
// For Doctrine ORM v3, ensure the parameters are returned as an ArrayCollection
$paramsCollection = [];

foreach ($params as $k => $val){
foreach ($params as $k => $val) {
$paramsCollection[] = new \Doctrine\ORM\Query\Parameter($k, $val);
}

Expand All @@ -35,4 +43,4 @@ public static function buildParams(array $params): ArrayCollection|array
// For Doctrine ORM v2, return the parameters as is
return $params;
}
}
}
78 changes: 78 additions & 0 deletions Tests/Service/DoctrineParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Studit\H5PBundle\Tests\Service;

use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Studit\H5PBundle\Service\DoctrineParser;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Query\Parameter;
use Studit\H5PBundle\Utils\VersionORM;

class DoctrineParserTest extends TestCase
{
/**
* Test that buildParams returns an ArrayCollection for Doctrine ORM v3.
* @throws Exception
*/
public function testBuildParamsForDoctrineV3()
{
// Mock VersionProvider to simulate Doctrine v2 version
$mockVersionORM = $this->createMock(VersionORM::class);
$mockVersionORM->method('getDoctrineVersion')
->willReturn('3.1.0'); // Simulate Doctrine v2 version
// Inject the mocked version provider into DoctrineParser
$doctrineParser = new DoctrineParser($mockVersionORM);

// Define test parameters
$params = [
'param1' => 'value1',
'param2' => 'value2',
];

// Call the method under test
$result = $doctrineParser->buildParams($params);

// Assert that the result is an instance of ArrayCollection
$this->assertInstanceOf(ArrayCollection::class, $result);

// Assert that the ArrayCollection contains Parameter objects
foreach ($result as $param) {
$this->assertInstanceOf(Parameter::class, $param);
}

// Assert that the parameters inside the Parameter objects match the input parameters
$this->assertEquals('value1', $result[0]->getValue());
$this->assertEquals('value2', $result[1]->getValue());
}

/**
* Test that buildParams returns the original parameters as an array for Doctrine ORM v2.
* @throws Exception
*/
public function testBuildParamsForDoctrineV2()
{
// Mock VersionProvider to simulate Doctrine v2 version
$mockVersionORM = $this->createMock(VersionORM::class);
$mockVersionORM->method('getDoctrineVersion')
->willReturn('2.9.3'); // Simulate Doctrine v2 version

// Inject the mocked version provider into DoctrineParser
$doctrineParser = new DoctrineParser($mockVersionORM);

// Define test parameters
$params = [
'param1' => 'value1',
'param2' => 'value2',
];

// Call the method under test
$result = $doctrineParser->buildParams($params);

// Assert that the result is an array (not an ArrayCollection)
$this->assertIsArray($result);

// Assert that the returned array contains the same values as the input parameters
$this->assertSame($params, $result);
}
}
25 changes: 25 additions & 0 deletions Utils/VersionORM.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Studit\H5PBundle\Utils;

use Composer\InstalledVersions;

/**
* VersionProvider class provides a simple interface to retrieve the version of the Doctrine ORM package.
* It wraps the `InstalledVersions::getVersion` method, allowing easier testing and version checking.
*/
class VersionORM
{
/**
* Retrieves the installed version of the Doctrine ORM package.
*
* This method calls `InstalledVersions::getVersion('doctrine/orm')` and returns the version string
* for the Doctrine ORM package if available. If the package is not found, it returns null.
*
* @return string|null The version of Doctrine ORM if installed, null otherwise.
*/
public function getDoctrineVersion(): ?string
{
return InstalledVersions::getVersion('doctrine/orm');
}
}

0 comments on commit fe90047

Please sign in to comment.