Skip to content

Commit 94b1730

Browse files
add null check for FieldEvaluator::getPropertyValue
1 parent ff511ce commit 94b1730

6 files changed

Lines changed: 156 additions & 23 deletions

File tree

.github/workflows/test-application.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
elasticsearch-package-constraint: '^2.1'
2828
minimum-stability: 'stable'
2929
dependency-versions: 'lowest'
30-
tools: 'composer:v1'
30+
tools: 'composer:v2'
3131
php-cs-fixer: false
3232
max-phpunit-version: '7'
3333

Search/Metadata/FieldEvaluator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public function evaluateCondition($object, string $condition)
9393
*/
9494
private function getPropertyValue($object, Property $field)
9595
{
96+
if (null === $object) {
97+
return null;
98+
}
99+
96100
return $this->accessor->getValue($object, $field->getProperty());
97101
}
98102

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-
protected function setUp()
25+
protected function setUp(): void
2626
{
2727
AppKernel::resetEnvironment();
2828
AppKernel::installDistEnvironment();
2929
}
3030

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

Tests/ProphecyTrait.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

Tests/Unit/Search/Metadata/FieldEvaluatorTest.php

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class FieldEvaluatorTest extends TestCase
3737
*/
3838
private $expressionLanguage;
3939

40-
protected function setUp()
40+
protected function setUp(): void
4141
{
4242
parent::setUp();
4343
$this->expressionLanguage = $this->prophesize('Symfony\Component\ExpressionLanguage\ExpressionLanguage');
@@ -225,4 +225,148 @@ public function testEvaluateConditionWithError()
225225

226226
$this->fieldEvaluator->evaluateCondition(['test' => true], 'invalid_syntax');
227227
}
228+
229+
/**
230+
* Test getPropertyValue method specifically
231+
*/
232+
public function testGetPropertyValueWithNullObject()
233+
{
234+
$field = new Property('someProperty');
235+
$result = $this->fieldEvaluator->getValue(null, $field);
236+
237+
$this->assertNull($result);
238+
}
239+
240+
public function testGetPropertyValueWithValidObject()
241+
{
242+
$product = new Product();
243+
$product->title = 'Test Product';
244+
245+
$field = new Property('title');
246+
$result = $this->fieldEvaluator->getValue($product, $field);
247+
248+
$this->assertSame('Test Product', $result);
249+
}
250+
251+
public function testGetPropertyValueWithArrayAccess()
252+
{
253+
$data = ['name' => 'Test Name', 'value' => 123];
254+
255+
$field = new Property('[name]');
256+
$result = $this->fieldEvaluator->getValue($data, $field);
257+
258+
$this->assertSame('Test Name', $result);
259+
}
260+
261+
public function testGetPropertyValueWithNestedProperty()
262+
{
263+
$product = new Product();
264+
$category = new \stdClass();
265+
$category->name = 'Electronics';
266+
$product->category = $category;
267+
268+
$field = new Property('category.name');
269+
$result = $this->fieldEvaluator->getValue($product, $field);
270+
271+
$this->assertSame('Electronics', $result);
272+
}
273+
274+
public function testGetPropertyValueWithNullProperty()
275+
{
276+
$product = new Product();
277+
$product->description = null;
278+
279+
$field = new Property('description');
280+
$result = $this->fieldEvaluator->getValue($product, $field);
281+
282+
$this->assertNull($result);
283+
}
284+
285+
public function testGetPropertyValueWithBooleanProperty()
286+
{
287+
$product = new Product();
288+
$product->isActive = false;
289+
290+
$field = new Property('isActive');
291+
$result = $this->fieldEvaluator->getValue($product, $field);
292+
293+
$this->assertFalse($result);
294+
}
295+
296+
public function testGetPropertyValueWithIntegerProperty()
297+
{
298+
$product = new Product();
299+
$product->quantity = 0;
300+
301+
$field = new Property('quantity');
302+
$result = $this->fieldEvaluator->getValue($product, $field);
303+
304+
$this->assertSame(0, $result);
305+
}
306+
307+
public function testGetPropertyValueWithFloatProperty()
308+
{
309+
$product = new Product();
310+
$product->price = 19.99;
311+
312+
$field = new Property('price');
313+
$result = $this->fieldEvaluator->getValue($product, $field);
314+
315+
$this->assertSame(19.99, $result);
316+
}
317+
318+
public function testGetPropertyValueWithArrayProperty()
319+
{
320+
$product = new Product();
321+
$product->tags = ['tag1', 'tag2', 'tag3'];
322+
323+
$field = new Property('tags');
324+
$result = $this->fieldEvaluator->getValue($product, $field);
325+
326+
$this->assertSame(['tag1', 'tag2', 'tag3'], $result);
327+
}
328+
329+
public function testGetPropertyValueWithEmptyArrayProperty()
330+
{
331+
$product = new Product();
332+
$product->tags = [];
333+
334+
$field = new Property('tags');
335+
$result = $this->fieldEvaluator->getValue($product, $field);
336+
337+
$this->assertSame([], $result);
338+
}
339+
340+
public function testGetPropertyValueWithEmptyStringProperty()
341+
{
342+
$product = new Product();
343+
$product->title = '';
344+
345+
$field = new Property('title');
346+
$result = $this->fieldEvaluator->getValue($product, $field);
347+
348+
$this->assertSame('', $result);
349+
}
350+
351+
public function testGetPropertyValueWithMethodAccess()
352+
{
353+
$product = new Product();
354+
$product->setTitle('Method Value');
355+
356+
$field = new Property('title');
357+
$result = $this->fieldEvaluator->getValue($product, $field);
358+
359+
$this->assertSame('Method Value', $result);
360+
}
361+
362+
public function testGetPropertyValueWithGetterAccess()
363+
{
364+
$product = new Product();
365+
$product->setBody('Body Content');
366+
367+
$field = new Property('body');
368+
$result = $this->fieldEvaluator->getValue($product, $field);
369+
370+
$this->assertSame('Body Content', $result);
371+
}
228372
}

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@
3232
"symfony/security-bundle": "^4.3 || ^5.0 || ^6.0 || ^7.0",
3333
"symfony/twig-bundle": "^4.3 || ^5.0 || ^6.0 || ^7.0",
3434
"symfony/validator": "^4.3 || ^5.0 || ^6.0 || ^7.0",
35-
"symfony/phpunit-bridge": "^5.0.4 || ^6.0 || ^7.0",
36-
"matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0",
35+
"symfony/phpunit-bridge": "^5.0.4 || ^6.0 || ^7.0 ",
36+
"matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0 || 6.0",
3737
"doctrine/doctrine-bundle": "^1.10 || ^2.0",
3838
"doctrine/orm": "^2.5",
3939

4040
"behat/behat": "^3.4.2",
4141

4242
"webmozart/assert": "^1.7",
43-
"doctrine/annotations": "^1.14"
43+
"doctrine/annotations": "^1.14",
44+
"phpspec/prophecy-phpunit": "^2.4"
4445
},
4546
"suggest": {
4647
"sensio/distribution-bundle": "Required if the SearchScriptHandler is used",

0 commit comments

Comments
 (0)