Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add value function to expression language to allow existence checks #173

7 changes: 7 additions & 0 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
SYMFONY_DEPRECATIONS_HELPER: weak
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ELASTICSEARCH_VERSION: ${{ matrix.elasticsearch-version }}
SYMFONY_MAX_PHPUNIT_VERSION: ${{ matrix.max-phpunit-version }}

strategy:
fail-fast: false
Expand All @@ -28,6 +29,7 @@ jobs:
dependency-versions: 'lowest'
tools: 'composer:v1'
php-cs-fixer: false
max-phpunit-version: '7'

- php-version: '7.3'
elasticsearch-version: '2.4.6'
Expand All @@ -36,6 +38,7 @@ jobs:
dependency-versions: 'highest'
tools: 'composer:v2'
php-cs-fixer: false
max-phpunit-version: '7'

- php-version: '7.4'
elasticsearch-version: '2.4.6'
Expand All @@ -44,6 +47,7 @@ jobs:
dependency-versions: 'highest'
tools: 'composer:v2'
php-cs-fixer: true
max-phpunit-version: '7'

- php-version: '8.0'
elasticsearch-version: '7.11.1'
Expand All @@ -52,6 +56,7 @@ jobs:
dependency-versions: 'highest'
tools: 'composer:v2'
php-cs-fixer: false
max-phpunit-version: '8'

- php-version: '8.1'
elasticsearch-version: '7.11.1'
Expand All @@ -60,6 +65,7 @@ jobs:
dependency-versions: 'highest'
tools: 'composer:v2'
php-cs-fixer: false
max-phpunit-version: '8'

- php-version: '8.1'
elasticsearch-version: '7.11.1'
Expand All @@ -68,6 +74,7 @@ jobs:
dependency-versions: 'highest'
tools: 'composer:v2'
php-cs-fixer: false
max-phpunit-version: '8'

services:
elasticsearch:
Expand Down
27 changes: 27 additions & 0 deletions Search/ExpressionLanguage/MassiveSearchExpressionLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function registerFunctions()

$this->addFunction($this->createJoinFunction());
$this->addFunction($this->createMapFunction());
$this->addFunction($this->createValueFunction());
}

