This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
III-2080: Add mutable strategy so an injected strategy can be changed…
… on the fly
- Loading branch information
1 parent
f68ae22
commit 1db9132
Showing
2 changed files
with
136 additions
and
0 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,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
89
tests/IndexationStrategy/MutableIndexationStrategyTest.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,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); | ||
} | ||
} |