Skip to content

Commit

Permalink
Add tests for nullish query.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Feb 26, 2024
1 parent c03e0e8 commit 6525428
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
26 changes: 25 additions & 1 deletion tests/TestApp/Model/Table/FinderArticlesTable.php
Expand Up @@ -47,7 +47,19 @@ public function findSlugged(SelectQuery $query, string $slug): SelectQuery
}

/**
* Requires slug key to be present in $options array.
* Requires nullable slug key to be present in $options array.
*
* @param \Cake\ORM\Query\SelectQuery $query
* @param string|null $slug
* @return \Cake\ORM\Query\SelectQuery
*/
public function findSluggedNullable(SelectQuery $query, ?string $slug): SelectQuery
{
return $query->where(['title IS' => $slug]);
}

/**
* Requires uid key to be present in $options array.
*
* @param \Cake\ORM\Query\SelectQuery $query
* @param int $uid
Expand All @@ -57,4 +69,16 @@ public function findUser(SelectQuery $query, int $uid): SelectQuery
{
return $query->where(['user_id' => $uid]);
}

/**
* Requires nullable uid key to be present in $options array.
*
* @param \Cake\ORM\Query\SelectQuery $query
* @param int|null $uid
* @return \Cake\ORM\Query\SelectQuery
*/
public function findUserNullable(SelectQuery $query, ?int $uid): SelectQuery
{
return $query->where(['user_id IS' => $uid]);
}
}
77 changes: 77 additions & 0 deletions tests/TestCase/Model/Filter/FinderTest.php
Expand Up @@ -191,6 +191,83 @@ public function testProcessCastCallbackNull()
);
}

/**
* Tests that a custom finder that requires certain values to be cast, using
* a custom callable and null input. In this case, the callable should return
* null if already null or empty string.
*
* @return void
*/
public function testProcessCastCallbackNullableString()
{
$articles = $this->getTableLocator()->get('FinderArticles', [
'className' => '\Search\Test\TestApp\Model\Table\FinderArticlesTable',
]);
$manager = new Manager($articles);
$options = [
'cast' => [
'slug' => function ($value) {
if ($value === null || $value === '') {
return null;
}

return (string)$value;
},
],
];
$filter = new Finder('sluggedNullable', $manager, $options);
$filter->setArgs(['slug' => null]);
$filter->setQuery($articles->find());
$filter->process();

$this->assertMatchesRegularExpression(
'/WHERE \(title\) IS NULL$/',
$filter->getQuery()->sql()
);
$this->assertSame(
[],
Hash::extract($filter->getQuery()->getValueBinder()->bindings(), '{s}.value')
);
}

/**
* Tests that a custom finder that requires certain values to be cast, using
* a custom callable and null input. In this case, the callable should return
* null if already null or empty string.
*
* @return void
*/
public function testProcessCastCallbackNullableInt()
{
$articles = $this->getTableLocator()->get('FinderArticles', [
'className' => '\Search\Test\TestApp\Model\Table\FinderArticlesTable',
]);
$manager = new Manager($articles);
$options = [
'cast' => [
'uid' => function ($value) {
if ($value === null || $value === '') {
return null;
}

return (int)$value;
},
],
];
$filter = new Finder('userNullable', $manager, $options);
$filter->setArgs(['uid' => null]);
$filter->setQuery($articles->find());
$filter->process();
$this->assertMatchesRegularExpression(
'/WHERE \(user_id\) IS NULL$/',
$filter->getQuery()->sql()
);
$this->assertSame(
[],
Hash::extract($filter->getQuery()->getValueBinder()->bindings(), '{s}.value')
);
}

/**
* @return void
*/
Expand Down

0 comments on commit 6525428

Please sign in to comment.