Skip to content

Commit

Permalink
[ElasticSearch] Add projections
Browse files Browse the repository at this point in the history
  • Loading branch information
Arminek committed May 29, 2017
1 parent 7713de0 commit 505822b
Show file tree
Hide file tree
Showing 24 changed files with 838 additions and 58 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

"sylius/sylius": "dev-master",
"ongr/elasticsearch-dsl": "^5.0",
"ongr/elasticsearch-bundle": "^5.0"
"ongr/elasticsearch-bundle": "^5.0",
"simple-bus/symfony-bridge": "^4.1"
},
"require-dev": {
"behat/behat": "^3.2",
Expand Down
7 changes: 4 additions & 3 deletions spec/Document/AttributeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Sylius\ElasticSearchPlugin\Document;

use ONGR\ElasticsearchBundle\Collection\Collection;
use Sylius\ElasticSearchPlugin\Document\Attribute;
use PhpSpec\ObjectBehavior;

Expand All @@ -12,11 +13,11 @@ function it_is_initializable()
$this->shouldHaveType(Attribute::class);
}

function it_has_value()
function it_has_code()
{
$this->setValue('red');
$this->setCode('color');

$this->getValue()->shouldReturn('red');
$this->getCode()->shouldReturn('color');
}

function it_has_name()
Expand Down
38 changes: 38 additions & 0 deletions spec/Document/AttributeValueSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace spec\Sylius\ElasticSearchPlugin\Document;

use Sylius\ElasticSearchPlugin\Document\Attribute;
use Sylius\ElasticSearchPlugin\Document\AttributeValue;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

final class AttributeValueSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(AttributeValue::class);
}

function it_has_value()
{
$this->setValue('Red');

$this->getValue()->shouldReturn('Red');
}

function it_has_attribute()
{
$attribute = new Attribute();
$this->setAttribute($attribute);

$this->getAttribute()->shouldReturn($attribute);
}

function it_has_code()
{
$this->setCode('red');

$this->getCode()->shouldReturn('red');
}
}
22 changes: 16 additions & 6 deletions spec/Document/ProductSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Sylius\ElasticSearchPlugin\Document\Price;
use Sylius\ElasticSearchPlugin\Document\Product;
use PhpSpec\ObjectBehavior;
use Sylius\ElasticSearchPlugin\Document\TaxonCode;

final class ProductSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -57,18 +58,27 @@ function it_has_price()
$this->getPrice()->shouldReturn($price);
}

function it_has_taxon_code()
function it_has_main_taxon_code()
{
$this->setTaxonCode('Tree');
$taxonCode = new TaxonCode();
$this->setMainTaxonCode($taxonCode);

$this->getTaxonCode()->shouldReturn('Tree');
$this->getMainTaxonCode()->shouldReturn($taxonCode);
}

function it_has_taxon_codes()
{
$taxonCodes = new Collection();
$this->setTaxonCodes($taxonCodes);

$this->getTaxonCodes()->shouldReturn($taxonCodes);
}

function it_has_attributes()
{
$attributes = new Collection();
$this->setAttributes($attributes);
$attributeValues = new Collection();
$this->setAttributeValues($attributeValues);

$this->getAttributes()->shouldReturn($attributes);
$this->getAttributeValues()->shouldReturn($attributeValues);
}
}
22 changes: 22 additions & 0 deletions spec/Document/TaxonCodeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace spec\Sylius\ElasticSearchPlugin\Document;

use Sylius\ElasticSearchPlugin\Document\TaxonCode;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

final class TaxonCodeSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(TaxonCode::class);
}

function it_has_value()
{
$this->setValue('mug');

$this->getValue()->shouldReturn('mug');
}
}
42 changes: 42 additions & 0 deletions spec/EventListener/ProductPublisherSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace spec\Sylius\ElasticSearchPlugin\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use SimpleBus\Message\Bus\MessageBus;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\ElasticSearchPlugin\Event\ProductCreated;
use Sylius\ElasticSearchPlugin\EventListener\ProductPublisher;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

final class ProductPublisherSpec extends ObjectBehavior
{
function let(MessageBus $eventBus)
{
$this->beConstructedWith($eventBus);
}

function it_is_initializable()
{
$this->shouldHaveType(ProductPublisher::class);
}

function it_publishes_product_event(MessageBus $eventBus, LifecycleEventArgs $event, ProductInterface $product)
{
$event->getEntity()->willReturn($product);

$eventBus->handle(ProductCreated::occur($product->getWrappedObject()))->shouldBeCalled();

$this->postPersist($event);
}

function it_does_not_publish_product_event_if_entity_is_not_a_product(MessageBus $eventBus, LifecycleEventArgs $event)
{
$event->getEntity()->willReturn(new \stdClass());

$eventBus->handle(Argument::any())->shouldNotBeCalled();

$this->postPersist($event);
}
}
78 changes: 78 additions & 0 deletions spec/Projection/ProductProjectorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace spec\Sylius\ElasticSearchPlugin\Projection;

