Skip to content

Commit c68bb8e

Browse files
Add value function to expression language to allow existence checks (#173)
1 parent 1693932 commit c68bb8e

31 files changed

+168
-9
lines changed

.github/workflows/test-application.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
SYMFONY_DEPRECATIONS_HELPER: weak
1717
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1818
ELASTICSEARCH_VERSION: ${{ matrix.elasticsearch-version }}
19+
SYMFONY_MAX_PHPUNIT_VERSION: ${{ matrix.max-phpunit-version }}
1920

2021
strategy:
2122
fail-fast: false
@@ -28,6 +29,7 @@ jobs:
2829
dependency-versions: 'lowest'
2930
tools: 'composer:v1'
3031
php-cs-fixer: false
32+
max-phpunit-version: '7'
3133

3234
- php-version: '7.3'
3335
elasticsearch-version: '2.4.6'
@@ -36,6 +38,7 @@ jobs:
3638
dependency-versions: 'highest'
3739
tools: 'composer:v2'
3840
php-cs-fixer: false
41+
max-phpunit-version: '7'
3942

4043
- php-version: '7.4'
4144
elasticsearch-version: '2.4.6'
@@ -44,6 +47,7 @@ jobs:
4447
dependency-versions: 'highest'
4548
tools: 'composer:v2'
4649
php-cs-fixer: true
50+
max-phpunit-version: '7'
4751

4852
- php-version: '8.0'
4953
elasticsearch-version: '7.11.1'
@@ -52,6 +56,7 @@ jobs:
5256
dependency-versions: 'highest'
5357
tools: 'composer:v2'
5458
php-cs-fixer: false
59+
max-phpunit-version: '8'
5560

5661
- php-version: '8.1'
5762
elasticsearch-version: '7.11.1'
@@ -60,6 +65,7 @@ jobs:
6065
dependency-versions: 'highest'
6166
tools: 'composer:v2'
6267
php-cs-fixer: false
68+
max-phpunit-version: '8'
6369

6470
- php-version: '8.1'
6571
elasticsearch-version: '7.11.1'
@@ -68,6 +74,7 @@ jobs:
6874
dependency-versions: 'highest'
6975
tools: 'composer:v2'
7076
php-cs-fixer: false
77+
max-phpunit-version: '8'
7178

7279
services:
7380
elasticsearch:

Search/ExpressionLanguage/MassiveSearchExpressionLanguage.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ protected function registerFunctions()
2525

2626
$this->addFunction($this->createJoinFunction());
2727
$this->addFunction($this->createMapFunction());
28+
$this->addFunction($this->createValueFunction());
2829
}
2930

3031
/**
@@ -82,4 +83,30 @@ function(array $values, $elements, $expression) {
8283
}
8384
);
8485
}
86+
87+
/**
88+
* Value returns the value of the variable. If the variable does not exists a default will be returned.
89+
*
90+
* For example:
91+
*
92+
* massive_search_value("expression", {"hidden": false}) = array('hidden' => true);
93+
*
94+
* @return ExpressionFunction
95+
*/
96+
private function createValueFunction()
97+
{
98+
return new ExpressionFunction(
99+
'massive_search_value',
100+
function($elements, $expression) {
101+
throw new \Exception('Value function does not support compilation');
102+
},
103+
function(array $values, $variable, $default) {
104+
if (isset($values[$variable])) {
105+
return $values[$variable];
106+
}
107+
108+
return $default;
109+
}
110+
);
111+
}
85112
}

