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
6 changed files
with
201 additions
and
3 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,41 @@ | ||
.SILENT: | ||
.PHONY: help | ||
|
||
## Colors | ||
COLOR_RESET = \033[0m | ||
COLOR_INFO = \033[32m | ||
COLOR_COMMENT = \033[33m | ||
|
||
## Help | ||
help: | ||
printf "${COLOR_COMMENT}Usage:${COLOR_RESET}\n" | ||
printf " make <target>\n\n" | ||
printf "${COLOR_COMMENT}Available targets:${COLOR_RESET}\n" | ||
awk '/^[a-zA-Z\-\_0-9\.@]+:/ { \ | ||
helpMessage = match(lastLine, /^## (.*)/); \ | ||
if (helpMessage) { \ | ||
helpCommand = substr($$1, 0, index($$1, ":")); \ | ||
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ | ||
printf " ${COLOR_INFO}%-16s${COLOR_RESET} %s\n", helpCommand, helpMessage; \ | ||
} \ | ||
} \ | ||
{ lastLine = $$0 }' $(MAKEFILE_LIST) | ||
|
||
## Install | ||
install: | ||
composer install | ||
|
||
## Run tests | ||
test: | ||
vendor/bin/phpunit | ||
|
||
## Loop unit tests | ||
test-loop: | ||
while true; \ | ||
do vendor/bin/phpunit; \ | ||
read continue; \ | ||
done; | ||
|
||
## Code coverage | ||
coverage: | ||
vendor/bin/phpunit --coverage-text |
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,64 @@ | ||
<?php | ||
|
||
namespace GBProd\ElasticaSpecification; | ||
|
||
use Elastica\QueryBuilder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\AndXBuilder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\NotBuilder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\OrXBuilder; | ||
use GBProd\Specification\AndX; | ||
use GBProd\Specification\Not; | ||
use GBProd\Specification\OrX; | ||
use GBProd\Specification\Specification; | ||
|
||
/** | ||
* Handler for elastica specifications | ||
* | ||
* @author gbprod <contact@gb-prod.fr> | ||
*/ | ||
class Handler | ||
{ | ||
/** | ||
* @param Registry | ||
*/ | ||
private $registry; | ||
|
||
/** | ||
* @param Registry $registry | ||
*/ | ||
public function __construct(Registry $registry) | ||
{ | ||
$this->registry = $registry; | ||
|
||
$this->registry->register(AndX::class, new AndXBuilder($registry)); | ||
$this->registry->register(OrX::class, new OrXBuilder($registry)); | ||
$this->registry->register(Not::class, new NotBuilder($registry)); | ||
} | ||
|
||
/** | ||
* handle specification for querybuilder | ||
* | ||
* @param Specification $spec | ||
* @param QueryBuilder $qb | ||
* | ||
* @return array | ||
*/ | ||
public function handle(Specification $spec, QueryBuilder $qb) | ||
{ | ||
$builder = $this->registry->getBuilder($spec); | ||
|
||
return $builder->build($spec, $qb); | ||
} | ||
|
||
/** | ||
* Register a builder for specification | ||
* | ||
* @param string $classname specification fully qualified classname | ||
* @param Builder $builder | ||
*/ | ||
public function registerBuilder($classname, Builder $builder) | ||
{ | ||
$this->registry->register($classname, $builder); | ||
} | ||
} |
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
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
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
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,93 @@ | ||
<?php | ||
|
||
namespace Tests\GBProd\ElasticaSpecification; | ||
|
||
use Elastica\QueryBuilder; | ||
use Elastica\Query\AbstractQuery; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\AndXBuilder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\NotBuilder; | ||
use GBProd\ElasticaSpecification\ExpressionBuilder\OrXBuilder; | ||
use GBProd\ElasticaSpecification\Handler; | ||
use GBProd\ElasticaSpecification\Registry; | ||
use GBProd\Specification\AndX; | ||
use GBProd\Specification\Not; | ||
use GBProd\Specification\OrX; | ||
use GBProd\Specification\Specification; | ||
|
||
class HandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Registry | ||
*/ | ||
private $registry; | ||
|
||
/** | ||
* @var Handler | ||
*/ | ||
private $handler; | ||
|
||
protected function setUp() | ||
{ | ||
$this->registry = new Registry(); | ||
$this->handler = new Handler($this->registry); | ||
} | ||
|
||
public function testConstructWillRegisterBaseBuilders() | ||
{ | ||
$spec1 = $this->createMock(Specification::class); | ||
$spec2 = $this->createMock(Specification::class); | ||
|
||
$this->assertInstanceOf( | ||
AndXBuilder::class, | ||
$this->registry->getBuilder(new AndX($spec1, $spec2)) | ||
); | ||
|
||
$this->assertInstanceOf( | ||
OrXBuilder::class, | ||
$this->registry->getBuilder(new OrX($spec1, $spec2)) | ||
); | ||
|
||
$this->assertInstanceOf( | ||
NotBuilder::class, | ||
$this->registry->getBuilder(new Not($spec1)) | ||
); | ||
} | ||
|
||
public function testRegisterBuilderAddBuilderInRegistry() | ||
{ | ||
$builder = $this->createMock(Builder::class); | ||
$spec = $this->createMock(Specification::class); | ||
|
||
$this->handler->registerBuilder(get_class($spec), $builder); | ||
|
||
$this->assertEquals( | ||
$builder, | ||
$this->registry->getBuilder($spec) | ||
); | ||
} | ||
|
||
public function testHandle() | ||
{ | ||
$this->handler = new Handler(new Registry()); | ||
|
||
$builder = $this->prophesize(Builder::class); | ||
|
||
$spec = $this->createMock(Specification::class); | ||
$this->handler->registerBuilder(get_class($spec), $builder->reveal()); | ||
|
||
$builtQuery = $this->getMockForAbstractClass(AbstractQuery::class); | ||
$qb = new QueryBuilder(); | ||
|
||
$builder | ||
->build($spec, $qb) | ||
->willReturn($builtQuery) | ||
->shouldBeCalled() | ||
; | ||
|
||
$this->assertEquals( | ||
$builtQuery, | ||
$this->handler->handle($spec, $qb) | ||
); | ||
} | ||
} |