|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Models; |
| 13 | + |
| 14 | +use Tests\Support\Models\SecondaryModel; |
| 15 | + |
| 16 | +/** |
| 17 | + * @group DatabaseLive |
| 18 | + * |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +final class WhenWhenNotModelTest extends LiveModelTestCase |
| 22 | +{ |
| 23 | + public function testWhenWithTrueCondition(): void |
| 24 | + { |
| 25 | + $secondaryData = [ |
| 26 | + [ |
| 27 | + 'key' => 'foo', |
| 28 | + 'value' => 'foobar', |
| 29 | + ], |
| 30 | + [ |
| 31 | + 'key' => 'bar', |
| 32 | + 'value' => 'foobar', |
| 33 | + ], |
| 34 | + [ |
| 35 | + 'key' => 'baz', |
| 36 | + 'value' => 'foobaz', |
| 37 | + ], |
| 38 | + ]; |
| 39 | + $filter = 'foobar'; |
| 40 | + |
| 41 | + $this->createModel(SecondaryModel::class)->insertBatch($secondaryData); |
| 42 | + |
| 43 | + $result = $this->model->when($filter, static function ($query, $filter) { |
| 44 | + $query->where('value', $filter); |
| 45 | + })->find(); |
| 46 | + |
| 47 | + $this->assertCount(2, $result); |
| 48 | + $this->assertSame('foo', $result[0]->key); |
| 49 | + $this->assertSame('bar', $result[1]->key); |
| 50 | + } |
| 51 | + |
| 52 | + public function testWhenWithFalseConditionAndDefaultCallback(): void |
| 53 | + { |
| 54 | + $secondaryData = [ |
| 55 | + [ |
| 56 | + 'key' => 'foo', |
| 57 | + 'value' => 'foobar', |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'key' => 'bar', |
| 61 | + 'value' => 'foobar', |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'key' => 'baz', |
| 65 | + 'value' => 'foobaz', |
| 66 | + ], |
| 67 | + ]; |
| 68 | + $filter = ''; |
| 69 | + |
| 70 | + $this->createModel(SecondaryModel::class)->insertBatch($secondaryData); |
| 71 | + |
| 72 | + $result = $this->model->when($filter, static function ($query, $filter) { |
| 73 | + $query->where('value', $filter); |
| 74 | + }, static function ($query) { |
| 75 | + $query->where('value', 'foobar'); |
| 76 | + })->find(); |
| 77 | + |
| 78 | + $this->assertCount(2, $result); |
| 79 | + $this->assertSame('foo', $result[0]->key); |
| 80 | + $this->assertSame('bar', $result[1]->key); |
| 81 | + } |
| 82 | + |
| 83 | + public function testWhenNotWithFalseCondition(): void |
| 84 | + { |
| 85 | + $secondaryData = [ |
| 86 | + [ |
| 87 | + 'key' => 'foo', |
| 88 | + 'value' => 'foobar', |
| 89 | + ], |
| 90 | + [ |
| 91 | + 'key' => 'bar', |
| 92 | + 'value' => 'foobar', |
| 93 | + ], |
| 94 | + [ |
| 95 | + 'key' => 'baz', |
| 96 | + 'value' => 'foobaz', |
| 97 | + ], |
| 98 | + ]; |
| 99 | + $filter = ''; |
| 100 | + |
| 101 | + $this->createModel(SecondaryModel::class)->insertBatch($secondaryData); |
| 102 | + |
| 103 | + $result = $this->model->whenNot($filter, static function ($query, $filter) { |
| 104 | + $query->where('value !=', 'foobar'); |
| 105 | + })->find(); |
| 106 | + |
| 107 | + $this->assertCount(1, $result); |
| 108 | + $this->assertSame('baz', $result[0]->key); |
| 109 | + $this->assertSame('foobaz', $result[0]->value); |
| 110 | + } |
| 111 | + |
| 112 | + public function testWhenNotWithTrueConditionAndDefaultCallback(): void |
| 113 | + { |
| 114 | + $secondaryData = [ |
| 115 | + [ |
| 116 | + 'key' => 'foo', |
| 117 | + 'value' => 'foobar', |
| 118 | + ], |
| 119 | + [ |
| 120 | + 'key' => 'bar', |
| 121 | + 'value' => 'foobar', |
| 122 | + ], |
| 123 | + [ |
| 124 | + 'key' => 'baz', |
| 125 | + 'value' => 'foobaz', |
| 126 | + ], |
| 127 | + ]; |
| 128 | + $filter = 'foobar'; |
| 129 | + |
| 130 | + $this->createModel(SecondaryModel::class)->insertBatch($secondaryData); |
| 131 | + |
| 132 | + $result = $this->model->whenNot($filter, static function ($query, $filter) { |
| 133 | + $query->where('value !=', 'foobar'); |
| 134 | + }, static function ($query) { |
| 135 | + $query->where('value', 'foobar'); |
| 136 | + })->find(); |
| 137 | + |
| 138 | + $this->assertCount(2, $result); |
| 139 | + $this->assertSame('foo', $result[0]->key); |
| 140 | + $this->assertSame('bar', $result[1]->key); |
| 141 | + } |
| 142 | +} |
0 commit comments