Skip to content

Commit

Permalink
added test for addIdentifiersToQuery: testAddIdentifiersToQueryWithCu…
Browse files Browse the repository at this point in the history
…stomIdMapping
  • Loading branch information
oleg-andreyev committed Feb 2, 2020
1 parent 28fd117 commit cc77f69
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Model/ModelManager.php
Expand Up @@ -406,7 +406,7 @@ public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, a
$qb = $queryProxy->getQueryBuilder();
$classMetadata = $this->getMetadata($class);

$prefix = uniqid();
$prefix = $this->generatePrefix();
$sqls = [];
foreach ($idx as $pos => $id) {
$ids = explode(self::ID_SEPARATOR, $id);
Expand Down Expand Up @@ -660,4 +660,9 @@ private function getValueFromType($value, Type $type, string $fieldType, Abstrac

return (string) $type->convertToDatabaseValue($value, $platform);
}

protected function generatePrefix(): string
{
return uniqid();
}
}
51 changes: 51 additions & 0 deletions tests/Model/ModelManagerTest.php
Expand Up @@ -967,6 +967,57 @@ public function testGetUrlsafeIdentifierNull(): void
$this->assertNull($model->getNormalizedIdentifier(null));
}

public function testAddIdentifiersToQueryWithCustomIdMapping(): void
{
$managerRegistry = $this->createMock(ManagerRegistry::class);
$entityManager = $this->createMock(EntityManagerInterface::class);
$classMetadataFactory = $this->createMock(ClassMetadataFactory::class);
$proxyQuery = $this->createMock(ProxyQuery::class);
$queryBuilder = new QueryBuilder($entityManager);
$classMetadata = $this->createMock(ClassMetadata::class);

Type::addType('binary_uuid', UuidBinaryType::class);

$managerRegistry->method('getManagerForClass')
->with(\stdClass::class)
->willReturn($entityManager);

$entityManager->method('getMetadataFactory')
->willReturn($classMetadataFactory);

$classMetadataFactory->method('getMetadataFor')
->with(\stdClass::class)
->willReturn($classMetadata);

$classMetadata->method('getIdentifierFieldNames')
->willReturn(['uuid']);

$proxyQuery->method('getQueryBuilder')
->willReturn($queryBuilder);

$classMetadata->method('getFieldMapping')
->with('uuid')
->willReturn([
'type' => 'binary_uuid'
]);

$queryBuilder->from('foobar', 'a');

$modelManager = new class ($managerRegistry) extends ModelManager {
protected function generatePrefix(): string
{
return '1234';
}
};

$modelManager->addIdentifiersToQuery(\stdClass::class, $proxyQuery, ['uuid']);

$this->assertEquals(
'SELECT FROM foobar a WHERE ( a.uuid = :field_1234_uuid_0 )',
$queryBuilder->getDQL()
);
}

private function getMetadata($class, $isVersioned)
{
$metadata = new ClassMetadata($class);
Expand Down

0 comments on commit cc77f69

Please sign in to comment.