/**
Expand Down Expand Up @@ -82,4 +83,30 @@ function(array $values, $elements, $expression) {
}
);
}

/**
* Value returns the value of the variable. If the variable does not exists a default will be returned.
*
* For example:
*
* massive_search_value("expression", {"hidden": false}) = array('hidden' => true);
*
* @return ExpressionFunction
*/
private function createValueFunction()
{
return new ExpressionFunction(
'massive_search_value',
function($elements, $expression) {
throw new \Exception('Value function does not support compilation');
},
function(array $values, $variable, $default) {
if (isset($values[$variable])) {
return $values[$variable];
}

return $default;
}
);
}
}
2 changes: 1 addition & 1 deletion Search/ObjectToDocumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function populateDocument(
$type = $mapping['type'];
/** @var FieldInterface $mappingField */
$mappingField = $mapping['field'];
$condition = method_exists($mappingField, 'getCondition') ? $mappingField->getCondition() : null;
$condition = \method_exists($mappingField, 'getCondition') ? $mappingField->getCondition() : null;
$validField = $condition ? $this->fieldEvaluator->evaluateCondition($object, $condition) : true;

if (false === $validField) {
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ abstract class BaseTestCase extends TestCase
{
private $kernels = [];

public function setUp()
protected function setUp()
{
AppKernel::resetEnvironment();
AppKernel::installDistEnvironment();
}

public function tearDown()
protected function tearDown()
{
AppKernel::resetEnvironment();
}
Expand Down
16 changes: 16 additions & 0 deletions Tests/ProphecyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the MassiveSearchBundle
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Prophecy\PhpUnit;

trait ProphecyTrait
{
}
3 changes: 3 additions & 0 deletions Tests/Unit/Command/PurgeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

class PurgeCommandTest extends TestCase
{
use ProphecyTrait;

/**
* @var SearchManagerInterface
*/
Expand Down
6 changes: 5 additions & 1 deletion Tests/Unit/Command/ReindexCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use Massive\Bundle\SearchBundle\Search\Reindex\ReindexProviderRegistry;
use Massive\Bundle\SearchBundle\Search\Reindex\ResumeManagerInterface;
use Massive\Bundle\SearchBundle\Search\SearchManager;
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -28,6 +30,8 @@

class ReindexCommandTest extends TestCase
{
use ProphecyTrait;

/**
* @var ResumeManagerInterface
*/
Expand Down Expand Up @@ -95,7 +99,7 @@ public function testCheckpointAsk()
Argument::type(OutputInterface::class),
Argument::type(ConfirmationQuestion::class),
true
)->shouldBeCalled();
)->shouldBeCalled()->willReturn(true);
$this->execute('prod', []);
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/Adapter/ZendLuceneAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
use Massive\Bundle\SearchBundle\Search\Field;
use Massive\Bundle\SearchBundle\Search\SearchQuery;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Filesystem\Filesystem;

class ZendLuceneAdapterTest extends TestCase
{
use ProphecyTrait;

/**
* @var string
*/
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/Converter/ConverterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
use Massive\Bundle\SearchBundle\Search\Document;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class ConverterManagerTest extends TestCase
{
use ProphecyTrait;

public function testConvert()
{
$document = $this->prophesize(Document::class);
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/Decorator/IndexNameDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use Massive\Bundle\SearchBundle\Search\Metadata\FieldEvaluator;
use Massive\Bundle\SearchBundle\Search\Metadata\IndexMetadataInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class IndexNameDecoratorTest extends TestCase
{
use ProphecyTrait;

/**
* @var FieldEvaluator
*/
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/Decorator/LocalizationDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
use Massive\Bundle\SearchBundle\Search\Metadata\IndexMetadataInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class LocalizationDecoratorTest extends TestCase
{
use ProphecyTrait;

/**
* @var IndexNameDecorator
*/
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/Decorator/PrefixDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
use Massive\Bundle\SearchBundle\Search\Metadata\IndexMetadata;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class PrefixDecoratorTest extends TestCase
{
use ProphecyTrait;

/**
* @var IndexNameDecoratorInterface
*/
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
use Massive\Bundle\SearchBundle\Search\Document;
use Massive\Bundle\SearchBundle\Search\Field;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class DocumentTest extends TestCase
{
use ProphecyTrait;

/**
* @var Field
*/
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/EventListener/DeindexListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
use Massive\Bundle\SearchBundle\Search\EventListener\DeindexListener;
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use stdClass;

class DeindexListenerTest extends TestCase
{
use ProphecyTrait;

public function testOnDeindex()
{
$entity = new stdClass();
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/EventListener/IndexListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
use Massive\Bundle\SearchBundle\Search\EventListener\IndexListener;
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use stdClass;

class IndexListenerTest extends TestCase
{
use ProphecyTrait;

public function testOnIndex()
{
$entity = new stdClass();
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/EventListener/ZendRebuildSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
use Massive\Bundle\SearchBundle\Search\Event\IndexRebuildEvent;
use Massive\Bundle\SearchBundle\Search\EventListener\ZendRebuildSubscriber;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Console\Output\OutputInterface;

class ZendRebuildSubscriberTest extends TestCase
{
use ProphecyTrait;

/**
* @var ZendLuceneAdapter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
use Massive\Bundle\SearchBundle\Search\SearchEvents;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use stdClass;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class DoctrineOrmSubscriberTest extends TestCase
{
use ProphecyTrait;

public function testMapping()
{
$eventArgs = $this->prophesize(LifecycleEventArgs::class);
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/Search/EventSubscriber/PurgeSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
use Massive\Bundle\SearchBundle\Search\EventSubscriber\PurgeSubscriber;
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class PurgeSubscriberTest extends TestCase
{
use ProphecyTrait;

/**
* @var SearchManagerInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* with this source code in the file LICENSE.
*/

namespace Massive\Bundle\SearchBundle\Unit\Search\EventSubscriber;
namespace Massive\Bundle\SearchBundle\Unit\Search\ExpressionLanguage;

use Massive\Bundle\SearchBundle\Search\ExpressionLanguage\MassiveSearchExpressionLanguage;
use PHPUnit\Framework\TestCase;
Expand All @@ -36,15 +36,40 @@ public function provideExpression()
'map([{"foo": "one"}, {"foo":"two"}, {"foo": "three"}], "el[\'foo\']")',
['one', 'two', 'three'],
],
[
'massive_search_value("three", null)',
null,
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("three", "default")',
'default',
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("three", null)',
'Z',
['one' => 'X', 'two' => 'Y', 'three' => 'Z'],
],
[
'massive_search_value("three", {"test": true})["test"]',
true,
['one' => 'X', 'two' => 'Y'],
],
[
'massive_search_value("three", {"test": true})["test"]',
false,
['one' => 'X', 'two' => 'Y', 'three' => ['test' => false]],
],
];
}

/**
* @dataProvider provideExpression
*/
public function testExpression($expression, $expectedResult)
public function testExpression($expression, $expectedResult, $values = [])
{
$result = $this->expressionLanguage->evaluate($expression);
$result = $this->expressionLanguage->evaluate($expression, $values);
$this->assertEquals($expectedResult, $result);
}
}
3 changes: 3 additions & 0 deletions Tests/Unit/Search/Metadata/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
use Massive\Bundle\SearchBundle\Search\Metadata\ClassMetadata;
use Massive\Bundle\SearchBundle\Search\Metadata\IndexMetadata;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class ClassMetadataTest extends TestCase
{
use ProphecyTrait;

/**
* @var IndexMetadata
*/
Expand Down
Loading
Loading