Skip to content

Commit

Permalink
Pipeline aggs: unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Feb 8, 2019
1 parent 4b12ddc commit ad6ecb4
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Aggregation\Builder as AggregationBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder as QueryBuilder;
use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Aggregation\BuilderInterface;
use Smile\ElasticsuiteCore\Search\Request\PipelineInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;

Expand Down Expand Up @@ -128,6 +129,21 @@ public function testBuildInvalidAggregation()
$this->getAggregationBuilder()->buildAggregations($buckets);
}

/**
* Test building an aggregation with a simple pipeline aggregation
*
* @return void
*/
public function testBuildPipelinedAggregation()
{
$buckets = [$this->createPipelinedBucket('aggregation', 'bucketType', 'pipeline', 'pipelineType')];
$aggregations = $this->getAggregationBuilder()->buildAggregations($buckets);

$aggregation = $this->getAggregationByName($aggregations, 'aggregation');
$subAggregation = current($this->getSubAggregations($aggregation));
$this->assertInstanceOf(PipelineInterface::class, $subAggregation);
}

/**
* Prepare the aggregation builder used by the test case.
*
Expand Down Expand Up @@ -219,6 +235,43 @@ private function createFilteredBucket($name, $type)
return $bucket;
}

/**
* Create a bucket aggregation with a pipeline.
*
* @param string $name Bucket name.
* @param string $type Bucket type.
* @param string $pipelineName Pipeline name.
* @param string $pipelineType Pipeline type.
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function createPipelinedBucket($name, $type, $pipelineName, $pipelineType)
{
$pipeline = $this->createPipeline($pipelineName, $pipelineType);
$bucket = $this->createBucket($name, $type);
$bucket->method('getPipelines')->will($this->returnValue([$pipeline]));

return $bucket;
}

/**
* Create a pipeline aggregation.
*
* @param string $name Pipeline name.
* @param string $type Pipeline type.
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
private function createPipeline($name, $type)
{
$pipeline = $this->getMockBuilder(PipelineInterface::class)->getMock();

$pipeline->method('getName')->will($this->returnValue($name));
$pipeline->method('getType')->will($this->returnValue($type));

return $pipeline;
}

/**
* Mock a query builder.
*
Expand Down Expand Up @@ -265,9 +318,9 @@ private function getAggregationByName($aggregations, $aggregationName)
* Return all subaggregations of parent aggregation.
*
* @param string $aggregation Parent aggregation.
* @param number $expectedCount Expected number of subaggregation.
* @param int $expectedCount Expected number of subaggregation.
*
* @return string
* @return array
*/
private function getSubAggregations($aggregation, $expectedCount = 1)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <richard.bayet@smile.fr>
* @copyright 2019 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCore\Test\Unit\Search\Request\Aggregation;

use Smile\ElasticsuiteCore\Search\Request\Aggregation\PipelineFactory;
use Smile\ElasticsuiteCore\Search\Request\PipelineInterface;

/**
* Search request pipeline aggregation builder test case.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Richard BAYET <richard.bayet@smile.fr>
*/
class PipelineFactoryTest extends \PHPUnit\Framework\TestCase
{
/**
* Test the pipeline aggregation creation using the factory.
*
* @return void
*/
public function testPipelineCreate()
{
$pipeline = $this->getPipelineFactory()->create('pipelineType', []);
$this->assertInstanceOf(PipelineInterface::class, $pipeline);
}

/**
* Test trying to create an invalid pipeline aggregation type throws an exception.
*
* @expectedException \LogicException
* @expectedExceptionMessage No factory found for pipeline aggregation of type invalidPipelineType
*
* @return void
*/
public function testInvalidPipelineCreate()
{
$this->getPipelineFactory()->create('invalidPipelineType', []);
}

/**
* Prepared a mocked pipeline aggregation factory.
*
* @return \Smile\ElasticsuiteCore\Search\Request\Aggregation\PipelineFactory
*/
private function getPipelineFactory()
{
$pipelineMock = $this->getMockBuilder(PipelineInterface::class)->getMock();

$factoryName = sprintf("%s%s", PipelineInterface::class, 'Factory');
$pipelineFactoryMock = $this->getMockBuilder($factoryName)
->setMethods(['create'])
->getMock();

$pipelineFactoryMock->method('create')
->will($this->returnValue($pipelineMock));

$factories = ['pipelineType' => $pipelineFactoryMock];

return new PipelineFactory($factories);
}
}

0 comments on commit ad6ecb4

Please sign in to comment.