From aa0b40b6792e271f253ffb573fb18a1dbabdbeea Mon Sep 17 00:00:00 2001 From: Mathieu Darse Date: Fri, 3 Jul 2015 19:38:36 +0200 Subject: [PATCH] Test TermNode --- .../SearchEngine/Elastic/AST/TermNode.php | 2 +- .../Phrasea/SearchEngine/AST/TermNodeTest.php | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/Alchemy/Tests/Phrasea/SearchEngine/AST/TermNodeTest.php diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TermNode.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TermNode.php index b18b0cbb58..0b26997d0e 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TermNode.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TermNode.php @@ -9,7 +9,7 @@ class TermNode extends AbstractTermNode { public function buildQuery(QueryContext $context) { - $query = array(); + $query = []; $query['bool']['should'] = $this->buildConceptQueries($context); return $query; diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/AST/TermNodeTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/AST/TermNodeTest.php new file mode 100644 index 0000000000..70506d9c6e --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/AST/TermNodeTest.php @@ -0,0 +1,66 @@ +assertTrue(method_exists(TermNode::class, '__toString'), 'Class does not have method __toString'); + $node = new TermNode('foo'); + $this->assertEquals('', (string) $node); + $node_with_context = new TermNode('foo', new Context('bar')); + $this->assertEquals('', (string) $node_with_context); + } + + public function testQueryBuild() + { + $query_context = $this->prophesize(QueryContext::class); + $query_context + ->getLocalizedFields() + ->willReturn(['foo.fr', 'foo.en']); + $query_context + ->getAllowedPrivateFields() + ->willReturn([]); + $query_context + ->getFields() + ->willReturn(['foo']); + + $node = new TermNode('bar'); + $node->setConcepts([ + new Concept('/baz'), + new Concept('/qux'), + ]); + $query = $node->buildQuery($query_context->reveal()); + + $expected = '{ + "bool": { + "should": [{ + "multi_match": { + "fields": ["concept_path.foo"], + "query": "/baz" + } + }, { + "multi_match": { + "fields": ["concept_path.foo"], + "query": "/qux" + } + }] + } + }'; + + $this->assertEquals(json_decode($expected, true), $query); + } +}