Merge pull request #1525 from mdarse/new-in-syntax

Grammar unification
This commit is contained in:
Benoît Burnichon
2015-11-04 21:16:03 +01:00
25 changed files with 424 additions and 1567 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace Alchemy\Tests\Phrasea\SearchEngine\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\AST\Field as ASTField;
use Alchemy\Phrasea\SearchEngine\Elastic\AST\FieldEqualsExpression;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Field as StructureField;
/**
* @group unit
* @group searchengine
* @group ast
*/
class FieldEqualsExpressionTest extends \PHPUnit_Framework_TestCase
{
public function testSerialization()
{
$this->assertTrue(method_exists(FieldEqualsExpression::class, '__toString'), 'Class does not have method __toString');
$field = $this->prophesize(ASTField::class);
$field->__toString()->willReturn('foo');
$node = new FieldEqualsExpression($field->reveal(), 'bar');
$this->assertEquals('(foo == <value:"bar">)', (string) $node);
}
/**
* @dataProvider queryProvider
*/
public function testQueryBuild($index_field, $value, $compatible_value, $private, $expected_json)
{
$structure_field = $this->prophesize(StructureField::class);
$structure_field->isValueCompatible($value)->willReturn($compatible_value);
$structure_field->getIndexField(true)->willReturn($index_field);
$structure_field->isPrivate()->willReturn($private);
if ($private) {
$structure_field->getDependantCollections()->willReturn(['baz', 'qux']);
}
$ast_field = $this->prophesize(ASTField::class);
$query_context = $this->prophesize(QueryContext::class);
$query_context->get($ast_field->reveal())->willReturn($structure_field);
$node = new FieldEqualsExpression($ast_field->reveal(), 'bar');
$query = $node->buildQuery($query_context->reveal());
$this->assertEquals(json_decode($expected_json, true), $query);
}
public function queryProvider()
{
return [
['foo.raw', 'bar', true, true, '{
"filtered": {
"filter": {
"terms": {
"base_id": ["baz","qux"] } },
"query": {
"term": {
"foo.raw": "bar" } } } }'],
['foo.raw', 'bar', true, false, '{
"term": {
"foo.raw": "bar" } }'],
['foo.raw', 'bar', false, true, 'null'],
['foo.raw', 'bar', false, false, 'null'],
];
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Alchemy\Tests\Phrasea\SearchEngine\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\AST\KeyValue\Key;
use Alchemy\Phrasea\SearchEngine\Elastic\AST\KeyValue\Expression as KeyValueExpression;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
/**
* @group unit
* @group searchengine
* @group ast
*/
class KeyValueExpressionTest extends \PHPUnit_Framework_TestCase
{
public function testSerialization()
{
$this->assertTrue(method_exists(KeyValueExpression::class, '__toString'), 'Class does not have method __toString');
$key = $this->prophesize(Key::class);
$key->__toString()->willReturn('foo');
$node = new KeyValueExpression($key->reveal(), 'bar');
$this->assertEquals('<foo:bar>', (string) $node);
}
public function testQueryBuild()
{
$query_context = $this->prophesize(QueryContext::class);
$key = $this->prophesize(Key::class);
$key->buildQueryForValue('bar', $query_context->reveal())->willReturn('baz');
$node = new KeyValueExpression($key->reveal(), 'bar');
$query = $node->buildQuery($query_context->reveal());
$this->assertEquals('baz', $query);
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Alchemy\Tests\Phrasea\SearchEngine\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\AST\KeyValue\NativeKey;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
/**
* @group unit
* @group searchengine
* @group ast
*/
class NativeKeyTest extends \PHPUnit_Framework_TestCase
{
public function testSerialization()
{
$this->assertTrue(method_exists(NativeKey::class, '__toString'), 'Class does not have method __toString');
$this->assertEquals('database', (string) NativeKey::database());
$this->assertEquals('collection', (string) NativeKey::collection());
$this->assertEquals('media_type', (string) NativeKey::mediaType());
$this->assertEquals('record_identifier', (string) NativeKey::recordIdentifier());
}
public function testDatabaseQuery()
{
$query_context = $this->prophesize(QueryContext::class);
$key = NativeKey::database();
$query = $key->buildQueryForValue('bar', $query_context->reveal());
$expected = '{
"term": {
"databox_name": "bar"
}
}';
$this->assertEquals(json_decode($expected, true), $query);
}
public function testCollectionQuery()
{
$query_context = $this->prophesize(QueryContext::class);
$key = NativeKey::collection();
$query = $key->buildQueryForValue('bar', $query_context->reveal());
$expected = '{
"term": {
"collection_name": "bar"
}
}';
$this->assertEquals(json_decode($expected, true), $query);
}
public function testMediaTypeQuery()
{
$query_context = $this->prophesize(QueryContext::class);
$key = NativeKey::mediaType();
$query = $key->buildQueryForValue('bar', $query_context->reveal());
$expected = '{
"term": {
"type": "bar"
}
}';
$this->assertEquals(json_decode($expected, true), $query);
}
public function testRecordIdentifierQuery()
{
$query_context = $this->prophesize(QueryContext::class);
$key = NativeKey::recordIdentifier();
$query = $key->buildQueryForValue('bar', $query_context->reveal());
$expected = '{
"term": {
"record_id": "bar"
}
}';
$this->assertEquals(json_decode($expected, true), $query);
}
}

