Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelJoinTablesQueryAdapterTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
42  public function testAddLeftTable()
43  {
45  $adapter = new RedBeanModelJoinTablesQueryAdapter('QueryFromModel');
46  $alias = $adapter->addLeftTableAndGetAliasName('zz', 'joinid');
47  $this->assertEquals('zz', $alias);
48  $this->assertEquals(0, $adapter->getFromTableJoinCount());
49  $this->assertEquals(1, $adapter->getLeftTableJoinCount());
50  $fromPart = $adapter->getJoinFromQueryPart();
51  $joinPart = $adapter->getJoinQueryPart();
52  $wherePart = $adapter->getJoinWhereQueryPart();
53  $compareFromPart = null;
54  $compareJoinPart = "left join {$quote}zz{$quote} ";
55  $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
56  $compareWherePart = null;
57  $this->assertEquals($compareFromPart, $fromPart);
58  $this->assertEquals($compareJoinPart, $joinPart);
59  $this->assertEquals($compareWherePart, $wherePart);
60 
61  //Add a second left join
62  $alias = $adapter->addLeftTableAndGetAliasName('a', 'joinid');
63  $this->assertEquals('a', $alias);
64  $this->assertEquals(0, $adapter->getFromTableJoinCount());
65  $this->assertEquals(2, $adapter->getLeftTableJoinCount());
66  $fromPart = $adapter->getJoinFromQueryPart();
67  $joinPart = $adapter->getJoinQueryPart();
68  $wherePart = $adapter->getJoinWhereQueryPart();
69  $compareFromPart = null;
70  $compareJoinPart = "left join {$quote}zz{$quote} ";
71  $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
72  $compareJoinPart .= "left join {$quote}a{$quote} ";
73  $compareJoinPart .= "on {$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
74  $compareWherePart = null;
75  $this->assertEquals($compareFromPart, $fromPart);
76  $this->assertEquals($compareJoinPart, $joinPart);
77  $this->assertEquals($compareWherePart, $wherePart);
78 
79  //Add a third left join that repeats an existing left join table
80  $alias = $adapter->addLeftTableAndGetAliasName('zz', 'otherjoinid');
81  $this->assertEquals('zz1', $alias);
82  $this->assertEquals(0, $adapter->getFromTableJoinCount());
83  $this->assertEquals(3, $adapter->getLeftTableJoinCount());
84  $fromPart = $adapter->getJoinFromQueryPart();
85  $joinPart = $adapter->getJoinQueryPart();
86  $wherePart = $adapter->getJoinWhereQueryPart();
87  $compareFromPart = null;
88  $compareJoinPart = "left join {$quote}zz{$quote} ";
89  $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
90  $compareJoinPart .= "left join {$quote}a{$quote} ";
91  $compareJoinPart .= "on {$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
92  $compareJoinPart .= "left join {$quote}zz{$quote} zz1 ";
93  $compareJoinPart .= "on {$quote}zz1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}otherjoinid{$quote} ";
94  $compareWherePart = null;
95  $this->assertEquals($compareFromPart, $fromPart);
96  $this->assertEquals($compareJoinPart, $joinPart);
97  $this->assertEquals($compareWherePart, $wherePart);
98 
99  //Add a fourth left join that is a repeat of not only an existing table, but an existing join relationship.
100  $alias = $adapter->addLeftTableAndGetAliasName('zz', 'otherjoinid');
101  $this->assertEquals('zz1', $alias);
102  $this->assertEquals(0, $adapter->getFromTableJoinCount());
103  $this->assertEquals(3, $adapter->getLeftTableJoinCount());
104  $fromPart = $adapter->getJoinFromQueryPart();
105  $joinPart = $adapter->getJoinQueryPart();
106  $wherePart = $adapter->getJoinWhereQueryPart();
107  $compareFromPart = null;
108  $compareJoinPart = "left join {$quote}zz{$quote} ";
109  $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
110  $compareJoinPart .= "left join {$quote}a{$quote} ";
111  $compareJoinPart .= "on {$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
112  $compareJoinPart .= "left join {$quote}zz{$quote} zz1 ";
113  $compareJoinPart .= "on {$quote}zz1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}otherjoinid{$quote} ";
114  $compareWherePart = null;
115  $this->assertEquals($compareFromPart, $fromPart);
116  $this->assertEquals($compareJoinPart, $joinPart);
117  $this->assertEquals($compareWherePart, $wherePart);
118 
119  //Add a fifth left join that repeats the main from table that the adapter was constructed with
120  $alias = $adapter->addLeftTableAndGetAliasName('queryfrommodel', 'joinid');
121  $this->assertEquals('queryfrommodel1', $alias);
122  $this->assertEquals(0, $adapter->getFromTableJoinCount());
123  $this->assertEquals(4, $adapter->getLeftTableJoinCount());
124  $fromPart = $adapter->getJoinFromQueryPart();
125  $joinPart = $adapter->getJoinQueryPart();
126  $wherePart = $adapter->getJoinWhereQueryPart();
127  $compareFromPart = null;
128  $compareJoinPart = "left join {$quote}zz{$quote} ";
129  $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
130  $compareJoinPart .= "left join {$quote}a{$quote} ";
131  $compareJoinPart .= "on {$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
132  $compareJoinPart .= "left join {$quote}zz{$quote} zz1 ";
133  $compareJoinPart .= "on {$quote}zz1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}otherjoinid{$quote} ";
134  $compareJoinPart .= "left join {$quote}queryfrommodel{$quote} queryfrommodel1 ";
135  $compareJoinPart .= "on {$quote}queryfrommodel1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
136  $compareWherePart = null;
137  $this->assertEquals($compareFromPart, $fromPart);
138  $this->assertEquals($compareJoinPart, $joinPart);
139  $this->assertEquals($compareWherePart, $wherePart);
140  }
141 
142  public function testAddFromTable()
143  {
145  $adapter = new RedBeanModelJoinTablesQueryAdapter('QueryFromModel');
146  $alias = $adapter->addFromTableAndGetAliasName('zz', 'somejoinid');
147  $this->assertEquals(1, $adapter->getFromTableJoinCount());
148  $this->assertEquals(0, $adapter->getLeftTableJoinCount());
149  $this->assertEquals('zz', $alias);
150  $fromPart = $adapter->getJoinFromQueryPart();
151  $joinPart = $adapter->getJoinQueryPart();
152  $wherePart = $adapter->getJoinWhereQueryPart();
153  $compareFromPart = "{$quote}zz{$quote}";
154  $compareJoinPart = null;
155  $compareWherePart = "{$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote}";
156  $this->assertEquals($compareFromPart, $fromPart);
157  $this->assertEquals($compareJoinPart, $joinPart);
158  $this->assertEquals($compareWherePart, $wherePart);
159 
160  //Add second from table
161  $alias = $adapter->addFromTableAndGetAliasName('a', 'somejoinid');
162  $this->assertEquals(2, $adapter->getFromTableJoinCount());
163  $this->assertEquals(0, $adapter->getLeftTableJoinCount());
164  $this->assertEquals('a', $alias);
165  $fromPart = $adapter->getJoinFromQueryPart();
166  $joinPart = $adapter->getJoinQueryPart();
167  $wherePart = $adapter->getJoinWhereQueryPart();
168  $compareFromPart = "{$quote}zz{$quote}, {$quote}a{$quote}";
169  $compareJoinPart = null;
170  $compareWherePart = "{$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote} and ";
171  $compareWherePart .= "{$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote}";
172  $this->assertEquals($compareFromPart, $fromPart);
173  $this->assertEquals($compareJoinPart, $joinPart);
174  $this->assertEquals($compareWherePart, $wherePart);
175 
176  //add third from table that was a table alread added
177  $alias = $adapter->addFromTableAndGetAliasName('zz', 'somejoinid');
178  $this->assertEquals(3, $adapter->getFromTableJoinCount());
179  $this->assertEquals(0, $adapter->getLeftTableJoinCount());
180  $this->assertEquals('zz1', $alias);
181  $fromPart = $adapter->getJoinFromQueryPart();
182  $joinPart = $adapter->getJoinQueryPart();
183  $wherePart = $adapter->getJoinWhereQueryPart();
184  $compareFromPart = "{$quote}zz{$quote}, {$quote}a{$quote}, {$quote}zz{$quote} zz1";
185  $compareJoinPart = null;
186  $compareWherePart = "{$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote} and ";
187  $compareWherePart .= "{$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote} and ";
188  $compareWherePart .= "{$quote}zz1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote}";
189  $this->assertEquals($compareFromPart, $fromPart);
190  $this->assertEquals($compareJoinPart, $joinPart);
191  $this->assertEquals($compareWherePart, $wherePart);
192 
193  //Add a fourth table, that is a left join.
194  $alias = $adapter->addLeftTableAndGetAliasName('z', 'joinid');
195  $this->assertEquals(3, $adapter->getFromTableJoinCount());
196  $this->assertEquals(1, $adapter->getLeftTableJoinCount());
197  $this->assertEquals('z', $alias);
198  $fromPart = $adapter->getJoinFromQueryPart();
199  $joinPart = $adapter->getJoinQueryPart();
200  $wherePart = $adapter->getJoinWhereQueryPart();
201  $compareFromPart = "{$quote}zz{$quote}, {$quote}a{$quote}, {$quote}zz{$quote} zz1";
202  $compareJoinPart = "left join {$quote}z{$quote} ";
203  $compareJoinPart .= "on {$quote}z{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
204  $compareWherePart = "{$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote} and ";
205  $compareWherePart .= "{$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote} and ";
206  $compareWherePart .= "{$quote}zz1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote}";
207  $this->assertEquals($compareFromPart, $fromPart);
208  $this->assertEquals($compareJoinPart, $joinPart);
209  $this->assertEquals($compareWherePart, $wherePart);
210 
211  //Add a fifth table, that is a left join of an existing from table.
212  $alias = $adapter->addLeftTableAndGetAliasName('queryfrommodel', 'joinid');
213  $this->assertEquals('queryfrommodel1', $alias);
214  $this->assertEquals(3, $adapter->getFromTableJoinCount());
215  $this->assertEquals(2, $adapter->getLeftTableJoinCount());
216  $fromPart = $adapter->getJoinFromQueryPart();
217  $joinPart = $adapter->getJoinQueryPart();
218  $wherePart = $adapter->getJoinWhereQueryPart();
219  $compareFromPart = "{$quote}zz{$quote}, {$quote}a{$quote}, {$quote}zz{$quote} zz1";
220  $compareJoinPart = "left join {$quote}z{$quote}";
221  $compareJoinPart .= " on {$quote}z{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote}";
222  $compareJoinPart .= " left join {$quote}queryfrommodel{$quote} queryfrommodel1";
223  $compareJoinPart .= " on {$quote}queryfrommodel1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
224  $compareWherePart = "{$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote} and ";
225  $compareWherePart .= "{$quote}a{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote} and ";
226  $compareWherePart .= "{$quote}zz1{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}somejoinid{$quote}";
227  $this->assertEquals($compareFromPart, $fromPart);
228  $this->assertEquals($compareJoinPart, $joinPart);
229  $this->assertEquals($compareWherePart, $wherePart);
230 
231  //Add a sixth table that exists already to demonstrate the alias working for a third table.
232  //You must use a different join id so that it creates a new alias. using cjoinid instead of joinid
233  $alias = $adapter->addLeftTableAndGetAliasName('queryfrommodel', 'cjoinid');
234  $this->assertEquals('queryfrommodel2', $alias);
235 
236  //If we just use joinid, then it will not create a new alias.
237  $alias = $adapter->addLeftTableAndGetAliasName('queryfrommodel', 'joinid');
238  $this->assertEquals('queryfrommodel1', $alias);
239  $alias = $adapter->addLeftTableAndGetAliasName('queryfrommodel', 'cjoinid');
240  $this->assertEquals('queryfrommodel2', $alias);
241  }
242 
243  public function testGetAlreadyFromJoinedTableAliasName()
244  {
245  $adapter = new RedBeanModelJoinTablesQueryAdapter('QueryFromModel');
246  $this->assertNull($adapter->GetAlreadyFromJoinedTableAliasName('zz'));
247  $alias = $adapter->addFromTableAndGetAliasName('z', 'joinid');
248  $this->assertEquals($alias, $adapter->getAlreadyFromJoinedTableAliasName('z'));
249  }
250 
251  public function testAddLeftTableAndGetAliasNameWithSpecifiedOnTableAliasName()
252  {
254  $adapter = new RedBeanModelJoinTablesQueryAdapter('QueryFromModel');
255  $alias = $adapter->addLeftTableAndGetAliasName('zz', 'joinid');
256  $this->assertEquals('zz', $alias);
257  $this->assertEquals(0, $adapter->getFromTableJoinCount());
258  $this->assertEquals(1, $adapter->getLeftTableJoinCount());
259  $fromPart = $adapter->getJoinFromQueryPart();
260  $joinPart = $adapter->getJoinQueryPart();
261  $wherePart = $adapter->getJoinWhereQueryPart();
262  $compareFromPart = null;
263  $compareJoinPart = "left join {$quote}zz{$quote} ";
264  $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
265  $compareWherePart = null;
266  $this->assertEquals($compareFromPart, $fromPart);
267  $this->assertEquals($compareJoinPart, $joinPart);
268  $this->assertEquals($compareWherePart, $wherePart);
269 
270  //Now add a specified onTableAliasName
271  $alias = $adapter->addLeftTableAndGetAliasName('xyz', 'ajoinid', 'zz');
272  $this->assertEquals('xyz', $alias);
273  $this->assertEquals(0, $adapter->getFromTableJoinCount());
274  $this->assertEquals(2, $adapter->getLeftTableJoinCount());
275  $fromPart = $adapter->getJoinFromQueryPart();
276  $joinPart = $adapter->getJoinQueryPart();
277  $wherePart = $adapter->getJoinWhereQueryPart();
278  $compareFromPart = null;
279  $compareJoinPart = "left join {$quote}zz{$quote} ";
280  $compareJoinPart .= "on {$quote}zz{$quote}.{$quote}id{$quote} = {$quote}queryfrommodel{$quote}.{$quote}joinid{$quote} ";
281  $compareJoinPart .= "left join {$quote}xyz{$quote} ";
282  $compareJoinPart .= "on {$quote}xyz{$quote}.{$quote}id{$quote} = {$quote}zz{$quote}.{$quote}ajoinid{$quote} ";
283  $compareWherePart = null;
284  $this->assertEquals($compareFromPart, $fromPart);
285  $this->assertEquals($compareJoinPart, $joinPart);
286  $this->assertEquals($compareWherePart, $wherePart);
287  }
288  }
289 ?>
Generated on Thu Jul 9 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.