1: <?php
2: namespace tests\integration\barebones\models\encounters;
3:
4: use tests\integration\barebones\BarebonesTestCase;
5: use tests\integration\barebones\BarebonesConstants;
6:
7: use Barebones\Lib\ApplicationDataConnectionPool;
8: use barebones\lib\CollectionModel;
9: use barebones\models\examples\ExampleModel;
10: use CodingSystemModel;
11: use ProviderModel;
12: use ICD10Model;
13: use Session;
14:
15: class CollectionTest extends BarebonesTestCase
16: {
17: public function testCollectionOptions()
18: {
19: Session::set('usr_cur_customer_id', 8);
20: $provider = ProviderModel::query()
21: ->limit(1)
22: ->fetch()
23: ->nextModel();
24: $example1 = new ExampleModel();
25: $example1->exa_string = 'A';
26: $example1->exa_phone = '2298675309';
27: $example1->exa_mod_id = $provider->phy_id;
28: $example1->save();
29: $example2 = new ExampleModel();
30: $example2->exa_string = 'B';
31: $example2->exa_phone = '2298675309';
32: $example2->save();
33: $example3 = new ExampleModel();
34: $example3->exa_string = 'B';
35: $example3->exa_phone = '2298675309';
36: $example3->save();
37:
38:
39: $collection = new CollectionModel(new ExampleModel(), ['return' => ['exa_phone']]);
40: $item = $collection->next();
41: $this->assertArrayHasKey('exa_phone', $item);
42: $this->assertTrue(!isset($item['exa_string']));
43:
44:
45: $collection = new CollectionModel(new ExampleModel(), ['total_rows' => false]);
46: $this->assertNull($collection->getTotalRows());
47:
48:
49: $collection = new CollectionModel(new ExampleModel(),
50: ['where' => [
51: "`exa_string` = 'A'",
52: "`exa_string` != 'B'"
53: ]]);
54: $item = $collection->next();
55: $this->assertEquals($item['exa_string'], 'A');
56:
57:
58: $collection = new CollectionModel(new ExampleModel(), [
59: 'group_by' => 'exa_phone',
60: 'where' => ["`exa_phone` = '2298675309'"],
61: ]);
62: $this->assertEquals($collection->size(), 1);
63:
64:
65: $collection = new CollectionModel(new ExampleModel(), [
66: 'group_by' => 'exa_string',
67: 'where' => ["`exa_string` = 'B'"],
68: 'having' => ["COUNT(`exa_id`) > 1"],
69: ]);
70: $item = $collection->next();
71: $this->assertNotNull($item);
72: $this->assertEquals($item['exa_string'], 'B');
73:
74:
75: $collection = new CollectionModel(new ExampleModel(), [
76: 'order_by' => 'exa_string',
77: 'sort_by' => 'DESC',
78: 'where' => ["`exa_id` IN (".$example1->id.", ".$example2->id.")"],
79: ]);
80: $item = $collection->next();
81: $this->assertEquals($item['exa_string'], 'B');
82: $item = $collection->next();
83: $this->assertEquals($item['exa_string'], 'A');
84:
85:
86: $collection = new CollectionModel(new ExampleModel(), [
87: 'order_by' => 'exa_string',
88: 'sort_by' => 'ASC',
89: 'where' => ["`exa_id` IN (".$example1->id.", ".$example2->id.")"],
90: ]);
91: $item = $collection->next();
92: $this->assertEquals($item['exa_string'], 'A');
93: $item = $collection->next();
94: $this->assertEquals($item['exa_string'], 'B');
95:
96:
97: $collection = new CollectionModel(new ExampleModel(), [
98: 'order_by' => ['exa_phone', 'exa_string'],
99: 'sort_by' => ['DESC', 'ASC'],
100: 'where' => ["`exa_id` IN (".$example1->id.", ".$example2->id.")"],
101: ]);
102: $item = $collection->next();
103: $this->assertEquals($item['exa_string'], 'A');
104: $item = $collection->next();
105: $this->assertEquals($item['exa_string'], 'B');
106:
107:
108: $collection = new CollectionModel(new ExampleModel(), ['limit' => 1]);
109: $this->assertEquals($collection->size(), 1);
110:
111:
112: $collection = new CollectionModel(new ExampleModel(), [
113: 'order_by' => 'exa_string',
114: 'sort_by' => 'ASC',
115: 'limit' => '1',
116: 'offset' => '1',
117: 'where' => ["`exa_id` IN (".$example1->id.", ".$example2->id.")"],
118: ]);
119: $item = $collection->next();
120: $this->assertEquals($item['exa_string'], 'B');
121:
122:
123: $collection = new CollectionModel(new ExampleModel(), [
124: 'where' => ["`exa_id` = ".$example1->id],
125: 'join' => ['LEFT JOIN _8.physicians ON exa_mod_id=phy_id']
126: ]);
127: $item = $collection->next();
128: $this->assertEquals($item['phy_id'], $provider->phy_id);
129:
130: }
131:
132: public function testDefaultToCustomerConnection()
133: {
134: Session::set('usr_cur_customer_id', 81);
135:
136: $collection = new CollectionModel(new ICD10Model(), [
137: 'limit' => 1,
138: 'join' => ["LEFT JOIN `_81`.`encounters_icd9_codes_stats`
139: ON `encounters_icd9_codes_stats`.`eics_code_id` = `icd10cm_codes`.`i10c_id`
140: AND `encounters_icd9_codes_stats`.`eics_cs_id` = '".CodingSystemModel::ICD10_CS_ID."'"]
141: ]);
142: }
143:
144: public function testModelModeMaintainExtraData()
145: {
146: Session::set('usr_cur_customer_id', 8);
147:
148: $provider = ProviderModel::query()
149: ->limit(1)
150: ->fetch()
151: ->nextModel();
152:
153: $model = new ExampleModel();
154: $model->exa_string = 'test';
155: $model->exa_mod_id = $provider->phy_id;
156: $model->save();
157:
158: $collection = ExampleModel::query()
159: ->leftJoin('ProviderModel', 'phy_id', '=', 'exa_mod_id')
160: ->where('exa_id', '=', $model->id)
161: ->fetch();
162:
163: $result = $collection->nextModel();
164: $this->assertEquals($result->phy_fname, $provider->phy_fname, "Extra data maintained");
165: }
166: }
167:
168: