-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from holtkamp/add-isMemberOfCondition
Add isMemberOf condition
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace spec\Rb\Specification\Doctrine\Condition; | ||
|
||
use Doctrine\ORM\Query\Expr; | ||
use Doctrine\ORM\QueryBuilder; | ||
use PhpSpec\ObjectBehavior; | ||
use Rb\Specification\Doctrine\SpecificationInterface; | ||
|
||
class IsMemberOfSpec extends ObjectBehavior | ||
{ | ||
private $className = '\Foo'; | ||
private $field = 'foo'; | ||
|
||
private $dqlAlias = 'a'; | ||
|
||
public function let() | ||
{ | ||
$this->beConstructedWith($this->field, $this->className, $this->dqlAlias); | ||
} | ||
|
||
public function it_is_an_expression() | ||
{ | ||
$this->shouldBeAnInstanceOf(SpecificationInterface::class); | ||
} | ||
|
||
public function it_calls_is_instance_of(QueryBuilder $queryBuilder, Expr $expr) | ||
{ | ||
$expression = 'a.foo member of ' . $this->className; | ||
|
||
$queryBuilder->expr()->willReturn($expr); | ||
$expr->isMemberOf(sprintf('%s.%s', $this->dqlAlias, $this->field), $this->className)->willReturn($expression); | ||
|
||
$this->isSatisfiedBy('foo')->shouldReturn(true); | ||
$this->modify($queryBuilder, 'b')->shouldReturn($expression); | ||
} | ||
|
||
public function it_uses_dql_alias_if_passed(QueryBuilder $queryBuilder, Expr $expr) | ||
{ | ||
$dqlAlias = 'x'; | ||
$this->beConstructedWith($this->field, $this->className, null); | ||
$queryBuilder->expr()->willReturn($expr); | ||
|
||
$expr->isMemberOf(sprintf('%s.%s', $dqlAlias, $this->field), $this->className)->shouldBeCalled(); | ||
|
||
$this->isSatisfiedBy('foo')->shouldReturn(true); | ||
$this->modify($queryBuilder, $dqlAlias); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rb\Specification\Doctrine\Condition; | ||
|
||
use Doctrine\ORM\QueryBuilder; | ||
use Rb\Specification\Doctrine\AbstractSpecification; | ||
|
||
class IsMemberOf extends AbstractSpecification | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $className; | ||
|
||
/** | ||
* @param string $field | ||
* @param string $className | ||
* @param string $dqlAlias | ||
*/ | ||
public function __construct($field, $className, $dqlAlias = null) | ||
{ | ||
$this->className = $className; | ||
|
||
parent::__construct($field, $dqlAlias); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function modify(QueryBuilder $queryBuilder, $dqlAlias) | ||
{ | ||
return (string) $queryBuilder->expr()->isMemberOf( | ||
$this->createPropertyWithAlias($dqlAlias), | ||
$this->className | ||
); | ||
} | ||
} |