Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
III-2080: Add mutable strategy so an injected strategy can be changed…
Browse files Browse the repository at this point in the history
… on the fly
  • Loading branch information
bertramakers committed May 16, 2017
1 parent f68ae22 commit 1db9132
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/IndexationStrategy/MutableIndexationStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace CultuurNet\UDB3\Search\ElasticSearch\IndexationStrategy;

use CultuurNet\UDB3\ReadModel\JsonDocument;
use ValueObjects\StringLiteral\StringLiteral;

class MutableIndexationStrategy implements IndexationStrategyInterface
{
/**
* @var IndexationStrategyInterface
*/
private $indexationStrategy;

/**
* @param IndexationStrategyInterface $indexationStrategy
*/
public function __construct(IndexationStrategyInterface $indexationStrategy)
{
$this->indexationStrategy = $indexationStrategy;
}

/**
* @param IndexationStrategyInterface $newIndexationStrategy
*/
public function setIndexationStrategy(IndexationStrategyInterface $newIndexationStrategy)
{
if ($this->indexationStrategy instanceof BulkIndexationStrategy) {
$this->indexationStrategy->flush();
}

$this->indexationStrategy = $newIndexationStrategy;
}

/**
* @param StringLiteral $indexName
* @param StringLiteral $documentType
* @param JsonDocument $jsonDocument
*/
public function indexDocument(
StringLiteral $indexName,
StringLiteral $documentType,
JsonDocument $jsonDocument
) {
$this->indexationStrategy->indexDocument($indexName, $documentType, $jsonDocument);
}
}
89 changes: 89 additions & 0 deletions tests/IndexationStrategy/MutableIndexationStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace CultuurNet\UDB3\Search\ElasticSearch\IndexationStrategy;

use CultuurNet\UDB3\ReadModel\JsonDocument;
use ValueObjects\StringLiteral\StringLiteral;

class MutableIndexationStrategyTest extends \PHPUnit_Framework_TestCase
{
/**
* @var IndexationStrategyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $mockStrategy1;

/**
* @var IndexationStrategyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $mockStrategy2;

/**
* @var BulkIndexationStrategy|\PHPUnit_Framework_MockObject_MockObject
*/
private $mockBulkStrategy;

/**
* @var MutableIndexationStrategy
*/
private $mutableStrategy;

public function setUp()
{
$this->mockStrategy1 = $this->createMock(IndexationStrategyInterface::class);
$this->mockStrategy2 = $this->createMock(IndexationStrategyInterface::class);

$this->mockBulkStrategy = $this->createMock(BulkIndexationStrategy::class);

$this->mutableStrategy = new MutableIndexationStrategy($this->mockStrategy1);
}

/**
* @test
*/
public function it_delegates_the_indexing_of_documents_to_the_currently_injected_strategy()
{
$index = new StringLiteral('udb3_core');
$type = new StringLiteral('event');
$document = new JsonDocument('ba2c3314-f50f-4f9f-b57a-1353eaaaf84c', '{"foo":"bar"}');

$this->mockStrategy1->expects($this->once())
->method('indexDocument')
->with($index, $type, $document);

$this->mutableStrategy->indexDocument($index, $type, $document);
}

/**
* @test
*/
public function it_can_swap_out_the_injected_strategy_for_another()
{
$this->mutableStrategy->setIndexationStrategy($this->mockStrategy2);

$index = new StringLiteral('udb3_core');
$type = new StringLiteral('event');
$document = new JsonDocument('ba2c3314-f50f-4f9f-b57a-1353eaaaf84c', '{"foo":"bar"}');

$this->mockStrategy1->expects($this->never())
->method('indexDocument');

$this->mockStrategy2->expects($this->once())
->method('indexDocument')
->with($index, $type, $document);

$this->mutableStrategy->indexDocument($index, $type, $document);
}

/**
* @test
*/
public function it_flushes_an_injected_bulk_strategy_before_swapping_it_out()
{
$this->mutableStrategy->setIndexationStrategy($this->mockBulkStrategy);

$this->mockBulkStrategy->expects($this->once())
->method('flush');

$this->mutableStrategy->setIndexationStrategy($this->mockStrategy2);
}
}

0 comments on commit 1db9132

Please sign in to comment.