Skip to content

Commit

Permalink
AbstractORMCriteriaBuilder should have option to use leftJoin (#103)
Browse files Browse the repository at this point in the history
* AbstractORMCriteriaBuilder should have option to use leftJoin

* Extracter to separate method
  • Loading branch information
krzysztof-gzocha committed Apr 10, 2017
1 parent 911075b commit 0745ca7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Extra feature is join() method which will add another join only
* if there is not such join already.
*
*@author Krzysztof Gzocha <krzysztof@propertyfinder.ae>
* @author Krzysztof Gzocha <krzysztof@propertyfinder.ae>
*/
abstract class AbstractORMCriteriaBuilder implements CriteriaBuilderInterface
{
Expand Down Expand Up @@ -51,7 +51,7 @@ protected function join(

$joinParts = $queryBuilder->getDQLPart('join');
if (!array_key_exists($entity, $joinParts)) {
return $queryBuilder->join($join, $alias);
return $this->makeJoin($queryBuilder, $join, $alias, $joinType);
}

return $this->filterExistingJoins(
Expand Down Expand Up @@ -92,6 +92,22 @@ function (Join $joinObj) use ($alias, $join, $joinType) {
return $queryBuilder;
}

return $this->makeJoin($queryBuilder, $join, $alias, $joinType);
}

/**
* @param QueryBuilder $queryBuilder
* @param string $join
* @param string $alias
* @param string $joinType
* @return QueryBuilder
*/
private function makeJoin(QueryBuilder $queryBuilder, string $join, string $alias, string $joinType): QueryBuilder
{
if (Join::LEFT_JOIN === $joinType) {
return $queryBuilder->leftJoin($join, $alias);
}

return $queryBuilder->join($join, $alias);
}
}
31 changes: 22 additions & 9 deletions tests/CriteriaBuilder/Doctrine/AbstractORMCriteriaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class AbstractORMCriteriaBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @param SearchingContextInterface $searchingContext
* @param $expected
* @param bool $expected
* @dataProvider supportedContextDataProvider
*/
public function testSupportSearchingContext(
SearchingContextInterface $searchingContext,
$expected
bool $expected
) {
/** @var AbstractORMCriteriaBuilder $criteriaBuilder */
$criteriaBuilder = $this
Expand All @@ -44,14 +44,14 @@ public function supportedContextDataProvider()
* @param bool $sameJoinType
* @param bool $sameAlias
* @param bool $sameJoin
* @param bool $shouldJoinBeColled
* @param bool $shouldJoinBeCalled
* @dataProvider filterExistingJoinDataProvider
*/
public function testFilterExistingJoins(
$sameJoinType = true,
$sameAlias = true,
$sameJoin = true,
$shouldJoinBeColled = false
$shouldJoinBeCalled = false
) {
$builder = new ORMCriteriaBuilderStub();
$joinType = Join::INNER_JOIN;
Expand All @@ -62,8 +62,14 @@ public function testFilterExistingJoins(
new Join($joinType, $join, $alias),
];

if ($sameJoinType) {
$mock = $this->getQueryBuilderMock($shouldJoinBeCalled);
} else {
$mock = $this->getQueryBuilderMock(false, $shouldJoinBeCalled);
}

$builder->filterExistingJoins(
$this->getQueryBuilderMock($shouldJoinBeColled),
$mock,
$joinParts,
$sameAlias ? $alias : 'differentAlias',
$sameJoin ? $join : 'otherEntity.otherEntityName',
Expand All @@ -74,7 +80,7 @@ public function testFilterExistingJoins(
/**
* @return array
*/
public function filterExistingJoinDataProvider()
public function filterExistingJoinDataProvider(): array
{
return [
[true, true, true, false],
Expand Down Expand Up @@ -102,14 +108,14 @@ public function testJoinMethod($sameEntityName, $joinWillBeCalled)
];

$builder->join(
$this->getQueryBuilderMock($joinWillBeCalled, $alreadyDefinedJoins),
$this->getQueryBuilderMock(false, $joinWillBeCalled, $alreadyDefinedJoins),
$sameEntityName ? 'entityName.newEntityName' : 'newEntity.newerEntity',
'alias',
Join::LEFT_JOIN
);
}

public function joinMethodDataProvider()
public function joinMethodDataProvider(): array
{
return [
[true, false],
Expand All @@ -119,12 +125,14 @@ public function joinMethodDataProvider()

/**
* @param bool $joinWillBeCalled
* @param bool $leftJoinWillBeCalled
* @param array $joinParts
*
* @return QueryBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private function getQueryBuilderMock(
$joinWillBeCalled = false,
bool $joinWillBeCalled = false,
bool $leftJoinWillBeCalled = false,
array $joinParts = []
) {
$mock = $this
Expand All @@ -137,6 +145,11 @@ private function getQueryBuilderMock(
->method('join')
->willReturnSelf();

$mock
->expects($leftJoinWillBeCalled ? $this->once() : $this->never())
->method('leftJoin')
->willReturnSelf();

$mock
->expects($this->any())
->method('getDQLPart')
Expand Down

0 comments on commit 0745ca7

Please sign in to comment.