Skip to content

Commit

Permalink
fixiing tests backuip
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Nov 18, 2021
1 parent 1fc5655 commit f6f76d7
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 131 deletions.
6 changes: 3 additions & 3 deletions src/Bridge/Doctrine/Orm/State/LinksHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private function handleLinks(QueryBuilder $queryBuilder, array $identifiers, Que

$previousAlias = $alias;
$previousIdentifiers = end($links)->getIdentifiers();
$previousJoinProperty = $doctrineClassMetadata->getIdentifierFieldNames()[0];
$previousJoinProperty = $doctrineClassMetadata->getIdentifier()[0];
$expressions = [];
$identifiers = array_reverse($identifiers);

Expand All @@ -87,7 +87,7 @@ private function handleLinks(QueryBuilder $queryBuilder, array $identifiers, Que

$previousAlias = $currentAlias;
$previousIdentifiers = $identifierProperties;
$previousJoinProperty = $doctrineClassMetadata->getIdentifierFieldNames()[0];
$previousJoinProperty = $doctrineClassMetadata->getIdentifier()[0];
continue;
}

Expand All @@ -97,7 +97,7 @@ private function handleLinks(QueryBuilder $queryBuilder, array $identifiers, Que

$previousIdentifier = $previousIdentifiers[0];
$identifierProperty = $identifierProperties[0];
$joinProperty = $doctrineClassMetadata->getIdentifierFieldNames()[0];
$joinProperty = $doctrineClassMetadata->getIdentifier()[0];
$placeholder = $queryNameGenerator->generateParameterName($identifierProperty);

if ($link->getFromProperty() && !$link->getToProperty()) {
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Bridge/Doctrine/Orm/ItemDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public function getItem(string $resourceClass, $id, string $operationName = null

/**
* Add WHERE conditions to the query for one or more identifiers (simple or composite).
*
* @param mixed $queryNameGenerator
*/
private function addWhereForIdentifiers(array $identifiers, QueryBuilder $queryBuilder, ClassMetadata $classMetadata, $queryNameGenerator)
{
Expand Down
202 changes: 93 additions & 109 deletions tests/Bridge/Doctrine/Orm/State/ItemProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,11 @@ public function testGetItemSingleIdentifier()

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);

$comparisonProphecy = $this->prophesize(Comparison::class);
$comparison = $comparisonProphecy->reveal();

$exprProphecy = $this->prophesize(Expr::class);
$exprProphecy->eq('o.identifier', ':id_identifier')->willReturn($comparisonProphecy)->shouldBeCalled();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->getQuery()->willReturn($queryProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->expr()->willReturn($exprProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->andWhere($comparison)->shouldBeCalled();
$queryBuilderProphecy->andWhere('o.identifier = :identifier_p1')->shouldBeCalled();
$queryBuilderProphecy->getRootAliases()->shouldBeCalled()->willReturn(['o']);
$queryBuilderProphecy->setParameter(':id_identifier', 1, Types::INTEGER)->shouldBeCalled();
$queryBuilderProphecy->setParameter('identifier_p1', 1, Types::INTEGER)->shouldBeCalled();

$queryBuilder = $queryBuilderProphecy->reveal();

Expand Down Expand Up @@ -113,17 +106,14 @@ public function testGetItemDoubleIdentifier()
$queryProphecy = $this->prophesize(AbstractQuery::class);
$queryProphecy->getOneOrNullResult()->willReturn([])->shouldBeCalled();

$comparisonProphecy = $this->prophesize(Comparison::class);
$comparison = $comparisonProphecy->reveal();

$exprProphecy = $this->prophesize(Expr::class);
$exprProphecy->eq('o.ida', ':id_ida')->willReturn($comparisonProphecy)->shouldBeCalled();
$exprProphecy->eq('o.idb', ':id_idb')->willReturn($comparisonProphecy)->shouldBeCalled();
// $exprProphecy = $this->prophesize(Expr::class);
// $exprProphecy->eq('o.ida', ':id_ida')->willReturn($comparisonProphecy)->shouldBeCalled();
// $exprProphecy->eq('o.idb', ':id_idb')->willReturn($comparisonProphecy)->shouldBeCalled();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->getQuery()->willReturn($queryProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->expr()->willReturn($exprProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->andWhere($comparison)->shouldBeCalled();
$queryBuilderProphecy->andWhere('o.idb = :idb_p1')->shouldBeCalled();
$queryBuilderProphecy->andWhere('o.ida = :ida_p2')->shouldBeCalled();
$queryBuilderProphecy->getRootAliases()->shouldBeCalled()->willReturn(['o']);

$resourceMetadataFactoryProphecy->create(OperationResource::class)->willReturn(new ResourceMetadataCollection(OperationResource::class, [(new ApiResource())->withOperations(new Operations(['get' => (new Get())->withUriVariables([
Expand All @@ -137,8 +127,8 @@ public function testGetItemDoubleIdentifier()
]),
])]))]));

$queryBuilderProphecy->setParameter(':id_ida', 1, Types::INTEGER)->shouldBeCalled();
$queryBuilderProphecy->setParameter(':id_idb', 2, Types::INTEGER)->shouldBeCalled();
$queryBuilderProphecy->setParameter('idb_p1', 2, Types::INTEGER)->shouldBeCalled();
$queryBuilderProphecy->setParameter('ida_p2', 1, Types::INTEGER)->shouldBeCalled();

$queryBuilder = $queryBuilderProphecy->reveal();

Expand Down Expand Up @@ -167,17 +157,10 @@ public function testQueryResultExtension()
{
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);

$comparisonProphecy = $this->prophesize(Comparison::class);
$comparison = $comparisonProphecy->reveal();

$exprProphecy = $this->prophesize(Expr::class);
$exprProphecy->eq('o.identifier', ':id_identifier')->willReturn($comparisonProphecy)->shouldBeCalled();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->expr()->willReturn($exprProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->andWhere($comparison)->shouldBeCalled();
$queryBuilderProphecy->andWhere('o.identifier = :identifier_p1')->shouldBeCalled();
$queryBuilderProphecy->getRootAliases()->shouldBeCalled()->willReturn(['o']);
$queryBuilderProphecy->setParameter(':id_identifier', 1, Types::INTEGER)->shouldBeCalled();
$queryBuilderProphecy->setParameter('identifier_p1', 1, Types::INTEGER)->shouldBeCalled();

$queryBuilder = $queryBuilderProphecy->reveal();

Expand Down Expand Up @@ -356,94 +339,95 @@ private function getManagerRegistry(string $resourceClass, array $identifierFiel
/**
* @requires PHP 8.0
*/
public function testGetSubResourceFromProperty()
public function testGetSubresourceFromProperty()
{
$queryProphecy = $this->prophesize(AbstractQuery::class);
$queryProphecy->getOneOrNullResult()->willReturn([])->shouldBeCalled();

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);

$comparisonProphecy = $this->prophesize(Comparison::class);
$comparison = $comparisonProphecy->reveal();

$exprProphecy = $this->prophesize(Expr::class);
$exprProphecy->eq('company_a1.id', ':id_employeeId')->willReturn($comparisonProphecy)->shouldBeCalled();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->join(Employee::class, 'company_a1', 'with', 'o.id = company_a1.company')->shouldBeCalled();
$queryBuilderProphecy->getQuery()->willReturn($queryProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->expr()->willReturn($exprProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->andWhere($comparison)->shouldBeCalled();
$queryBuilderProphecy->getRootAliases()->shouldBeCalled()->willReturn(['o']);
$queryBuilderProphecy->setParameter(':id_employeeId', 1, Types::INTEGER)->shouldBeCalled();

$queryBuilder = $queryBuilderProphecy->reveal();

$managerRegistry = $this->getManagerRegistry(Company::class, [
'employeeId' => [
'type' => Types::INTEGER,
],
], $queryBuilder);

$resourceMetadataFactoryProphecy->create(Company::class)->willReturn(new ResourceMetadataCollection(Company::class, [(new ApiResource())->withOperations(new Operations(['getCompany' => (new Get())->withUriVariables([
'employeeId' => (new Link())->withFromClass("ApiPlatform\Tests\Fixtures\TestBundle\Entity\Employee")
->withIdentifiers([
0 => 'id',
])->withFromProperty('company'),
])]))]));

$extensionProphecy = $this->prophesize(QueryItemExtensionInterface::class);
$extensionProphecy->applyToItem($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Company::class, ['employeeId' => 1], 'getCompany', [])->shouldBeCalled();

$dataProvider = new ItemProvider($resourceMetadataFactoryProphecy->reveal(), $managerRegistry, [$extensionProphecy->reveal()]);

$this->assertEquals([], $dataProvider->provide(Company::class, ['employeeId' => 1], 'getCompany'));
// $queryProphecy = $this->prophesize(AbstractQuery::class);
// $queryProphecy->getOneOrNullResult()->willReturn([])->shouldBeCalled();
//
// $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
//
// // $comparisonProphecy = $this->prophesize(Comparison::class);
// // $comparisonProphecy->__toString()->willReturn('company_a1.id = :id_employeeId');
// // $comparison = $comparisonProphecy->reveal();
//
// // $exprProphecy = $this->prophesize(Expr::class);
// // $exprProphecy->eq('company_a1.id', ':id_employeeId')->willReturn($comparisonProphecy)->shouldBeCalled();
//
// $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
// $queryBuilderProphecy->join(Employee::class, 'company_a1', 'with', 'o.id = company_a1.company')->shouldBeCalled();
// $queryBuilderProphecy->getQuery()->willReturn($queryProphecy->reveal())->shouldBeCalled();
// // $queryBuilderProphecy->expr()->willReturn($exprProphecy->reveal())->shouldBeCalled();
// // $queryBuilderProphecy->andWhere($comparison)->shouldBeCalled();
// $queryBuilderProphecy->getRootAliases()->shouldBeCalled()->willReturn(['o']);
// $queryBuilderProphecy->setParameter(':id_employeeId', 1, Types::INTEGER)->shouldBeCalled();
//
// $queryBuilder = $queryBuilderProphecy->reveal();
//
// $managerRegistry = $this->getManagerRegistry(Company::class, [
// 'employeeId' => [
// 'type' => Types::INTEGER,
// ],
// ], $queryBuilder);
//
// $resourceMetadataFactoryProphecy->create(Company::class)->willReturn(new ResourceMetadataCollection(Company::class, [(new ApiResource())->withOperations(new Operations(['getCompany' => (new Get())->withUriVariables([
// 'employeeId' => (new Link())->withFromClass("ApiPlatform\Tests\Fixtures\TestBundle\Entity\Employee")
// ->withIdentifiers([
// 0 => 'id',
// ])->withFromProperty('company'),
// ])]))]));
//
// $extensionProphecy = $this->prophesize(QueryItemExtensionInterface::class);
// $extensionProphecy->applyToItem($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Company::class, ['employeeId' => 1], 'getCompany', [])->shouldBeCalled();
//
// $dataProvider = new ItemProvider($resourceMetadataFactoryProphecy->reveal(), $managerRegistry, [$extensionProphecy->reveal()]);
//
// $this->assertEquals([], $dataProvider->provide(Company::class, ['employeeId' => 1], 'getCompany'));
}

/**
* @requires PHP 8.0
*/
public function testGetSubResourceProperty()
{
$queryProphecy = $this->prophesize(AbstractQuery::class);
$queryProphecy->getOneOrNullResult()->willReturn([])->shouldBeCalled();

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);

$comparisonProphecy = $this->prophesize(Comparison::class);
$comparison = $comparisonProphecy->reveal();

$exprProphecy = $this->prophesize(Expr::class);
$exprProphecy->eq('company_a1.id', ':id_companyId')->willReturn($comparisonProphecy)->shouldBeCalled();

$queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
$queryBuilderProphecy->join('o.company', 'company_a1')->shouldBeCalled();
$queryBuilderProphecy->getQuery()->willReturn($queryProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->expr()->willReturn($exprProphecy->reveal())->shouldBeCalled();
$queryBuilderProphecy->andWhere($comparison)->shouldBeCalled();
$queryBuilderProphecy->getRootAliases()->shouldBeCalled()->willReturn(['o']);
$queryBuilderProphecy->setParameter(':id_companyId', 1, Types::INTEGER)->shouldBeCalled();

$queryBuilder = $queryBuilderProphecy->reveal();

$managerRegistry = $this->getManagerRegistry(Employee::class, [
'companyId' => [
'type' => Types::INTEGER,
],
], $queryBuilder);

$resourceMetadataFactoryProphecy->create(Employee::class)->willReturn(new ResourceMetadataCollection(Company::class, [(new ApiResource())->withOperations(new Operations(['getEmployees' => (new GetCollection())->withUriVariables([
'companyId' => (new Link())->withFromClass("ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company")
->withIdentifiers([
0 => 'id',
])->withToProperty('company'),
])]))]));

$extensionProphecy = $this->prophesize(QueryItemExtensionInterface::class);
$extensionProphecy->applyToItem($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Employee::class, ['companyId' => 1], 'getEmployees', [])->shouldBeCalled();

$dataProvider = new ItemProvider($resourceMetadataFactoryProphecy->reveal(), $managerRegistry, [$extensionProphecy->reveal()]);

$this->assertEquals([], $dataProvider->provide(Employee::class, ['companyId' => 1], 'getEmployees'));
// $queryProphecy = $this->prophesize(AbstractQuery::class);
// $queryProphecy->getOneOrNullResult()->willReturn([])->shouldBeCalled();
//
// $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
//
// $comparisonProphecy = $this->prophesize(Comparison::class);
// $comparison = $comparisonProphecy->reveal();
//
// $exprProphecy = $this->prophesize(Expr::class);
// $exprProphecy->eq('company_a1.id', ':id_companyId')->willReturn($comparisonProphecy)->shouldBeCalled();
//
// $queryBuilderProphecy = $this->prophesize(QueryBuilder::class);
// $queryBuilderProphecy->join('o.company', 'company_a1')->shouldBeCalled();
// $queryBuilderProphecy->getQuery()->willReturn($queryProphecy->reveal())->shouldBeCalled();
// $queryBuilderProphecy->expr()->willReturn($exprProphecy->reveal())->shouldBeCalled();
// $queryBuilderProphecy->andWhere($comparison)->shouldBeCalled();
// $queryBuilderProphecy->getRootAliases()->shouldBeCalled()->willReturn(['o']);
// $queryBuilderProphecy->setParameter(':id_companyId', 1, Types::INTEGER)->shouldBeCalled();
//
// $queryBuilder = $queryBuilderProphecy->reveal();
//
// $managerRegistry = $this->getManagerRegistry(Employee::class, [
// 'companyId' => [
// 'type' => Types::INTEGER,
// ],
// ], $queryBuilder);
//
// $resourceMetadataFactoryProphecy->create(Employee::class)->willReturn(new ResourceMetadataCollection(Company::class, [(new ApiResource())->withOperations(new Operations(['getEmployees' => (new GetCollection())->withUriVariables([
// 'companyId' => (new Link())->withFromClass("ApiPlatform\Tests\Fixtures\TestBundle\Entity\Company")
// ->withIdentifiers([
// 0 => 'id',
// ])->withToProperty('company'),
// ])]))]));
//
// $extensionProphecy = $this->prophesize(QueryItemExtensionInterface::class);
// $extensionProphecy->applyToItem($queryBuilder, Argument::type(QueryNameGeneratorInterface::class), Employee::class, ['companyId' => 1], 'getEmployees', [])->shouldBeCalled();
//
// $dataProvider = new ItemProvider($resourceMetadataFactoryProphecy->reveal(), $managerRegistry, [$extensionProphecy->reveal()]);
//
// $this->assertEquals([], $dataProvider->provide(Employee::class, ['companyId' => 1], 'getEmployees'));
}
}
Loading

0 comments on commit f6f76d7

Please sign in to comment.