This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/ElasticaSpecification/ExpressionBuilder/NotBuilder.php
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,45 @@ | ||
<?php | ||
|
||
namespace GBProd\ElasticaSpecification\ExpressionBuilder; | ||
|
||
use Elastica\QueryBuilder; | ||
use GBProd\ElasticaSpecification\Registry; | ||
use GBProd\Specification\Not; | ||
use GBProd\Specification\Specification; | ||
|
||
/** | ||
* Expression Builder for Not specification | ||
* | ||
* @author gbprod <contact@gb-prod.fr> | ||
*/ | ||
class NotBuilder implements Builder | ||
{ | ||
/** | ||
* @var Registry | ||
*/ | ||
private $registry; | ||
|
||
/** | ||
* @param Registry $registry | ||
*/ | ||
public function __construct(Registry $registry) | ||
{ | ||
$this->registry = $registry; | ||
} | ||
|
||
/** | ||
* {inheritdoc} | ||
*/ | ||
public function build(Specification $spec, QueryBuilder $qb) | ||
{ | ||
if (!$spec instanceof Not) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
$firstPartBuilder = $this->registry->getBuilder($spec->getWrappedSpecification()); | ||
|
||
return $qb->query()->bool() | ||
->addMustNot($firstPartBuilder->build($spec->getWrappedSpecification(), $qb)) | ||
; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
tests/ElasticaSpecification/ExpressionBuilder/NotBuilderTest.php
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,81 @@ | ||
<?php | ||
|
||
namespace Tests\GBProd\ElasticaSpecification; | ||
|
||
use Elastica\QueryBuilder; | ||
use Elastica\Query\AbstractQuery; | ||
use Elastica\Query\BoolQuery; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\NotBuilder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder; | ||
use GBProd\ElasticaSpecification\Registry; | ||
use GBProd\Specification\Not; | ||
use GBProd\Specification\Specification; | ||
|
||
class NotBuilderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstruct() | ||
{ | ||
$builder = new NotBuilder(new Registry()); | ||
|
||
$this->assertInstanceOf(NotBuilder::class, $builder); | ||
} | ||
|
||
public function testBuildReturnsNotExpression() | ||
{ | ||
$not = $this->createNot(); | ||
$registry = $this->createRegistry($not); | ||
|
||
$builder = new NotBuilder($registry); | ||
|
||
$query = $builder->build($not, new QueryBuilder()); | ||
|
||
$this->assertInstanceOf(BoolQuery::class, $query); | ||
|
||
$this->assertArrayHasKey('bool', $query->toArray()); | ||
$this->assertArrayHasKey('must_not', $query->toArray()['bool']); | ||
$this->assertCount(1, $query->toArray()['bool']['must_not']); | ||
} | ||
|
||
/** | ||
* @return Not | ||
*/ | ||
private function createNot() | ||
{ | ||
return new Not( | ||
$this->createMock(Specification::class) | ||
); | ||
} | ||
|
||
/** | ||
* @param Not $not | ||
* | ||
* @return Registry | ||
*/ | ||
private function createRegistry($not) | ||
{ | ||
$builder = $this->createMock(Builder::class); | ||
$builder | ||
->expects($this->any()) | ||
->method('build') | ||
->willReturn($this->createMock(AbstractQuery::class)) | ||
; | ||
|
||
$registry = new Registry(); | ||
|
||
$registry->register(get_class($not->getWrappedSpecification()), $builder); | ||
|
||
return $registry; | ||
} | ||
|
||
|
||
public function testBuildThrowExceptionIfNotNotSpecification() | ||
{ | ||
$spec = $this->createMock(Specification::class); | ||
$registry = new Registry(); | ||
$builder = new NotBuilder($registry); | ||
|
||
$this->setExpectedException(\InvalidArgumentException::class); | ||
|
||
$expr = $builder->build($spec, new QueryBuilder()); | ||
} | ||
} |