Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
* @method $this selectMax(string $select = '', string $alias = '')
* @method $this selectMin(string $select = '', string $alias = '')
* @method $this selectSum(string $select = '', string $alias = '')
* @method $this when($condition, callable $callback, ?callable $defaultCallback = null)
* @method $this whenNot($condition, callable $callback, ?callable $defaultCallback = null)
* @method $this where($key, $value = null, ?bool $escape = null)
* @method $this whereIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this whereNotIn(?string $key = null, $values = null, ?bool $escape = null)
Expand Down
142 changes: 142 additions & 0 deletions tests/system/Models/WhenWhenNotModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Models;

use Tests\Support\Models\SecondaryModel;

/**
* @group DatabaseLive
*
* @internal
*/
final class WhenWhenNotModelTest extends LiveModelTestCase
{
public function testWhenWithTrueCondition(): void
{
$secondaryData = [
[
'key' => 'foo',
'value' => 'foobar',
],
[
'key' => 'bar',
'value' => 'foobar',
],
[
'key' => 'baz',
'value' => 'foobaz',
],
];
$filter = 'foobar';

$this->createModel(SecondaryModel::class)->insertBatch($secondaryData);

$result = $this->model->when($filter, static function ($query, $filter) {
$query->where('value', $filter);
})->find();

$this->assertCount(2, $result);
$this->assertSame('foo', $result[0]->key);
$this->assertSame('bar', $result[1]->key);
}

public function testWhenWithFalseConditionAndDefaultCallback(): void
{
$secondaryData = [
[
'key' => 'foo',
'value' => 'foobar',
],
[
'key' => 'bar',
'value' => 'foobar',
],
[
'key' => 'baz',
'value' => 'foobaz',
],
];
$filter = '';

$this->createModel(SecondaryModel::class)->insertBatch($secondaryData);

$result = $this->model->when($filter, static function ($query, $filter) {
$query->where('value', $filter);
}, static function ($query) {
$query->where('value', 'foobar');
})->find();

$this->assertCount(2, $result);
$this->assertSame('foo', $result[0]->key);
$this->assertSame('bar', $result[1]->key);
}

public function testWhenNotWithFalseCondition(): void
{
$secondaryData = [
[
'key' => 'foo',
'value' => 'foobar',
],
[
'key' => 'bar',
'value' => 'foobar',
],
[
'key' => 'baz',
'value' => 'foobaz',
],
];
$filter = '';

$this->createModel(SecondaryModel::class)->insertBatch($secondaryData);

$result = $this->model->whenNot($filter, static function ($query, $filter) {
$query->where('value !=', 'foobar');
})->find();

$this->assertCount(1, $result);
$this->assertSame('baz', $result[0]->key);
$this->assertSame('foobaz', $result[0]->value);
}

public function testWhenNotWithTrueConditionAndDefaultCallback(): void
{
$secondaryData = [
[
'key' => 'foo',
'value' => 'foobar',
],
[
'key' => 'bar',
'value' => 'foobar',
],
[
'key' => 'baz',
'value' => 'foobaz',
],
];
$filter = 'foobar';

$this->createModel(SecondaryModel::class)->insertBatch($secondaryData);

$result = $this->model->whenNot($filter, static function ($query, $filter) {
$query->where('value !=', 'foobar');
}, static function ($query) {
$query->where('value', 'foobar');
})->find();

$this->assertCount(2, $result);
$this->assertSame('foo', $result[0]->key);
$this->assertSame('bar', $result[1]->key);
}
}