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()
87 public function testArrayGetWithWhere() {
88 $result = MockArrayEntity
::get()
89 ->addWhere('field2', '=', 'yack')
91 $this->assertEquals([2], array_column((array) $result, 'field1'));
93 $result = MockArrayEntity
::get()
94 ->addWhere('field5', '!=', 'banana')
95 ->addWhere('field3', 'IS NOT NULL')
97 $this->assertEquals([4, 5], array_column((array) $result, 'field1'));
99 $result = MockArrayEntity
::get()
100 ->addWhere('field1', '>=', '4')
102 $this->assertEquals([4, 5], array_column((array) $result, 'field1'));
104 $result = MockArrayEntity
::get()
105 ->addWhere('field1', '<', '2')
107 $this->assertEquals([1], array_column((array) $result, 'field1'));
109 $result = MockArrayEntity
::get()
110 ->addWhere('field2', 'LIKE', '%ra%')
112 $this->assertEquals([1, 3], array_column((array) $result, 'field1'));
114 $result = MockArrayEntity
::get()
115 ->addWhere('field3', 'IS NULL')
117 $this->assertEquals([1, 3], array_column((array) $result, 'field1'));
119 $result = MockArrayEntity
::get()
120 ->addWhere('field3', '=', '0')
122 $this->assertEquals([2], array_column((array) $result, 'field1'));
124 $result = MockArrayEntity
::get()
125 ->addWhere('field2', 'LIKE', '%ra')
127 $this->assertEquals([1], array_column((array) $result, 'field1'));
129 $result = MockArrayEntity
::get()
130 ->addWhere('field2', 'LIKE', 'ra')
132 $this->assertEquals(0, count($result));
134 $result = MockArrayEntity
::get()
135 ->addWhere('field2', 'NOT LIKE', '%ra%')
137 $this->assertEquals([2, 4, 5], array_column((array) $result, 'field1'));
139 $result = MockArrayEntity
::get()
140 ->addWhere('field6', '=', '0')
142 $this->assertEquals([3, 4, 5], array_column((array) $result, 'field1'));
144 $result = MockArrayEntity
::get()
145 ->addWhere('field6', '=', 0)
147 $this->assertEquals([3, 4, 5], array_column((array) $result, 'field1'));
149 $result = MockArrayEntity
::get()
150 ->addWhere('field1', 'BETWEEN', [3, 5])
152 $this->assertEquals([3, 4, 5], array_column((array) $result, 'field1'));
154 $result = MockArrayEntity
::get()
155 ->addWhere('field1', 'NOT BETWEEN', [3, 4])
157 $this->assertEquals([1, 2, 5], array_column((array) $result, 'field1'));
160 public function testArrayGetWithNestedWhereClauses() {
161 $result = MockArrayEntity
::get()
162 ->addClause('OR', ['field2', 'LIKE', '%ra'], ['field2', 'LIKE', 'x ray'])
164 $this->assertEquals([1, 3], array_column((array) $result, 'field1'));
166 $result = MockArrayEntity
::get()
167 ->addClause('OR', ['field2', '=', 'zebra'], ['field2', '=', 'yack'])
168 ->addClause('OR', ['field5', '!=', 'apple'], ['field3', 'IS NULL'])
170 $this->assertEquals([1, 2], array_column((array) $result, 'field1'));
172 $result = MockArrayEntity
::get()
173 ->addClause('NOT', ['field2', '!=', 'yack'])
175 $this->assertEquals([2], array_column((array) $result, 'field1'));
177 $result = MockArrayEntity
::get()
178 ->addClause('OR', ['field1', '=', 2], ['AND', [['field5', '=', 'apple'], ['field3', '=', 1]]])
180 $this->assertEquals([2, 4, 5], array_column((array) $result, 'field1'));