use Doctrine\Common\Collections\ArrayCollection;
use ONGR\ElasticsearchBundle\Service\Manager;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\ElasticSearchPlugin\Document\Product;
use Sylius\ElasticSearchPlugin\Event\ProductCreated;
use Sylius\ElasticSearchPlugin\Factory\ProductFactoryInterface;
use Sylius\ElasticSearchPlugin\Projection\ProductProjector;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

final class ProductProjectorSpec extends ObjectBehavior
{
function let(Manager $manager, ProductFactoryInterface $factory)
{
$this->beConstructedWith($manager, $factory);
}

function it_is_initializable()
{
$this->shouldHaveType(ProductProjector::class);
}

function it_saves_product_document_if_product_has_channel_defined(
Manager $manager,
ProductFactoryInterface $factory,
ProductInterface $product,
LocaleInterface $locale,
ChannelInterface $channel
) {
$channel->getLocales()->willReturn(new ArrayCollection([$locale->getWrappedObject()]));
$product->getChannels()->willReturn(new ArrayCollection([$channel->getWrappedObject()]));

$productDocument = new Product();
$factory->createFromSyliusSimpleProductModel($product, $locale, $channel)->willReturn($productDocument);

$manager->persist($productDocument)->shouldBeCalled();
$manager->commit()->shouldBeCalled();

$this->handleProductCreated(ProductCreated::occur($product->getWrappedObject()));
}

function it_does_not_save_product_document_if_product_has_not_channel_defined(
Manager $manager,
ProductFactoryInterface $factory,
ProductInterface $product
) {
$product->getChannels()->willReturn(new ArrayCollection([]));
$factory->createFromSyliusSimpleProductModel(Argument::any(), Argument::any(), Argument::any())->shouldNotBeCalled();

$manager->persist(Argument::any())->shouldNotBeCalled();
$manager->commit()->shouldBeCalled();

$this->handleProductCreated(ProductCreated::occur($product->getWrappedObject()));
}

function it_does_not_save_product_document_if_channel_has_not_locales_defined(
Manager $manager,
ProductFactoryInterface $factory,
ProductInterface $product,
ChannelInterface $channel
) {
$channel->getLocales()->willReturn(new ArrayCollection([]));
$product->getChannels()->willReturn(new ArrayCollection([$channel->getWrappedObject()]));

$factory->createFromSyliusSimpleProductModel(Argument::any(), Argument::any(), Argument::any())->shouldNotBeCalled();

$manager->persist(Argument::any())->shouldNotBeCalled();
$manager->commit()->shouldBeCalled();

$this->handleProductCreated(ProductCreated::occur($product->getWrappedObject()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function it_is_query_factory()

function it_creates_query()
{
$this->create(['taxon_code' => 'mugs'])->shouldBeLike(new TermQuery('taxon_code', 'mugs'));
$this->create(['taxon_code' => 'mugs'])->shouldBeLike(new TermQuery('main_taxon_code.value', 'mugs'));
}

function it_cannot_create_query_if_there_is_no_required_parameters()
Expand Down
13 changes: 13 additions & 0 deletions src/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
namespace Sylius\ElasticSearchPlugin\Controller;

use FOS\RestBundle\View\ConfigurableViewHandlerInterface;
use FOS\RestBundle\View\View;
use Sylius\ElasticSearchPlugin\Document\Product;
use Sylius\ElasticSearchPlugin\Search\Criteria\Criteria;
use Sylius\ElasticSearchPlugin\Search\SearchEngineInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @author Arkadiusz Krakowiak <[email protected]>
Expand Down Expand Up @@ -42,9 +46,18 @@ public function __construct(ConfigurableViewHandlerInterface $restViewHandler, S

/**
* @param Request $request
*
* @return Response
*/
public function searchAction(Request $request)
{
$content = $request->getContent();
$criteria = Criteria::fromQueryParameters(Product::class, json_decode($content, true));

$result = $this->searchEngine->match($criteria);

$view = View::create($result);

return $this->restViewHandler->handle($view);
}
}
23 changes: 0 additions & 23 deletions src/Document/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ final class Attribute
*/
private $name;

/**
* @var string
*
* @ElasticSearch\Property(type="text")
*/
private $value;

/**
* @return string
*/
Expand Down Expand Up @@ -61,20 +54,4 @@ public function setName($name)
{
$this->name = $name;
}

/**
* @return string
*/
public function getValue()
{
return $this->value;
}

/**
* @param string $value
*/
public function setValue($value)
{
$this->value = $value;
}
}
Loading

0 comments on commit 505822b

Please sign in to comment.