diff --git a/system/Model.php b/system/Model.php index 25b2b354b990..e53c0aae4bd1 100644 --- a/system/Model.php +++ b/system/Model.php @@ -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) diff --git a/tests/system/Models/WhenWhenNotModelTest.php b/tests/system/Models/WhenWhenNotModelTest.php new file mode 100644 index 000000000000..9f19b9c52d41 --- /dev/null +++ b/tests/system/Models/WhenWhenNotModelTest.php @@ -0,0 +1,142 @@ + + * + * 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); + } +}