Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Move some common testing functionality to a separate base class for s…
Browse files Browse the repository at this point in the history
…tage tests
  • Loading branch information
Sam Stenvall committed Nov 7, 2017
1 parent de94572 commit e0fe352
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 123 deletions.
45 changes: 45 additions & 0 deletions tests/Pipelines/Stages/AbstractStageTestCase.php
@@ -0,0 +1,45 @@
<?php

namespace Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages;

use Elasticsearch\Namespaces\IndicesNamespace;
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
use Nord\Lumen\Elasticsearch\Tests\TestCase;

/**
* Class AbstractStageTestCase
* @package Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages;
*/
abstract class AbstractStageTestCase extends TestCase
{

/**
* @param array $methods
*
* @return IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockedIndices($methods = [])
{
return $this->getMockBuilder(IndicesNamespace::class)
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
}

/**
* @param IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $mockedIndices
*
* @return ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockedSearchService($mockedIndices)
{
$searchService = $this->getMockBuilder(ElasticsearchServiceContract::class)
->setMethods(['indices'])
->getMockForAbstractClass();

$searchService->method('indices')
->willReturn($mockedIndices);

return $searchService;
}
}
20 changes: 3 additions & 17 deletions tests/Pipelines/Stages/CheckIndexExistsStageTest.php
Expand Up @@ -2,43 +2,29 @@

namespace Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages;

use Elasticsearch\Namespaces\IndicesNamespace;
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload;
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CheckIndexExistsStage;
use Nord\Lumen\Elasticsearch\Tests\TestCase;

/**
* Class CheckIndexExistsStageTest
* @package Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages
*/
class CheckIndexExistsStageTest extends TestCase
class CheckIndexExistsStageTest extends AbstractStageTestCase
{

/**
* @expectedException \Nord\Lumen\Elasticsearch\Exceptions\IndexExistsException
*/
public function testStage()
{
/** @var IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $indices */
$indices = $this->getMockBuilder(IndicesNamespace::class)
->disableOriginalConstructor()
->setMethods(['exists'])
->getMock();
$indices = $this->getMockedIndices(['exists']);

$indices->expects($this->once())
->method('exists')
->with(['index' => 'content_1'])
->willReturn(true);

/** @var ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject $service */
$service = $this->getMockBuilder(ElasticsearchServiceContract::class)
->setMethods(['indices'])
->getMockForAbstractClass();

$service->expects($this->once())
->method('indices')
->willReturn($indices);
$service = $this->getMockedSearchService($indices);

$payload = new ApplyMigrationPayload($this->getResourcesBasePath() . '/content.php', 100);
$payload->setTargetVersionFile('1.php');
Expand Down
36 changes: 2 additions & 34 deletions tests/Pipelines/Stages/CreateIndexStageTest.php
Expand Up @@ -2,24 +2,21 @@

namespace Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages;

use Elasticsearch\Namespaces\IndicesNamespace;
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
use Nord\Lumen\Elasticsearch\Pipelines\Stages\CreateIndexStage;
use Nord\Lumen\Elasticsearch\Tests\TestCase;

/**
* Class CreateIndexStageTest
* @package Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages
*/
class CreateIndexStageTest extends TestCase
class CreateIndexStageTest extends AbstractStageTestCase
{

/**
*
*/
public function testStage()
{
$indices = $this->getMockedIndices();
$indices = $this->getMockedIndices(['create']);

$indices->expects($this->once())
->method('create')
Expand All @@ -30,33 +27,4 @@ public function testStage()
$stage = new CreateIndexStage($searchService);
$stage(new DummyPayload());
}

/**
* @return IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject
*/
private function getMockedIndices()
{
return $this->getMockBuilder(IndicesNamespace::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
}

/**
* @param IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $mockedIndices
*
* @return ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject
*/
private function getMockedSearchService($mockedIndices)
{
$searchService = $this->getMockBuilder(ElasticsearchServiceContract::class)
->setMethods(['indices'])
->getMockForAbstractClass();

$searchService->method('indices')
->willReturn($mockedIndices);

return $searchService;
}

}
39 changes: 3 additions & 36 deletions tests/Pipelines/Stages/ReIndexStageTest.php
Expand Up @@ -2,24 +2,21 @@

namespace Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages;

use Elasticsearch\Namespaces\IndicesNamespace;
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
use Nord\Lumen\Elasticsearch\Pipelines\Stages\ReIndexStage;
use Nord\Lumen\Elasticsearch\Tests\TestCase;

/**
* Class CreateIndexStageTest
* @package Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages
*/
class ReIndexStageTest extends TestCase
class ReIndexStageTest extends AbstractStageTestCase
{

/**
* Tests that we reindex from the old to the new index when the index exists
*/
public function testWhenIndexExists()
{
$indices = $this->getMockedIndices();
$indices = $this->getMockedIndices(['exists', 'putSettings']);
$searchService = $this->getMockedSearchService($indices);

$indices->expects($this->once())
Expand Down Expand Up @@ -54,7 +51,7 @@ public function testWhenIndexExists()
*/
public function testWhenIndexDoesNotExist()
{
$indices = $this->getMockedIndices();
$indices = $this->getMockedIndices(['exists', 'putSettings']);
$searchService = $this->getMockedSearchService($indices);

$indices->expects($this->once())
Expand All @@ -72,34 +69,4 @@ public function testWhenIndexDoesNotExist()
$stage = new ReIndexStage($searchService);
$stage(new DummyPayload());
}

/**
* @return IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject
*/
private function getMockedIndices()
{
$indices = $this->getMockBuilder(IndicesNamespace::class)
->disableOriginalConstructor()
->setMethods(['exists', 'putSettings'])
->getMock();

return $indices;
}

/**
* @param IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $mockedIndices
*
* @return ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject
*/
private function getMockedSearchService($mockedIndices)
{
$searchService = $this->getMockBuilder(ElasticsearchServiceContract::class)
->setMethods(['indices', 'reindex'])
->getMockForAbstractClass();

$searchService->method('indices')
->willReturn($mockedIndices);

return $searchService;
}
}
38 changes: 2 additions & 36 deletions tests/Pipelines/Stages/StoreIndexSettingsStageTest.php
Expand Up @@ -2,24 +2,21 @@

namespace Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages;

use Elasticsearch\Namespaces\IndicesNamespace;
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
use Nord\Lumen\Elasticsearch\Pipelines\Stages\StoreIndexSettingsStage;
use Nord\Lumen\Elasticsearch\Tests\TestCase;

/**
* Class StoreIndexSettingsStageTest
* @package Pipelines\Stages
*/
class StoreIndexSettingsStageTest extends TestCase
class StoreIndexSettingsStageTest extends AbstractStageTestCase
{

/**
*
*/
public function testStage()
{
$indices = $this->getMockedIndices();
$indices = $this->getMockedIndices(['getSettings', 'putSettings']);
$searchService = $this->getMockedSearchService($indices);

// Return some fake settings
Expand All @@ -41,35 +38,4 @@ public function testStage()
// Check that the number of replicas was stored in the payload
$this->assertEquals(5, $payload->getNumberOfReplicas());
}

/**
* @return IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject
*/
private function getMockedIndices()
{
$indices = $this->getMockBuilder(IndicesNamespace::class)
->disableOriginalConstructor()
->setMethods(['getSettings', 'putSettings'])
->getMock();

return $indices;
}

/**
* @param IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $mockedIndices
*
* @return ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject
*/
private function getMockedSearchService($mockedIndices)
{
$searchService = $this->getMockBuilder(ElasticsearchServiceContract::class)
->setMethods(['indices', 'reindex'])
->getMockForAbstractClass();

$searchService->method('indices')
->willReturn($mockedIndices);

return $searchService;
}

}

0 comments on commit e0fe352

Please sign in to comment.