From 50fdcac7d73c3b44812c3f05bbaf5432065de4b5 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 22 Feb 2024 09:48:40 +0100 Subject: [PATCH] Backport QueryParameterTest --- .../ORM/Functional/QueryParameterTest.php | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 tests/Tests/ORM/Functional/QueryParameterTest.php diff --git a/tests/Tests/ORM/Functional/QueryParameterTest.php b/tests/Tests/ORM/Functional/QueryParameterTest.php new file mode 100644 index 00000000000..2cbc85d6cf9 --- /dev/null +++ b/tests/Tests/ORM/Functional/QueryParameterTest.php @@ -0,0 +1,124 @@ +useModelSet('cms'); + + parent::setUp(); + + $user = new CmsUser(); + $user->name = 'John Doe'; + $user->username = 'john'; + $user2 = new CmsUser(); + $user2->name = 'Jane Doe'; + $user2->username = 'jane'; + $user3 = new CmsUser(); + $user3->name = 'Just Bill'; + $user3->username = 'bill'; + + $this->_em->persist($user); + $this->_em->persist($user2); + $this->_em->persist($user3); + $this->_em->flush(); + + $this->userId = $user->id; + + $this->_em->clear(); + } + + public function testParameterTypeInBuilder(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->setParameter('id', $this->userId, ParameterType::INTEGER) + ->getQuery() + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testParameterTypeInQuery(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->getQuery() + ->setParameter('id', $this->userId, ParameterType::INTEGER) + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testDbalTypeStringInBuilder(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->setParameter('id', $this->userId, Types::INTEGER) + ->getQuery() + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testDbalTypeStringInQuery(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->getQuery() + ->setParameter('id', $this->userId, Types::INTEGER) + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testArrayParameterTypeInBuilder(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.username IN (:usernames)') + ->orderBy('u.username') + ->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING) + ->getQuery() + ->getArrayResult(); + + self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result); + } + + public function testArrayParameterTypeInQuery(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.username IN (:usernames)') + ->orderBy('u.username') + ->getQuery() + ->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING) + ->getArrayResult(); + + self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result); + } +}