4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Action
;
22 use api\v
4\UnitTestCase
;
23 use Civi\Api4\MockArrayEntity
;
28 class GetFromArrayTest
extends UnitTestCase
{
30 public function testArrayGetWithLimit() {
31 $result = MockArrayEntity
::get()
35 $this->assertEquals(3, $result[0]['field1']);
36 $this->assertEquals(4, $result[1]['field1']);
38 // The object's count() method will account for all results, ignoring limit, while the array results are limited
39 $this->assertCount(2, (array) $result);
40 $this->assertCount(5, $result);
43 public function testArrayGetWithSort() {
44 $result = MockArrayEntity
::get()
45 ->addOrderBy('field1', 'DESC')
47 $this->assertEquals([5, 4, 3, 2, 1], array_column((array) $result, 'field1'));
49 $result = MockArrayEntity
::get()
50 ->addOrderBy('field5', 'DESC')
51 ->addOrderBy('field2', 'ASC')
53 $this->assertEquals([3, 2, 5, 4, 1], array_column((array) $result, 'field1'));
55 $result = MockArrayEntity
::get()
56 ->addOrderBy('field3', 'ASC')
57 ->addOrderBy('field2', 'ASC')
59 $this->assertEquals([3, 1, 2, 5, 4], array_column((array) $result, 'field1'));
62 public function testArrayGetWithSelect() {
63 $result = MockArrayEntity
::get()
88 public function testArrayGetWithWhere() {
89 $result = MockArrayEntity
::get()
90 ->addWhere('field2', '=', 'yack')
92 $this->assertEquals([2], array_column((array) $result, 'field1'));
94 $result = MockArrayEntity
::get()
95 ->addWhere('field5', '!=', 'banana')
96 ->addWhere('field3', 'IS NOT NULL')
98 $this->assertEquals([4, 5], array_column((array) $result, 'field1'));
100 $result = MockArrayEntity
::get()
101 ->addWhere('field1', '>=', '4')
103 $this->assertEquals([4, 5], array_column((array) $result, 'field1'));
105 $result = MockArrayEntity
::get()
106 ->addWhere('field1', '<', '2')
108 $this->assertEquals([1], array_column((array) $result, 'field1'));
110 $result = MockArrayEntity
::get()
111 ->addWhere('field2', 'LIKE', '%ra%')
113 $this->assertEquals([1, 3], array_column((array) $result, 'field1'));
115 $result = MockArrayEntity
::get()
116 ->addWhere('field2', 'REGEXP', '(zebra|yac[a-z]|something/else)')
118 $this->assertEquals([1, 2], array_column((array) $result, 'field1'));
120 $result = MockArrayEntity
::get()
121 ->addWhere('field2', 'NOT REGEXP', '^[x|y|z]')
123 $this->assertEquals([4, 5], array_column((array) $result, 'field1'));
125 $result = MockArrayEntity
::get()
126 ->addWhere('field3', 'IS NULL')
128 $this->assertEquals([1, 3], array_column((array) $result, 'field1'));
130 $result = MockArrayEntity
::get()
131 ->addWhere('field3', '=', '0')
133 $this->assertEquals([2], array_column((array) $result, 'field1'));
135 $result = MockArrayEntity
::get()
136 ->addWhere('field2', 'LIKE', '%ra')
138 $this->assertEquals([1], array_column((array) $result, 'field1'));
140 $result = MockArrayEntity
::get()
141 ->addWhere('field2', 'LIKE', 'ra')
143 $this->assertEquals(0, count($result));
145 $result = MockArrayEntity
::get()
146 ->addWhere('field2', 'NOT LIKE', '%ra%')
148 $this->assertEquals([2, 4, 5], array_column((array) $result, 'field1'));
150 $result = MockArrayEntity
::get()
151 ->addWhere('field6', '=', '0')
153 $this->assertEquals([3, 4, 5], array_column((array) $result, 'field1'));
155 $result = MockArrayEntity
::get()
156 ->addWhere('field6', '=', 0)
158 $this->assertEquals([3, 4, 5], array_column((array) $result, 'field1'));
160 $result = MockArrayEntity
::get()
161 ->addWhere('field1', 'BETWEEN', [3, 5])
163 $this->assertEquals([3, 4, 5], array_column((array) $result, 'field1'));
165 $result = MockArrayEntity
::get()
166 ->addWhere('field1', 'NOT BETWEEN', [3, 4])
168 $this->assertEquals([1, 2, 5], array_column((array) $result, 'field1'));
171 public function testArrayGetWithNestedWhereClauses() {
172 $result = MockArrayEntity
::get()
173 ->addClause('OR', ['field2', 'LIKE', '%ra'], ['field2', 'LIKE', 'x ray'])
175 $this->assertEquals([1, 3], array_column((array) $result, 'field1'));
177 $result = MockArrayEntity
::get()
178 ->addClause('OR', ['field2', '=', 'zebra'], ['field2', '=', 'yack'])
179 ->addClause('OR', ['field5', '!=', 'apple'], ['field3', 'IS NULL'])
181 $this->assertEquals([1, 2], array_column((array) $result, 'field1'));
183 $result = MockArrayEntity
::get()
184 ->addClause('NOT', ['field2', '!=', 'yack'])
186 $this->assertEquals([2], array_column((array) $result, 'field1'));
188 $result = MockArrayEntity
::get()
189 ->addClause('OR', ['field1', '=', 2], ['AND', [['field5', '=', 'apple'], ['field3', '=', 1]]])
191 $this->assertEquals([2, 4, 5], array_column((array) $result, 'field1'));