diff --git a/Entity/LibrariesHubCache.php b/Entity/LibrariesHubCache.php index cc45f85..03093a4 100644 --- a/Entity/LibrariesHubCache.php +++ b/Entity/LibrariesHubCache.php @@ -4,7 +4,7 @@ use Doctrine\ORM\Mapping as ORM; -#[ORM\Entity()] +#[ORM\Entity] #[ORM\Table('h5p_libraries_hub_cache')] class LibrariesHubCache { diff --git a/Entity/LibrariesLanguagesRepository.php b/Entity/LibrariesLanguagesRepository.php index 0831fa6..a5ea268 100644 --- a/Entity/LibrariesLanguagesRepository.php +++ b/Entity/LibrariesLanguagesRepository.php @@ -5,13 +5,14 @@ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\NoResultException; use Doctrine\Persistence\ManagerRegistry; +use Studit\H5PBundle\Service\DoctrineParser; /** * LibrariesLanguagesRepository */ class LibrariesLanguagesRepository extends ServiceEntityRepository { - public function __construct(ManagerRegistry $registry) + public function __construct(ManagerRegistry $registry, private readonly DoctrineParser $parser) { parent::__construct($registry, LibrariesLanguages::class); } @@ -22,10 +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(['majorVersion' => $majorVersion, 'machineName' => $machineName, 'minorVersion' => $minorVersion, 'languageCode' => $languageCode]); + ->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; @@ -35,10 +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(['majorVersion' => $majorVersion, 'machineName' => $machineName, 'minorVersion' => $minorVersion]); + ->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. diff --git a/Entity/Library.php b/Entity/Library.php index 902d2b3..0556bac 100644 --- a/Entity/Library.php +++ b/Entity/Library.php @@ -116,7 +116,7 @@ class Library #[ORM\OneToMany(targetEntity: ContentLibraries::class, mappedBy: "library")] /** - * @var ArrayCollection|Collection + * @var ArrayCollection|Collection $contentLibraries */ private ArrayCollection|Collection $contentLibraries; /** diff --git a/Entity/LibraryRepository.php b/Entity/LibraryRepository.php index 212f266..44b8094 100644 --- a/Entity/LibraryRepository.php +++ b/Entity/LibraryRepository.php @@ -7,6 +7,7 @@ use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\NoResultException; use Doctrine\Persistence\ManagerRegistry; +use Studit\H5PBundle\Service\DoctrineParser; /** * LibraryRepository @@ -14,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); } @@ -90,16 +90,17 @@ 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([ + ->setParameters($this->parser->buildParams([ 'machineName' => $machineName, 'majorVersion' => $majorVersion, 'minorVersion' => $minorVersion - ]); + ])); try { $library = $qb->getQuery()->getSingleResult(); } catch (NoResultException $e) { @@ -107,6 +108,7 @@ public function findHasSemantics($machineName, $majorVersion, $minorVersion) } return (object)$library; } + public function findAllRunnableWithSemantics() { $qb = $this->createQueryBuilder('l') @@ -119,40 +121,43 @@ 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($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([ + ->setParameters($this->parser->buildParams([ 'machineName' => $machineName, 'majorVersion' => $majorVersion, 'minorVersion' => $minorVersion - ]); + ])); try { return $qb->getQuery()->getSingleScalarResult(); } catch (NoResultException $e) { 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([ + ->setParameters($this->parser->buildParams([ 'machineName' => $library['machineName'], 'majorVersion' => $library['majorVersion'], 'minorVersion' => $library['minorVersion'], 'patchVersion' => $library['patchVersion'] - ]); + ])); return $qb->getQuery()->getSingleScalarResult() > 0; } } diff --git a/Service/DoctrineParser.php b/Service/DoctrineParser.php new file mode 100644 index 0000000..bbeaf5b --- /dev/null +++ b/Service/DoctrineParser.php @@ -0,0 +1,46 @@ +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 function buildParams(array $params): ArrayCollection|array + { + $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) { + $paramsCollection[] = new \Doctrine\ORM\Query\Parameter($k, $val); + } + + return new ArrayCollection($paramsCollection); + } + // For Doctrine ORM v2, return the parameters as is + return $params; + } +} diff --git a/Tests/Service/DoctrineParserTest.php b/Tests/Service/DoctrineParserTest.php new file mode 100644 index 0000000..4825c74 --- /dev/null +++ b/Tests/Service/DoctrineParserTest.php @@ -0,0 +1,78 @@ +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); + } +} diff --git a/Utils/VersionORM.php b/Utils/VersionORM.php new file mode 100644 index 0000000..a85d672 --- /dev/null +++ b/Utils/VersionORM.php @@ -0,0 +1,25 @@ += 8.1", + "composer-runtime-api": "^2", "doctrine/orm": "~2.0|~3.0", "guzzlehttp/guzzle": "^7.9", "h5p/h5p-core": "1.27",