Search/ObjectToDocumentConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private function populateDocument(
181181
$type = $mapping['type'];
182182
/** @var FieldInterface $mappingField */
183183
$mappingField = $mapping['field'];
184-
$condition = method_exists($mappingField, 'getCondition') ? $mappingField->getCondition() : null;
184+
$condition = \method_exists($mappingField, 'getCondition') ? $mappingField->getCondition() : null;
185185
$validField = $condition ? $this->fieldEvaluator->evaluateCondition($object, $condition) : true;
186186

187187
if (false === $validField) {

Tests/Functional/BaseTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ abstract class BaseTestCase extends TestCase
2222
{
2323
private $kernels = [];
2424

25-
public function setUp()
25+
protected function setUp()
2626
{
2727
AppKernel::resetEnvironment();
2828
AppKernel::installDistEnvironment();
2929
}
3030

31-
public function tearDown()
31+
protected function tearDown()
3232
{
3333
AppKernel::resetEnvironment();
3434
}

Tests/ProphecyTrait.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the MassiveSearchBundle
5+
*
6+
* (c) MASSIVE ART WebServices GmbH
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Prophecy\PhpUnit;
13+
14+
trait ProphecyTrait
15+
{
16+
}

Tests/Unit/Command/PurgeCommandTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
1616
use PHPUnit\Framework\TestCase;
1717
use Prophecy\Argument;
18+
use Prophecy\PhpUnit\ProphecyTrait;
1819
use Symfony\Component\Console\Application;
1920
use Symfony\Component\Console\Helper\QuestionHelper;
2021
use Symfony\Component\Console\Tester\CommandTester;
2122

2223
class PurgeCommandTest extends TestCase
2324
{
25+
use ProphecyTrait;
26+
2427
/**
2528
* @var SearchManagerInterface
2629
*/

Tests/Unit/Command/ReindexCommandTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use Massive\Bundle\SearchBundle\Search\Reindex\ReindexProviderRegistry;
1818
use Massive\Bundle\SearchBundle\Search\Reindex\ResumeManagerInterface;
1919
use Massive\Bundle\SearchBundle\Search\SearchManager;
20+
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
2021
use PHPUnit\Framework\TestCase;
2122
use Prophecy\Argument;
23+
use Prophecy\PhpUnit\ProphecyTrait;
2224
use Symfony\Component\Console\Application;
2325
use Symfony\Component\Console\Helper\QuestionHelper;
2426
use Symfony\Component\Console\Input\InputInterface;
@@ -28,6 +30,8 @@
2830

2931
class ReindexCommandTest extends TestCase
3032
{
33+
use ProphecyTrait;
34+
3135
/**
3236
* @var ResumeManagerInterface
3337
*/
@@ -95,7 +99,7 @@ public function testCheckpointAsk()
9599
Argument::type(OutputInterface::class),
96100
Argument::type(ConfirmationQuestion::class),
97101
true
98-
)->shouldBeCalled();
102+
)->shouldBeCalled()->willReturn(true);
99103
$this->execute('prod', []);
100104
}
101105

Tests/Unit/Search/Adapter/ZendLuceneAdapterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
use Massive\Bundle\SearchBundle\Search\Field;
1818
use Massive\Bundle\SearchBundle\Search\SearchQuery;
1919
use PHPUnit\Framework\TestCase;
20+
use Prophecy\PhpUnit\ProphecyTrait;
2021
use Symfony\Component\Filesystem\Filesystem;
2122

2223
class ZendLuceneAdapterTest extends TestCase
2324
{
25+
use ProphecyTrait;
26+
2427
/**
2528
* @var string
2629
*/

Tests/Unit/Search/Converter/ConverterManagerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
use Massive\Bundle\SearchBundle\Search\Document;
1818
use PHPUnit\Framework\TestCase;
1919
use Prophecy\Argument;
20+
use Prophecy\PhpUnit\ProphecyTrait;
2021

2122
class ConverterManagerTest extends TestCase
2223
{
24+
use ProphecyTrait;
25+
2326
public function testConvert()
2427
{
2528
$document = $this->prophesize(Document::class);

Tests/Unit/Search/Decorator/IndexNameDecoratorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
use Massive\Bundle\SearchBundle\Search\Metadata\FieldEvaluator;
1717
use Massive\Bundle\SearchBundle\Search\Metadata\IndexMetadataInterface;
1818
use PHPUnit\Framework\TestCase;
19+
use Prophecy\PhpUnit\ProphecyTrait;
1920

2021
class IndexNameDecoratorTest extends TestCase
2122
{
23+
use ProphecyTrait;
24+
2225
/**
2326
* @var FieldEvaluator
2427
*/

0 commit comments

Comments
 (0)