Skip to content

Commit

Permalink
Update as per feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Feb 26, 2024
1 parent 6525428 commit a54e3b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
3 changes: 3 additions & 0 deletions src/Model/Filter/Finder.php
Expand Up @@ -41,6 +41,9 @@ public function process(): bool
$casts = $this->getConfig('cast');
foreach ($casts as $field => $toType) {
$value = $args[$field] ?? null;
if ($value === null) {
continue;
}

if (is_callable($toType)) {
$value = $toType($value);
Expand Down
46 changes: 7 additions & 39 deletions tests/TestCase/Model/Filter/FinderTest.php
Expand Up @@ -93,33 +93,6 @@ public function testProcessCast()
);
}

/**
* Tests that a custom finder that requires certain values to be cast, from null
* to int, float or bool.
*
* @return void
*/
public function testProcessCastNull()
{
$articles = $this->getTableLocator()->get('FinderArticles', [
'className' => '\Search\Test\TestApp\Model\Table\FinderArticlesTable',
]);
$manager = new Manager($articles);
$filter = new Finder('user', $manager, ['cast' => ['uid' => 'int']]);
$filter->setArgs(['uid' => null]);
$filter->setQuery($articles->find());
$filter->process();

$this->assertMatchesRegularExpression(
'/WHERE user_id = :c0$/',
$filter->getQuery()->sql()
);
$this->assertSame(
[0],
Hash::extract($filter->getQuery()->getValueBinder()->bindings(), '{s}.value')
);
}

/**
* Tests that a custom finder that requires certain values to be cast, using
* a custom callable.
Expand Down Expand Up @@ -158,8 +131,7 @@ public function testProcessCastCallback()
}

/**
* Tests that a custom finder that requires certain values to be cast, using
* a custom callable and null input.
* Tests that a null input does not use casting.
*
* @return void
*/
Expand All @@ -176,17 +148,13 @@ public function testProcessCastCallbackNull()
},
],
];
$filter = new Finder('slugged', $manager, $options);
$filter = new Finder('sluggedNullable', $manager, $options);
$filter->setArgs(['slug' => null]);
$filter->setQuery($articles->find());
$filter->process();

$this->assertMatchesRegularExpression(
'/WHERE title = :c0$/',
$filter->getQuery()->sql()
);
$this->assertSame(
[''],
[],
Hash::extract($filter->getQuery()->getValueBinder()->bindings(), '{s}.value')
);
}
Expand All @@ -207,7 +175,7 @@ public function testProcessCastCallbackNullableString()
$options = [
'cast' => [
'slug' => function ($value) {
if ($value === null || $value === '') {
if ($value === '') {
return null;
}

Expand All @@ -216,7 +184,7 @@ public function testProcessCastCallbackNullableString()
],
];
$filter = new Finder('sluggedNullable', $manager, $options);
$filter->setArgs(['slug' => null]);
$filter->setArgs(['slug' => '']);
$filter->setQuery($articles->find());
$filter->process();

Expand Down Expand Up @@ -246,7 +214,7 @@ public function testProcessCastCallbackNullableInt()
$options = [
'cast' => [
'uid' => function ($value) {
if ($value === null || $value === '') {
if ($value === '') {
return null;
}

Expand All @@ -255,7 +223,7 @@ public function testProcessCastCallbackNullableInt()
],
];
$filter = new Finder('userNullable', $manager, $options);
$filter->setArgs(['uid' => null]);
$filter->setArgs(['uid' => '']);
$filter->setQuery($articles->find());
$filter->process();
$this->assertMatchesRegularExpression(
Expand Down

0 comments on commit a54e3b9

Please sign in to comment.