at the end of the day, it was inevitable
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
namespace IndexBundle\Filter\Filters;
|
||||
|
||||
use IndexBundle\Filter\AbstractGroupFilter;
|
||||
use Tests\AppTestCase;
|
||||
|
||||
/**
|
||||
* Class GroupFilterTest
|
||||
*
|
||||
* @package IndexBundle\Filter\Filters
|
||||
*/
|
||||
class GroupFilterTest extends AppTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$this->getMockForAbstractClass(AbstractGroupFilter::class, [ [ new GteFilter('foo', 10), new AndFilter() ]]);
|
||||
$this->getMockForAbstractClass(AbstractGroupFilter::class, [ new GteFilter('foo', 10) ]);
|
||||
$this->getMockForAbstractClass(AbstractGroupFilter::class, [ new AndFilter() ]);
|
||||
$this->getMockForAbstractClass(AbstractGroupFilter::class);
|
||||
$this->getMockForAbstractClass(AbstractGroupFilter::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage '$filters' should be array of FilterInterface instances or single instance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateWithArrayOfInvalid()
|
||||
{
|
||||
$this->getMockForAbstractClass(AbstractGroupFilter::class, [ [ new GteFilter('foo', 10), 10 ] ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage '$filters' should be array of FilterInterface instances or single instance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateWithSingleInvalid()
|
||||
{
|
||||
$this->getMockForAbstractClass(AbstractGroupFilter::class, [ 1 ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSerialize()
|
||||
{
|
||||
$date = new \DateTime();
|
||||
|
||||
$filter1 = new EqFilter('some', 10);
|
||||
$filter21 = new EqFilter('another', 'foo');
|
||||
$filter22 = new NotFilter(new GteFilter('bar', $date));
|
||||
$filter2 = $this->getMockForAbstractClass(AbstractGroupFilter::class, [ [ $filter21, $filter22 ] ]);
|
||||
$filter = $this->getMockForAbstractClass(AbstractGroupFilter::class, [ [ $filter1, $filter2 ] ]);
|
||||
|
||||
/** @var AbstractGroupFilter $unserializedFilter */
|
||||
$unserializedFilter = unserialize(serialize($filter));
|
||||
|
||||
$this->assertInstanceOf(AbstractGroupFilter::class, $unserializedFilter);
|
||||
$this->assertCount(2, $unserializedFilter->getFilters());
|
||||
|
||||
/** @var EqFilter $unserializedInnerFilter1 */
|
||||
$unserializedInnerFilter1 = $unserializedFilter->getFilters()[0];
|
||||
/** @var AbstractGroupFilter $unserializedInnerFilter2 */
|
||||
$unserializedInnerFilter2 = $unserializedFilter->getFilters()[1];
|
||||
|
||||
$this->assertInstanceOf(EqFilter::class, $unserializedInnerFilter1);
|
||||
$this->assertSame('some', $unserializedInnerFilter1->getFieldName());
|
||||
$this->assertSame(10, $unserializedInnerFilter1->getValue());
|
||||
|
||||
$this->assertInstanceOf(AbstractGroupFilter::class, $unserializedInnerFilter2);
|
||||
$this->assertCount(2, $unserializedInnerFilter2->getFilters());
|
||||
|
||||
/** @var EqFilter $unserializedInnerFilter21 */
|
||||
$unserializedInnerFilter21 = $unserializedInnerFilter2->getFilters()[0];
|
||||
/** @var NotFilter $unserializedInnerFilter22 */
|
||||
$unserializedInnerFilter22 = $unserializedInnerFilter2->getFilters()[1];
|
||||
|
||||
$this->assertInstanceOf(EqFilter::class, $unserializedInnerFilter21);
|
||||
$this->assertSame('another', $unserializedInnerFilter21->getFieldName());
|
||||
$this->assertSame('foo', $unserializedInnerFilter21->getValue());
|
||||
|
||||
$this->assertInstanceOf(NotFilter::class, $unserializedInnerFilter22);
|
||||
|
||||
/** @var GteFilter $unserializedInnerFilter22Inner */
|
||||
$unserializedInnerFilter22Inner = $unserializedInnerFilter22->getFilter();
|
||||
|
||||
$this->assertInstanceOf(GteFilter::class, $unserializedInnerFilter22Inner);
|
||||
$this->assertSame('bar', $unserializedInnerFilter22Inner->getFieldName());
|
||||
$this->assertEquals(
|
||||
$date->format('c'),
|
||||
$unserializedInnerFilter22Inner->getValue()->format('c')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSort()
|
||||
{
|
||||
$date = new \DateTime();
|
||||
|
||||
$filter1 = new EqFilter('some', 10);
|
||||
$filter21 = new EqFilter('another', 'foo');
|
||||
$filter22 = new NotFilter(new GteFilter('bar', $date));
|
||||
$filter2 = new AndFilter([ $filter22, $filter21 ]);
|
||||
$filter = new OrFilter([ $filter2, $filter1 ]);
|
||||
|
||||
$filter->sort();
|
||||
|
||||
$this->assertSame([ $filter1, $filter2 ], $filter->getFilters());
|
||||
$this->assertSame([ $filter21, $filter22 ], $filter2->getFilters());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareValueFilters()
|
||||
{
|
||||
$filter = $this->getMockForAbstractClass(AbstractGroupFilter::class);
|
||||
|
||||
$this->assertGreaterThan(0, $this->call($filter, 'compareValueFilters', [
|
||||
new EqFilter('b', 10),
|
||||
new EqFilter('a', 10),
|
||||
]));
|
||||
|
||||
$this->assertLessThan(0, $this->call($filter, 'compareValueFilters', [
|
||||
new EqFilter('a', 9),
|
||||
new EqFilter('a', 10),
|
||||
]));
|
||||
|
||||
$this->assertEquals(0, $this->call($filter, 'compareValueFilters', [
|
||||
new GteFilter('a', 10),
|
||||
new LteFilter('a', 10),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareInFilters()
|
||||
{
|
||||
$filter = $this->getMockForAbstractClass(AbstractGroupFilter::class);
|
||||
|
||||
$this->assertGreaterThan(0, $this->call($filter, 'compareInFilters', [
|
||||
new InFilter('b', [ 10 ]),
|
||||
new InFilter('a', [ 10 ]),
|
||||
]));
|
||||
|
||||
$this->assertLessThan(0, $this->call($filter, 'compareInFilters', [
|
||||
new InFilter('a', [ 9 ]),
|
||||
new InFilter('a', [ 10 ]),
|
||||
]));
|
||||
|
||||
$this->assertEquals(0, $this->call($filter, 'compareInFilters', [
|
||||
new InFilter('a', [ 10 ]),
|
||||
new InFilter('a', [ 10 ]),
|
||||
]));
|
||||
|
||||
$this->assertLessThan(0, $this->call($filter, 'compareInFilters', [
|
||||
new InFilter('a', [ 10 ]),
|
||||
new InFilter('a', [ 10, 10 ]),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCompareGroupFilters()
|
||||
{
|
||||
$filter = $this->getMockForAbstractClass(AbstractGroupFilter::class);
|
||||
|
||||
$this->assertEquals(0, $this->call($filter, 'compareGroupFilters', [
|
||||
new AndFilter(),
|
||||
new AndFilter(),
|
||||
]));
|
||||
|
||||
$this->assertEquals(0, $this->call($filter, 'compareGroupFilters', [
|
||||
new AndFilter([ new AndFilter([ new EqFilter('a', 10) ]) ]),
|
||||
new AndFilter([ new AndFilter([ new GteFilter('a', 10) ]) ]),
|
||||
]));
|
||||
|
||||
$this->assertGreaterThan(0, $this->call($filter, 'compareGroupFilters', [
|
||||
new AndFilter([ new EqFilter('a', 10) ]),
|
||||
new AndFilter([ new EqFilter('a', 9) ]),
|
||||
]));
|
||||
|
||||
$this->assertLessThan(0, $this->call($filter, 'compareGroupFilters', [
|
||||
new AndFilter([ new EqFilter('a', 10) ]),
|
||||
new AndFilter([ new AndFilter() ]),
|
||||
]));
|
||||
|
||||
$this->assertLessThan(0, $this->call($filter, 'compareGroupFilters', [
|
||||
new AndFilter(),
|
||||
new AndFilter([ new AndFilter() ]),
|
||||
]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace IndexBundle\Filter\Filters;
|
||||
|
||||
use Common\Enum\PublisherTypeEnum;
|
||||
use Tests\AppTestCase;
|
||||
|
||||
/**
|
||||
* Class InFilterTest
|
||||
*
|
||||
* @package IndexBundle\Filter\Filters
|
||||
*/
|
||||
class InFilterTest extends AppTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$filter = new InFilter('foo', []);
|
||||
$this->assertEquals('foo', $filter->getFieldName());
|
||||
$this->assertEquals([], $filter->getValue());
|
||||
|
||||
$dateMut = date_create();
|
||||
$dateImm = date_create_immutable(' + 1 day');
|
||||
|
||||
$filter = new InFilter('bar', [
|
||||
12,
|
||||
true,
|
||||
2.3,
|
||||
'string',
|
||||
$dateMut,
|
||||
$dateImm,
|
||||
PublisherTypeEnum::unknown(),
|
||||
]);
|
||||
$this->assertEquals('bar', $filter->getFieldName());
|
||||
$this->assertEquals([
|
||||
12,
|
||||
true,
|
||||
2.3,
|
||||
'string',
|
||||
$dateMut,
|
||||
$dateImm,
|
||||
PublisherTypeEnum::UNKNOWN,
|
||||
], $filter->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage '$field' should be string
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateInvalidField()
|
||||
{
|
||||
new InFilter(1, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage '$values' should be an array of scalar values, AbstractEnum or \DateTimeInterface instances
|
||||
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateInvalidValues()
|
||||
{
|
||||
new InFilter('foo', [ [ 1 ] ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSerialize()
|
||||
{
|
||||
$values = [ 10, 20, 100 ];
|
||||
|
||||
$filter = new InFilter('foo', $values);
|
||||
|
||||
/** @var InFilter $unserializedFilter */
|
||||
$unserializedFilter = unserialize(serialize($filter));
|
||||
|
||||
$this->assertInstanceOf(InFilter::class, $unserializedFilter);
|
||||
$this->assertSame('foo', $unserializedFilter->getFieldName());
|
||||
$this->assertSame($values, $unserializedFilter->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSort()
|
||||
{
|
||||
$filter = new InFilter('foo', [ 11, -3, 30]);
|
||||
$filter->sort();
|
||||
$this->assertSame([-3, 11, 30], $filter->getValue());
|
||||
|
||||
$date1 = new \DateTime('+ 1 day');
|
||||
$date2 = new \DateTime();
|
||||
$date3 = new \DateTime('- 1 month');
|
||||
|
||||
$filter = new InFilter('some', [ $date1, $date2, $date3 ]);
|
||||
$filter->sort();
|
||||
$this->assertSame([ $date3, $date2, $date1 ], $filter->getValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace IndexBundle\Filter\Filters;
|
||||
|
||||
use Common\Enum\PublisherTypeEnum;
|
||||
use IndexBundle\Filter\AbstractValueFilter;
|
||||
use Tests\AppTestCase;
|
||||
|
||||
/**
|
||||
* Class ValueFilterTest
|
||||
*
|
||||
* @package IndexBundle\Filter\Filters
|
||||
*/
|
||||
class ValueFilterTest extends AppTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$mock = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', 10 ]);
|
||||
$this->assertEquals('foo', $mock->getFieldName());
|
||||
$this->assertEquals(10, $mock->getValue());
|
||||
|
||||
$mock = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', 'string' ]);
|
||||
$this->assertEquals('foo', $mock->getFieldName());
|
||||
$this->assertEquals('string', $mock->getValue());
|
||||
|
||||
$mock = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', 2.4 ]);
|
||||
$this->assertEquals('foo', $mock->getFieldName());
|
||||
$this->assertEquals(2.4, $mock->getValue());
|
||||
|
||||
$mock = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', true ]);
|
||||
$this->assertEquals('foo', $mock->getFieldName());
|
||||
$this->assertEquals(true, $mock->getValue());
|
||||
|
||||
$date = date_create();
|
||||
$mock = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', $date ]);
|
||||
$this->assertEquals('foo', $mock->getFieldName());
|
||||
$this->assertEquals($date, $mock->getValue());
|
||||
|
||||
$date = date_create_immutable();
|
||||
$mock = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', $date ]);
|
||||
$this->assertEquals('foo', $mock->getFieldName());
|
||||
$this->assertEquals($date, $mock->getValue());
|
||||
|
||||
$mock = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', PublisherTypeEnum::blogs() ]);
|
||||
$this->assertEquals('foo', $mock->getFieldName());
|
||||
$this->assertEquals(PublisherTypeEnum::BLOGS, $mock->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage '$field' should be string
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateInvalidField()
|
||||
{
|
||||
$this->getMockForAbstractClass(AbstractValueFilter::class, [ 1, 10 ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage '$value' should be scalar, AbstractEnum or \DateTimeInterface instance
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateInvalidValues()
|
||||
{
|
||||
$this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', [ 1 ] ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSerialize()
|
||||
{
|
||||
$filter = $this->getMockForAbstractClass(AbstractValueFilter::class, [
|
||||
'foo',
|
||||
20,
|
||||
]);
|
||||
|
||||
/** @var AbstractValueFilter $unserializedFilter */
|
||||
$unserializedFilter = unserialize(serialize($filter));
|
||||
|
||||
$this->assertInstanceOf(AbstractValueFilter::class, $unserializedFilter);
|
||||
$this->assertSame('foo', $unserializedFilter->getFieldName());
|
||||
$this->assertSame(20, $unserializedFilter->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testSort()
|
||||
{
|
||||
$filter = $this->getMockForAbstractClass(AbstractValueFilter::class, [ 'foo', 10 ]);
|
||||
$filter->sort();
|
||||
$this->assertSame(10, $filter->getValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace IndexBundle\Index\External;
|
||||
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\Cache\Adapter\NullAdapter;
|
||||
use Tests\AppTestCase;
|
||||
|
||||
/**
|
||||
* Class HoseIndexTest
|
||||
*
|
||||
* @package IndexBundle\Index\External
|
||||
*/
|
||||
class HoseIndexTest extends AppTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var HoseIndex
|
||||
*/
|
||||
private $index;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testBeforeSearch()
|
||||
{
|
||||
$parameters = [
|
||||
'body' => [ 'query' => [ 'query_string' => [ 'query' => sprintf(
|
||||
'published:[%s TO *]',
|
||||
date_create('+ 36 days')->format('c')
|
||||
), ], ], ],
|
||||
];
|
||||
|
||||
$parameters = $this->call($this->index, 'beforeSearch', [ $parameters ]);
|
||||
|
||||
$this->assertArrayHasKey('index', $parameters);
|
||||
$this->assertEquals(
|
||||
implode(',', [ HoseIndex::HOT, HoseIndex::WARM ]),
|
||||
$parameters['index']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getQueryRangeEndProvider
|
||||
*
|
||||
* @param \DateTime|null $expected Expected result.
|
||||
* @param string $query ES query string.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetQueryRangeEnd(\DateTime $expected = null, $query)
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->call($this->index, 'getQueryRangeEnd', [ $query ])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getQueryRangeEndProvider()
|
||||
{
|
||||
$date1 = date_create('+ 36 days 00:00:00');
|
||||
|
||||
return [
|
||||
[
|
||||
$date1,
|
||||
sprintf('published:[%s TO *]', $date1->format('c')),
|
||||
],
|
||||
[
|
||||
date_create('2017-11-27T00:00:00+07:00'),
|
||||
'{"body":{"query":{"query_string":{"query":"(main:("Clarion School" -"North Clarion") OR title:("Clarion School" -"North Clarion")) AND ((source_publisher_type:(MAINSTREAM_NEWS OR REVIEW OR CLASSIFIED OR UNKNOWN OR WEBLOG OR MICROBLOG OR UNKNOWN OR SOCIAL_MEDIA OR UNKNOWN OR VIDEO OR UNKNOWN OR FORUM OR MEMETRACKER OR UNKNOWN OR PHOTO OR UNKNOWN)) AND ((published:[2017-11-27T00:00:00+07:00 TO *]) AND (published:[* TO 2018-01-26T23:59:59+07:00])) AND (published:[* TO 2018-01-26T19:20:26+07:00]))"}},"sort":{"published":{"order":"desc"}}},"index":"content_*","type":"","size":100,"from":0}',
|
||||
],
|
||||
[
|
||||
null,
|
||||
'',
|
||||
],
|
||||
[
|
||||
null,
|
||||
'{"body":{"query":{"query_string":{"query":"(main:("Clarion School" -"North Clarion") OR title:("Clarion School" -"North Clarion")) AND ((source_publisher_type:(MAINSTREAM_NEWS OR REVIEW OR CLASSIFIED OR UNKNOWN OR WEBLOG OR MICROBLOG OR UNKNOWN OR SOCIAL_MEDIA OR UNKNOWN OR VIDEO OR UNKNOWN OR FORUM OR MEMETRACKER OR UNKNOWN OR PHOTO OR UNKNOWN)))"}},"sort":{"published":{"order":"desc"}}},"index":"content_*","type":"","size":100,"from":0}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider determineIndexProvider
|
||||
*
|
||||
* @param array $expected Expected hose indices.
|
||||
* @param array $arguments Tested method arguments.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDetermineIndex(array $expected, array $arguments)
|
||||
{
|
||||
$this->assertEquals(
|
||||
implode(',', $expected),
|
||||
$this->call($this->index, 'determineIndex', $arguments)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function determineIndexProvider()
|
||||
{
|
||||
return [
|
||||
'without date' => [
|
||||
[ HoseIndex::HOT, HoseIndex::WARM, HoseIndex::COLD ],
|
||||
[],
|
||||
],
|
||||
'72 days ahead' => [
|
||||
[ HoseIndex::HOT, HoseIndex::WARM, HoseIndex::COLD ],
|
||||
[ date_create('+ 72 days') ],
|
||||
],
|
||||
'60 days and 1 hour ahead' => [
|
||||
[ HoseIndex::HOT, HoseIndex::WARM, HoseIndex::COLD ],
|
||||
[ date_create('+ 60 days 01:00:00') ],
|
||||
],
|
||||
'60 days ahead' => [
|
||||
[ HoseIndex::HOT, HoseIndex::WARM ],
|
||||
[ date_create('+ 60 days 00:00:00') ],
|
||||
],
|
||||
'36 days ahead' => [
|
||||
[ HoseIndex::HOT, HoseIndex::WARM ],
|
||||
[ date_create('+ 36 days') ],
|
||||
],
|
||||
'30 days and 1 hour ahead' => [
|
||||
[ HoseIndex::HOT, HoseIndex::WARM ],
|
||||
[ date_create('+ 30 days 01:00:00') ],
|
||||
],
|
||||
'30 days ahead' => [
|
||||
[ HoseIndex::HOT ],
|
||||
[ date_create('+ 30 days 00:00:00') ],
|
||||
],
|
||||
'21 days ahead' => [
|
||||
[ HoseIndex::HOT ],
|
||||
[ date_create('+ 21 days') ],
|
||||
],
|
||||
'current date' => [
|
||||
[ HoseIndex::HOT ],
|
||||
[ date_create() ],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, open a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->index = new HoseIndex(
|
||||
new NullLogger(),
|
||||
new NullAdapter(),
|
||||
'host',
|
||||
'vendor',
|
||||
'auth'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\IndexBundle\Model;
|
||||
|
||||
use IndexBundle\Index\Strategy\IndexStrategyInterface;
|
||||
use Tests\AppTestCase;
|
||||
|
||||
/**
|
||||
* Class TestModel
|
||||
* @package Tests\IndexBundle\Model
|
||||
*/
|
||||
class AbstractIndexDocumentTest extends AppTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Array of available document properties.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $available = [
|
||||
'first',
|
||||
'second',
|
||||
'third',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var TestModel
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, open a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
/** @var IndexStrategyInterface|\PHPUnit_Framework_MockObject_MockObject $strategy */
|
||||
$strategy = $this->getMockForInterface(IndexStrategyInterface::class);
|
||||
|
||||
$strategy
|
||||
->method('normalizeDocumentData')
|
||||
->willReturnCallback(function (array $data) {
|
||||
return $data;
|
||||
});
|
||||
|
||||
$strategy
|
||||
->method('getIndexableData')
|
||||
->willReturnCallback(function (array $data) {
|
||||
return $data;
|
||||
});
|
||||
|
||||
$strategy
|
||||
->method('normalizeFieldName')
|
||||
->willReturnCallback(function ($fieldName) {
|
||||
return $fieldName;
|
||||
});
|
||||
|
||||
$strategy
|
||||
->method('denormalizeFieldName')
|
||||
->willReturnCallback(function ($fieldName) {
|
||||
return $fieldName;
|
||||
});
|
||||
|
||||
$strategy
|
||||
->method('normalizePublisherType')
|
||||
->willReturnCallback(function ($type) {
|
||||
return $type;
|
||||
});
|
||||
|
||||
$strategy
|
||||
->method('denormalizePublisherType')
|
||||
->willReturnCallback(function ($type) {
|
||||
return $type;
|
||||
});
|
||||
|
||||
$this->model = new TestModel($strategy, [
|
||||
'first' => 1,
|
||||
'second' => 'two',
|
||||
'third' => [ 3, 2, 1 ],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAccessAsArray()
|
||||
{
|
||||
$this->assertSame(1, $this->model['first']);
|
||||
$this->assertSame('two', $this->model['second']);
|
||||
$this->assertSame([ 3, 2, 1], $this->model['third']);
|
||||
|
||||
$this->model['first'] = 2;
|
||||
$this->model['second'] = 'third';
|
||||
$this->model['third'] = [ 1, 2, 3 ];
|
||||
|
||||
$this->assertSame(2, $this->model['first']);
|
||||
$this->assertSame('third', $this->model['second']);
|
||||
$this->assertSame([ 1, 2, 3], $this->model['third']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testIterate()
|
||||
{
|
||||
$expected = [
|
||||
'first' => 1,
|
||||
'second' => 'two',
|
||||
'third' => [ 3, 2, 1 ],
|
||||
];
|
||||
|
||||
reset($expected);
|
||||
foreach ($this->model as $name => $value) {
|
||||
$this->assertSame(key($expected), $name);
|
||||
$this->assertSame(current($expected), $value);
|
||||
|
||||
next($expected);
|
||||
}
|
||||
|
||||
$this->model['first'] = 2;
|
||||
$this->model['second'] = 'third';
|
||||
$this->model['third'] = [ 1, 2, 3 ];
|
||||
|
||||
$expected = [
|
||||
'first' => 2,
|
||||
'second' => 'third',
|
||||
'third' => [ 1, 2, 3 ],
|
||||
];
|
||||
|
||||
reset($expected);
|
||||
foreach ($this->model as $name => $value) {
|
||||
$this->assertSame(key($expected), $name);
|
||||
$this->assertSame(current($expected), $value);
|
||||
|
||||
next($expected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Unknown property 'fourth'
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAccessAsArrayInvalidProperty()
|
||||
{
|
||||
$this->model['fourth'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testAccessAsProperty()
|
||||
{
|
||||
$this->assertSame(1, $this->model->first);
|
||||
$this->assertSame('two', $this->model->second);
|
||||
$this->assertSame([ 3, 2, 1], $this->model->third);
|
||||
|
||||
$this->model->first = 2;
|
||||
$this->model->second = 'third';
|
||||
$this->model->third = [ 1, 2, 3 ];
|
||||
|
||||
$this->assertSame(2, $this->model->first);
|
||||
$this->assertSame('third', $this->model->second);
|
||||
$this->assertSame([ 1, 2, 3], $this->model->third);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\IndexBundle\Model;
|
||||
|
||||
use IndexBundle\Model\AbstractDocument;
|
||||
|
||||
/**
|
||||
* Class TestModel
|
||||
* @package Tests\IndexBundle\Model
|
||||
*
|
||||
* @property integer $first
|
||||
* @property string $second
|
||||
* @property array $third
|
||||
*/
|
||||
class TestModel extends AbstractDocument
|
||||
{
|
||||
|
||||
/**
|
||||
* Check that this document is normalized or not.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isNormalized()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
namespace IndexBundle\Normalizer\Query;
|
||||
|
||||
use Tests\AppTestCase;
|
||||
|
||||
/**
|
||||
* Class QueryNormalizerTest
|
||||
* @package AppBundle\Search\Request\Normalizer
|
||||
*/
|
||||
class QueryNormalizerTest extends AppTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var QueryNormalizer
|
||||
*/
|
||||
private $normalizer;
|
||||
|
||||
/**
|
||||
* Sets up the fixture, for example, open a network connection.
|
||||
* This method is called before a test is executed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->normalizer = new QueryNormalizer();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider queriesProvider
|
||||
*
|
||||
* @param string $query1 First search query.
|
||||
* @param string $query2 Second search query.
|
||||
* @param boolean $expects Match or don't.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGenerateUniqueKey($query1, $query2, $expects)
|
||||
{
|
||||
$query1 = $this->normalizer->normalize($query1);
|
||||
$query2 = $this->normalizer->normalize($query2);
|
||||
|
||||
$message = "Key's must be ". ($expects ? 'same' : 'differ')
|
||||
.', but first query is '. $query1 .' and second is '. $query2;
|
||||
$this->assertEquals($query1 === $query2, $expects, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function queriesProvider()
|
||||
{
|
||||
return [
|
||||
'cat dog === dog cat' => [
|
||||
'cat dog',
|
||||
'dog cat',
|
||||
true,
|
||||
],
|
||||
'cat~ dog~0.8 === dog~0.8 cat~' => [
|
||||
'cat~ dog~0.8',
|
||||
'dog~0.8 cat~',
|
||||
true,
|
||||
],
|
||||
'cat~ dog~0.7 !== dog~0.8 cat~' => [
|
||||
'cat~ dog~0.7',
|
||||
'dog~0.8 cat~',
|
||||
false,
|
||||
],
|
||||
'ca?t dog === dog ca?t' => [
|
||||
'ca?t dog',
|
||||
'dog ca?t',
|
||||
true,
|
||||
],
|
||||
'c?at dog !== dog ca?t' => [
|
||||
'c?at dog',
|
||||
'dog ca?t',
|
||||
false,
|
||||
],
|
||||
'cat^4 dog === dog cat^4' => [
|
||||
'cat^4 dog',
|
||||
'dog cat^4',
|
||||
true,
|
||||
],
|
||||
'cat^3 dog !== dog cat^4' => [
|
||||
'cat^3 dog',
|
||||
'dog cat^4',
|
||||
false,
|
||||
],
|
||||
'* dog === dog *' => [
|
||||
'* dog',
|
||||
'dog *',
|
||||
true,
|
||||
],
|
||||
'+cat dog === dog +cat' => [
|
||||
'+cat dog',
|
||||
'dog +cat',
|
||||
true,
|
||||
],
|
||||
'NOT cat AND dog === dog AND NOT cat' => [
|
||||
'NOT cat AND dog',
|
||||
'dog AND NOT cat',
|
||||
true,
|
||||
],
|
||||
'NOT (cat AND (dog OR fish)) === NOT ((fish OR dog) AND cat)' => [
|
||||
'NOT (cat AND (dog OR fish))',
|
||||
'NOT ((fish OR dog) AND cat)',
|
||||
true,
|
||||
],
|
||||
' AND or NOT !== NOT or AND' => [
|
||||
' AND or NOT',
|
||||
'NOT or AND',
|
||||
false,
|
||||
],
|
||||
'OR AND NOT === NOT OR AND' => [
|
||||
'OR AND NOT',
|
||||
'NOT OR AND',
|
||||
true,
|
||||
],
|
||||
'NOT cat !== cat NOT' => [
|
||||
'NOT cat',
|
||||
'cat NOT',
|
||||
false,
|
||||
],
|
||||
'cat AND dog !== cat and dog' => [
|
||||
'cat AND dog',
|
||||
'cat and dog',
|
||||
false,
|
||||
],
|
||||
'"cat fly" AND dog === dog AND "cat fly"' => [
|
||||
'"cat fly" AND dog',
|
||||
'dog AND "cat fly"',
|
||||
true,
|
||||
],
|
||||
'"cat fly"^3 AND dog === dog AND "cat fly"^3' => [
|
||||
'"cat fly"^3 AND dog',
|
||||
'dog AND "cat fly"^3',
|
||||
true,
|
||||
],
|
||||
'"cat fly"^4 AND dog !== dog AND "cat fly"^3' => [
|
||||
'"cat fly"^4 AND dog',
|
||||
'dog AND "cat fly"^3',
|
||||
false,
|
||||
],
|
||||
'"cat fly"~0.4 AND dog === dog AND "cat fly"~0.4' => [
|
||||
'"cat fly"~0.4 AND dog',
|
||||
'dog AND "cat fly"~0.4',
|
||||
true,
|
||||
],
|
||||
'"cat fly"~0.3 AND dog !== dog AND "cat fly"~0.4' => [
|
||||
'"cat fly"~0.3 AND dog',
|
||||
'dog AND "cat fly"~0.4',
|
||||
false,
|
||||
],
|
||||
'"cat fly"+ AND dog !== dog AND "cat fly"' => [
|
||||
'"cat fly"+ AND dog',
|
||||
'dog AND "cat fly"',
|
||||
false,
|
||||
],
|
||||
'"cat fly"+ AND dog === dog AND "cat fly"+' => [
|
||||
'"cat fly"+ AND dog',
|
||||
'dog AND "cat fly"+',
|
||||
true,
|
||||
],
|
||||
'cat AND dog !== cat dog' => [
|
||||
'cat AND dog',
|
||||
'cat dog',
|
||||
false,
|
||||
],
|
||||
'cat OR dog === cat dog' => [
|
||||
'cat OR dog',
|
||||
'cat dog',
|
||||
true,
|
||||
],
|
||||
'(cat) dog === cat dog' => [
|
||||
'(cat) dog',
|
||||
'cat dog',
|
||||
true,
|
||||
],
|
||||
'(cat AND dog) === cat AND dog' => [
|
||||
'(cat AND dog)',
|
||||
'cat AND dog',
|
||||
true,
|
||||
],
|
||||
'(cat dog) === dog cat' => [
|
||||
'(cat dog)',
|
||||
'dog cat',
|
||||
true,
|
||||
],
|
||||
'(((cat OR dog))) === cat dog' => [
|
||||
'(((cat OR dog)))',
|
||||
'cat dog',
|
||||
true,
|
||||
],
|
||||
'(cat AND dog) OR fish === cat AND dog OR fish' => [
|
||||
'(cat AND dog) OR fish',
|
||||
'cat AND dog OR fish',
|
||||
true,
|
||||
],
|
||||
'cat AND (dog OR fish) !== cat AND dog OR fish' => [
|
||||
'cat AND (dog OR fish)',
|
||||
'cat AND dog OR fish',
|
||||
false,
|
||||
],
|
||||
'(cat AND dog) (fish AND bird) === (cAt AND dog) (fish AND BIRd)' => [
|
||||
'(cat AND dog) (fish AND bird)',
|
||||
'(cAt AND dog) (fish AND BIRd)',
|
||||
true,
|
||||
],
|
||||
'(cat AND dog) AND bird === cAt AND BIRD AND dOg' => [
|
||||
'(cat AND dog) AND bird',
|
||||
'cAt AND BIRD AND dOg',
|
||||
true,
|
||||
],
|
||||
'(cat OR dog) bird === cAt bird doG' => [
|
||||
'(cat OR dog) bird',
|
||||
'cAt bird doG',
|
||||
true,
|
||||
],
|
||||
'(dog OR dog) AND cat === cat AND doG' => [
|
||||
'(dog OR dog) AND cat',
|
||||
'cat AND doG',
|
||||
true,
|
||||
],
|
||||
'(dog AND dog) AND cat === cat AND doG' => [
|
||||
'(dog AND dog) AND cat',
|
||||
'cat AND doG',
|
||||
true,
|
||||
],
|
||||
'(dog AND dog) AND cat AND (fish OR fish) === fish AND cat AND doG' => [
|
||||
'(dog AND dog) AND cat AND (fish OR fish)',
|
||||
'fish AND cat AND doG',
|
||||
true,
|
||||
],
|
||||
'(dog AND dog) OR (cat AND cat) === dog OR (cat)' => [
|
||||
'(dog AND dog) OR (cat AND cat)',
|
||||
'dog OR (cat)',
|
||||
true,
|
||||
],
|
||||
'"NOT ("dog cat"^3 AND (Alice OR "fish"~0.3)) OR NOT ("dog cat fish"~ AND NOT (+"Alice" OR dog~0.3 OR man^3))' => [
|
||||
'NOT ("dog cat"^3 AND (Alice OR "fish"~0.3)) OR NOT ("dog cat fish"~ AND NOT (+"Alice" OR dog~0.3 OR man^3))',
|
||||
'NOT (NOT (man^3 OR +"Alice" OR dog~0.3) AND "dog cat fish"~) OR NOT ("dog cat"^3 AND ("fish"~0.3 OR Alice))',
|
||||
true,
|
||||
],
|
||||
'+Ethiopia +Sudan -”South Africa” === -”South Africa” +Ethiopia +Sudan' => [
|
||||
'+Ethiopia +Sudan -”South Africa”',
|
||||
'-”South Africa” +Ethiopia +Sudan',
|
||||
true,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user