View File

@@ -1,5 +1,7 @@
foo|<text:"foo">
foo (bar)|<text:"foo" context:"bar">
foo "bar"|(<text:"foo "> AND <exact_text:"bar">)
"foo" bar|(<exact_text:"foo"> AND <text:" bar">)
# foo ("bar baz")|<text:"foo" context:"bar baz">
foo bar|<text:"foo bar">
foo bar (baz qux)|<text:"foo bar" context:"baz qux">
@@ -46,7 +48,7 @@ foo EXCEPT (bar AND baz)|(<text:"foo"> EXCEPT (<text:"bar"> AND <text:"baz">))
foo EXCEPT (bar OR baz)|(<text:"foo"> EXCEPT (<text:"bar"> OR <text:"baz">))
foo EXCEPT (bar EXCEPT baz)|(<text:"foo"> EXCEPT (<text:"bar"> EXCEPT <text:"baz">))
# Inequality operators
# Comparison operators
foo < 42|<range:foo lt="42">
foo ≤ 42|<range:foo lte="42">
foo > 42|<range:foo gt="42">
@@ -59,23 +61,36 @@ foo < "2015/01/01"|<range:foo lt="2015/01/01">
foo ≤ "2015/01/01"|<range:foo lte="2015/01/01">
foo > "2015/01/01"|<range:foo gt="2015/01/01">
foo ≥ "2015/01/01"|<range:foo gte="2015/01/01">
foo = 42|(<field:foo> == <value:"42">)
foo = bar|(<field:foo> == <value:"bar">)
foo = "bar"|(<field:foo> == <value:"bar">)
# Field narrowing
foo IN bar|(<text:"foo"> IN <field:bar>)
foo bar IN baz|(<text:"foo bar"> IN <field:baz>)
foo IN bar baz|<text:"foo IN bar baz">
fooINbar|<text:"fooINbar">
foo:bar|(<field:foo> MATCHES <text:"bar">)
foo:[bar]|(<field:foo> MATCHES <term:"bar">)
foo:[bar (baz)]|(<field:foo> MATCHES <term:"bar" context:"baz">)
foo:bar baz|((<field:foo> MATCHES <text:"bar">) AND <text:"baz">)
foo bar:baz|(<text:"foo"> AND (<field:bar> MATCHES <text:"baz">))
# Regular field with name colliding with a native key
field.collection:foo|(<field:collection> MATCHES <text:"foo">)
field.database:foo|(<field:database> MATCHES <text:"foo">)
field.type:foo|(<field:type> MATCHES <text:"foo">)
field.id:foo|(<field:id> MATCHES <text:"foo">)
# Matchers
collection:foo|<collection:foo>
collection:foo AND bar|(<collection:foo> AND <text:"bar">)
collection:foo bar|<text:"collection:foo bar">
collection:foo bar|(<collection:foo> AND <text:"bar">)
database:foo|<database:foo>
database:foo AND bar|(<database:foo> AND <text:"bar">)
database:foo bar|<text:"database:foo bar">
database:foo bar|(<database:foo> AND <text:"bar">)
type:foo|<media_type:foo>
type:foo AND bar|(<media_type:foo> AND <text:"bar">)
type:foo bar|(<media_type:foo> AND <text:"bar">)
id:90|<record_identifier:90>
id:90 AND foo|(<record_identifier:90> AND <text:"foo">)
id:90 foo|<text:"id:90 foo">
id:90 foo|(<record_identifier:90> AND <text:"foo">)
recordid:90|<record_identifier:90>
# Flag matcher
@@ -84,11 +99,13 @@ flag.foo:1|<flag:foo set>
flag.foo:false|<flag:foo cleared>
flag.foo:0|<flag:foo cleared>
flag.true:true|<flag:true set>
flag.foo bar:true|<text:"flag.foo bar:true">
flag.foo bar:true|(<text:"flag.foo"> AND (<field:bar> MATCHES <text:"true">))
true|<text:"true">
# Matcher on unknown name --> fulltext
foo:bar|<text:"foo:bar">
foo:bar|(<field:foo> MATCHES <text:"bar">)
foo:bar AND baz|((<field:foo> MATCHES <text:"bar">) AND <text:"baz">)
foo AND bar:baz|(<text:"foo"> AND (<field:bar> MATCHES <text:"baz">))
# Search terms with embedded keywords
INA|<text:"INA">
Can't render this file because it contains an unexpected character in line 1 and column 11.