Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
feat: handler (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbprod committed Sep 4, 2016
1 parent f1c5e11 commit 95a60e2
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 3 deletions.
41 changes: 41 additions & 0 deletions Makefile
@@ -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
64 changes: 64 additions & 0 deletions src/ElasticaSpecification/Handler.php
@@ -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);
}
}
Expand Up @@ -76,7 +76,7 @@ public function testBuildThrowExceptionIfNotAndXSpecification()
$registry = new Registry();
$builder = new AndXBuilder($registry);

$this->setExpectedException(\InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);

$expr = $builder->build($spec, new QueryBuilder());
}
Expand Down
Expand Up @@ -74,7 +74,7 @@ public function testBuildThrowExceptionIfNotNotSpecification()
$registry = new Registry();
$builder = new NotBuilder($registry);

$this->setExpectedException(\InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);

$expr = $builder->build($spec, new QueryBuilder());
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ public function testBuildThrowExceptionIfNotOrXSpecification()
$registry = new Registry();
$builder = new OrXBuilder($registry);

$this->setExpectedException(\InvalidArgumentException::class);
$this->expectException(\InvalidArgumentException::class);

$expr = $builder->build($spec, new QueryBuilder());
}
Expand Down
93 changes: 93 additions & 0 deletions tests/ElasticaSpecification/HandlerTest.php
@@ -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)
);
}
}

0 comments on commit 95a60e2

Please sign in to comment.