Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelSelectQueryAdapterTest.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 
38  {
39  protected static $chicagoOffsetInSeconds = 0;
40 
41  public static function setUpBeforeClass()
42  {
43  parent::setUpBeforeClass();
44  SecurityTestHelper::createSuperAdmin();
45  }
46 
47  public function setup()
48  {
49  parent::setup();
50  Yii::app()->user->userModel = User::getByUsername('super');
51  Yii::app()->user->userModel->timeZone = 'America/Chicago';
52  //Deal with daylight savings time.
53  $timeZoneObject = new DateTimeZone(Yii::app()->user->userModel->timeZone);
54  $offsetInSeconds = $timeZoneObject->getOffset(new DateTime());
55  $this->assertTrue($offsetInSeconds == -18000 || $offsetInSeconds == -21600);
56  self::$chicagoOffsetInSeconds = $offsetInSeconds;
57  }
58 
59  public function testIsDistinct()
60  {
61  $adapter = new RedBeanModelSelectQueryAdapter();
62  $this->assertFalse($adapter->isDistinct());
63  $adapter = new RedBeanModelSelectQueryAdapter(true);
64  $this->assertTrue($adapter->isDistinct());
65  }
66 
67  public function testAddClause()
68  {
70  $adapter = new RedBeanModelSelectQueryAdapter();
71  $this->assertEquals(0, $adapter->getClausesCount());
72  $adapter->addClause('a', 'b', 'c');
73  $this->assertEquals(1, $adapter->getClausesCount());
74  $compareString = "select {$quote}a{$quote}.{$quote}b{$quote} c ";
75  $this->assertEquals($compareString, $adapter->getSelect());
76 
77  $adapter = new RedBeanModelSelectQueryAdapter(true);
78  $this->assertEquals(0, $adapter->getClausesCount());
79  $adapter->addClause('a', 'b', 'c');
80  $this->assertEquals(1, $adapter->getClausesCount());
81  $compareString = "select distinct {$quote}a{$quote}.{$quote}b{$quote} c ";
82  $this->assertEquals($compareString, $adapter->getSelect());
83 
84  //Test with multiple clauses
86  $adapter = new RedBeanModelSelectQueryAdapter();
87  $this->assertEquals(0, $adapter->getClausesCount());
88  $adapter->addClause('a', 'b', 'c');
89  $adapter->addClause('d', 'e', 'f');
90  $this->assertEquals(2, $adapter->getClausesCount());
91  $compareString = "select {$quote}a{$quote}.{$quote}b{$quote} c, {$quote}d{$quote}.{$quote}e{$quote} f ";
92  $this->assertEquals($compareString, $adapter->getSelect());
93  }
94 
95  public function testAddClauseWithColumnNameOnlyAndNoEnclosure()
96  {
98  $adapter = new RedBeanModelSelectQueryAdapter();
99  $this->assertEquals(0, $adapter->getClausesCount());
100  $adapter->addClauseWithColumnNameOnlyAndNoEnclosure('a', 'b');
101  $this->assertEquals(1, $adapter->getClausesCount());
102  $compareString = "select a b ";
103  $this->assertEquals($compareString, $adapter->getSelect());
104  }
105 
106  public function testAddCountClause()
107  {
109  $adapter = new RedBeanModelSelectQueryAdapter();
110  $this->assertEquals(0, $adapter->getClausesCount());
111  $adapter->addCountClause('a', 'b', 'c');
112  $this->assertEquals(1, $adapter->getClausesCount());
113  $compareString = "select count({$quote}a{$quote}.{$quote}b{$quote}) c ";
114  $this->assertEquals($compareString, $adapter->getSelect());
115 
116  $adapter = new RedBeanModelSelectQueryAdapter(true);
117  $this->assertEquals(0, $adapter->getClausesCount());
118  $adapter->addCountClause('a', 'b', 'c');
119  $this->assertEquals(1, $adapter->getClausesCount());
120  $compareString = "select count(distinct {$quote}a{$quote}.{$quote}b{$quote}) c ";
121  $this->assertEquals($compareString, $adapter->getSelect());
122  }
123 
124  public function testAddNonSpecificCountClause()
125  {
126  $adapter = new RedBeanModelSelectQueryAdapter();
127  $this->assertEquals(0, $adapter->getClausesCount());
128  $adapter->addNonSpecificCountClause();
129  $this->assertEquals(1, $adapter->getClausesCount());
130  $compareString = "select count(*) ";
131  $this->assertEquals($compareString, $adapter->getSelect());
132  }
133 
134  public function testAddSummationClause()
135  {
137  $adapter = new RedBeanModelSelectQueryAdapter();
138  $this->assertEquals(0, $adapter->getClausesCount());
139  $adapter->addSummationClause('table', 'abc', 'c');
140  $this->assertEquals(1, $adapter->getClausesCount());
141  $compareString = "select sum({$quote}table{$quote}.{$quote}abc{$quote}) c ";
142  $this->assertEquals($compareString, $adapter->getSelect());
143 
144  $adapter = new RedBeanModelSelectQueryAdapter(true);
145  $this->assertEquals(0, $adapter->getClausesCount());
146  $adapter->addSummationClause('table', 'def', 'c');
147  $this->assertEquals(1, $adapter->getClausesCount());
148  $compareString = "select distinct sum({$quote}table{$quote}.{$quote}def{$quote}) c ";
149  $this->assertEquals($compareString, $adapter->getSelect());
150 
152  $adapter = new RedBeanModelSelectQueryAdapter();
153  $this->assertEquals(0, $adapter->getClausesCount());
154  $adapter->addSummationClause('table', 'abc', 'c', ' extra stuff');
155  $this->assertEquals(1, $adapter->getClausesCount());
156  $compareString = "select sum({$quote}table{$quote}.{$quote}abc{$quote} extra stuff) c ";
157  $this->assertEquals($compareString, $adapter->getSelect());
158  }
159 
160  public function testAddAverageClause()
161  {
163  $adapter = new RedBeanModelSelectQueryAdapter();
164  $this->assertEquals(0, $adapter->getClausesCount());
165  $adapter->addAverageClause('table', 'abc', 'c');
166  $this->assertEquals(1, $adapter->getClausesCount());
167  $compareString = "select avg({$quote}table{$quote}.{$quote}abc{$quote}) c ";
168  $this->assertEquals($compareString, $adapter->getSelect());
169 
171  $adapter = new RedBeanModelSelectQueryAdapter();
172  $this->assertEquals(0, $adapter->getClausesCount());
173  $adapter->addAverageClause('table', 'abc', 'c', ' extra stuff');
174  $this->assertEquals(1, $adapter->getClausesCount());
175  $compareString = "select avg({$quote}table{$quote}.{$quote}abc{$quote} extra stuff) c ";
176  $this->assertEquals($compareString, $adapter->getSelect());
177  }
178 
179  public function testAddMinimumClause()
180  {
182  $adapter = new RedBeanModelSelectQueryAdapter();
183  $this->assertEquals(0, $adapter->getClausesCount());
184  $adapter->addMinimumClause('table', 'abc', 'c');
185  $this->assertEquals(1, $adapter->getClausesCount());
186  $compareString = "select min({$quote}table{$quote}.{$quote}abc{$quote}) c ";
187  $this->assertEquals($compareString, $adapter->getSelect());
188 
190  $adapter = new RedBeanModelSelectQueryAdapter();
191  $this->assertEquals(0, $adapter->getClausesCount());
192  $adapter->addMinimumClause('table', 'abc', 'c', ' extra stuff');
193  $this->assertEquals(1, $adapter->getClausesCount());
194  $compareString = "select min({$quote}table{$quote}.{$quote}abc{$quote} extra stuff) c ";
195  $this->assertEquals($compareString, $adapter->getSelect());
196  }
197 
198  public function testAddMaximumClause()
199  {
201  $adapter = new RedBeanModelSelectQueryAdapter();
202  $this->assertEquals(0, $adapter->getClausesCount());
203  $adapter->addMaximumClause('table', 'abc', 'c');
204  $this->assertEquals(1, $adapter->getClausesCount());
205  $compareString = "select max({$quote}table{$quote}.{$quote}abc{$quote}) c ";
206  $this->assertEquals($compareString, $adapter->getSelect());
207 
209  $adapter = new RedBeanModelSelectQueryAdapter();
210  $this->assertEquals(0, $adapter->getClausesCount());
211  $adapter->addMaximumClause('table', 'abc', 'c', ' extra stuff');
212  $this->assertEquals(1, $adapter->getClausesCount());
213  $compareString = "select max({$quote}table{$quote}.{$quote}abc{$quote} extra stuff) c ";
214  $this->assertEquals($compareString, $adapter->getSelect());
215  }
216 
217  public function testAddDayClause()
218  {
220  $adapter = new RedBeanModelSelectQueryAdapter();
221  $this->assertEquals(0, $adapter->getClausesCount());
222  $adapter->addDayClause('table', 'abc', 'c');
223  $this->assertEquals(1, $adapter->getClausesCount());
224  $compareString = "select day({$quote}table{$quote}.{$quote}abc{$quote}) c ";
225  $this->assertEquals($compareString, $adapter->getSelect());
226  }
227 
228  public function testAddDayClauseWithTimeZoneAdjustment()
229  {
231  $adapter = new RedBeanModelSelectQueryAdapter();
232  $this->assertEquals(0, $adapter->getClausesCount());
233  $adapter->addDayClause('table', 'abc', 'c', true);
234  $this->assertEquals(1, $adapter->getClausesCount());
235  $compareString = "select day({$quote}table{$quote}.{$quote}abc{$quote} - INTERVAL " .
236  abs(self::$chicagoOffsetInSeconds) . " SECOND) c ";
237  $this->assertEquals($compareString, $adapter->getSelect());
238  }
239 
240  public function testAddDayDateClause()
241  {
243  $adapter = new RedBeanModelSelectQueryAdapter();
244  $this->assertEquals(0, $adapter->getClausesCount());
245  $adapter->addDayDateClause('table', 'abc', 'c');
246  $this->assertEquals(1, $adapter->getClausesCount());
247  $compareString = "select DATE_FORMAT({$quote}table{$quote}.{$quote}abc{$quote}, '%Y-%m-%d') c ";
248  $this->assertEquals($compareString, $adapter->getSelect());
249  }
250 
251  public function testAddWeekClause()
252  {
254  $adapter = new RedBeanModelSelectQueryAdapter();
255  $this->assertEquals(0, $adapter->getClausesCount());
256  $adapter->addWeekClause('table', 'abc', 'c');
257  $this->assertEquals(1, $adapter->getClausesCount());
258  $compareString = "select weekofyear({$quote}table{$quote}.{$quote}abc{$quote}) c ";
259  $this->assertEquals($compareString, $adapter->getSelect());
260  }
261 
262  public function testAddWeekClauseWithTimeZoneAdjustment()
263  {
265  $adapter = new RedBeanModelSelectQueryAdapter();
266  $this->assertEquals(0, $adapter->getClausesCount());
267  $adapter->addWeekClause('table', 'abc', 'c', true);
268  $this->assertEquals(1, $adapter->getClausesCount());
269  $compareString = "select weekofyear({$quote}table{$quote}.{$quote}abc{$quote} - INTERVAL " .
270  abs(self::$chicagoOffsetInSeconds) . " SECOND) c ";
271  $this->assertEquals($compareString, $adapter->getSelect());
272  }
273 
274  public function testAddFirstDayOfWeekDateClause()
275  {
277  $adapter = new RedBeanModelSelectQueryAdapter();
278  $this->assertEquals(0, $adapter->getClausesCount());
279  $adapter->addFirstDayOfWeekDateClause('table', 'abc', 'c');
280  $this->assertEquals(1, $adapter->getClausesCount());
281  $compareString = "select DATE_FORMAT(DATE_SUB({$quote}table{$quote}.{$quote}abc{$quote}, " .
282  "INTERVAL(WEEKDAY({$quote}table{$quote}.{$quote}abc{$quote})) day), " .
283  "'%Y-%m-%d') c ";
284  $this->assertEquals($compareString, $adapter->getSelect());
285  }
286 
287  public function testAddMonthClause()
288  {
290  $adapter = new RedBeanModelSelectQueryAdapter();
291  $this->assertEquals(0, $adapter->getClausesCount());
292  $adapter->addMonthClause('table', 'abc', 'c');
293  $this->assertEquals(1, $adapter->getClausesCount());
294  $compareString = "select month({$quote}table{$quote}.{$quote}abc{$quote}) c ";
295  $this->assertEquals($compareString, $adapter->getSelect());
296  }
297 
298  public function testAddMonthClauseWithTimeZoneAdjustment()
299  {
301  $adapter = new RedBeanModelSelectQueryAdapter();
302  $this->assertEquals(0, $adapter->getClausesCount());
303  $adapter->addMonthClause('table', 'abc', 'c', true);
304  $this->assertEquals(1, $adapter->getClausesCount());
305  $compareString = "select month({$quote}table{$quote}.{$quote}abc{$quote} - INTERVAL " .
306  abs(self::$chicagoOffsetInSeconds) . " SECOND) c ";
307  $this->assertEquals($compareString, $adapter->getSelect());
308  }
309 
310  public function testAddFirstDayOfMonthDateClause()
311  {
313  $adapter = new RedBeanModelSelectQueryAdapter();
314  $this->assertEquals(0, $adapter->getClausesCount());
315  $adapter->addFirstDayOfMonthDateClause('table', 'abc', 'c');
316  $this->assertEquals(1, $adapter->getClausesCount());
317  $compareString = "select DATE_FORMAT(DATE_ADD({$quote}table{$quote}.{$quote}abc{$quote}, " .
318  "INTERVAL(1-DAYOFMONTH({$quote}table{$quote}.{$quote}abc{$quote})) day), " .
319  "'%Y-%m-%d') c ";
320  $this->assertEquals($compareString, $adapter->getSelect());
321  }
322 
323  public function testAddQuarterClause()
324  {
326  $adapter = new RedBeanModelSelectQueryAdapter();
327  $this->assertEquals(0, $adapter->getClausesCount());
328  $adapter->addQuarterClause('table', 'abc', 'c');
329  $this->assertEquals(1, $adapter->getClausesCount());
330  $compareString = "select quarter({$quote}table{$quote}.{$quote}abc{$quote}) c ";
331  $this->assertEquals($compareString, $adapter->getSelect());
332  }
333 
334  public function testAddQuarterClauseWithTimeZoneAdjustment()
335  {
337  $adapter = new RedBeanModelSelectQueryAdapter();
338  $this->assertEquals(0, $adapter->getClausesCount());
339  $adapter->addQuarterClause('table', 'abc', 'c', true);
340  $this->assertEquals(1, $adapter->getClausesCount());
341  $compareString = "select quarter({$quote}table{$quote}.{$quote}abc{$quote} - INTERVAL " .
342  abs(self::$chicagoOffsetInSeconds) . " SECOND) c ";
343  $this->assertEquals($compareString, $adapter->getSelect());
344  }
345 
346  public function testAddYearClause()
347  {
349  $adapter = new RedBeanModelSelectQueryAdapter();
350  $this->assertEquals(0, $adapter->getClausesCount());
351  $adapter->addYearClause('table', 'abc', 'c');
352  $this->assertEquals(1, $adapter->getClausesCount());
353  $compareString = "select year({$quote}table{$quote}.{$quote}abc{$quote}) c ";
354  $this->assertEquals($compareString, $adapter->getSelect());
355  }
356 
357  public function testAddYearClauseWithTimeZoneAdjustment()
358  {
360  $adapter = new RedBeanModelSelectQueryAdapter();
361  $this->assertEquals(0, $adapter->getClausesCount());
362  $adapter->addYearClause('table', 'abc', 'c', true);
363  $this->assertEquals(1, $adapter->getClausesCount());
364  $compareString = "select year({$quote}table{$quote}.{$quote}abc{$quote} - INTERVAL " .
365  abs(self::$chicagoOffsetInSeconds) . " SECOND) c ";
366  $this->assertEquals($compareString, $adapter->getSelect());
367  }
368 
369  public function testResolveIdClause()
370  {
372  $adapter = new RedBeanModelSelectQueryAdapter();
373  $this->assertEquals(0, $adapter->getClausesCount());
374  $adapter->resolveIdClause('xModel', 'yTableAlias');
375  $this->assertEquals(1, $adapter->getClausesCount());
376  $adapter->resolveIdClause('xModel', 'yTableAlias');
377  $this->assertEquals(1, $adapter->getClausesCount());
378  $adapter->resolveIdClause('xModel', 'zTableAlias');
379  $this->assertEquals(2, $adapter->getClausesCount());
380  }
381 
382  public function testResolveForAliasName()
383  {
385  $adapter = new RedBeanModelSelectQueryAdapter();
386  $content = $adapter->resolveForAliasName('abc');
387  $this->assertEquals('abc', $content);
388  $content = $adapter->resolveForAliasName('abc', 'def');
389  $this->assertEquals('abc def', $content);
390  }
391 
392  public function testAddClauseByQueryString()
393  {
394  $adapter = new RedBeanModelSelectQueryAdapter();
395  $this->assertEquals(0, $adapter->getClausesCount());
396  $adapter->addClauseByQueryString('querystring');
397  $this->assertEquals(1, $adapter->getClausesCount());
398  $compareString = "select querystring ";
399  $this->assertEquals($compareString, $adapter->getSelect());
400 
401  //Test with aliasName
403  $adapter = new RedBeanModelSelectQueryAdapter();
404  $this->assertEquals(0, $adapter->getClausesCount());
405  $adapter->addClauseByQueryString('querystring', 'aliasName');
406  $this->assertEquals(1, $adapter->getClausesCount());
407  $compareString = "select querystring aliasName ";
408  $this->assertEquals($compareString, $adapter->getSelect());
409  }
410  }
411 ?>
static getByUsername($username)
Definition: User.php:49
Generated on Sat Jul 11 2020 07:10:26
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.