Hoa central
Example.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Database\Query as CUT;
40 use Hoa\Test;
41 
50 class Example extends Test\Unit\Suite
51 {
52  public function case_basic_one()
53  {
54  $this
55  ->given($q = new CUT())
56  ->when(
57  $result = (string) $q->select('a')->from('foo')
58  )
59  ->then
60  ->string($result)
61  ->isEqualTo('SELECT a FROM foo');
62  }
63 
64  public function case_reuse()
65  {
66  $this
67  ->given(
68  $q = new CUT(),
69  $q->setId('x')->select('a')->from('foo')
70  )
71  ->when(
72  $result = (string) CUT::get('x')->where('i > 3')
73  )
74  ->then
75  ->string($result)
76  ->isEqualTo('SELECT a FROM foo WHERE i > 3');
77  }
78 
79  public function case_bigger_one()
80  {
81  $this
82  ->given($q = new CUT())
83  ->when(
84  $result = (string) $q->select('a')
85  ->select('b')
86  ->distinct()
87  ->from('foo')
88  ->groupBy('a')
89  ->having('a > 42')
90  ->except()
91  ->select('c')
92  ->from('bar')
93  ->orderBy('c')
94  ->limit('2')
95  ->offset('1')
96  )
97  ->then
98  ->string($result)
99  ->isEqualTo(
100  'SELECT DISTINCT a, b FROM foo GROUP BY a ' .
101  'HAVING a > 42 EXCEPT ' .
102  'SELECT c FROM bar ORDER BY c LIMIT 2 OFFSET 1'
103  );
104  }
105 
106  public function case_sub_selects()
107  {
108  $this
109  ->given($q = new CUT())
110  ->when(
111  $result = (string) $q->select('a')
112  ->from(
113  $q->select('a', 'b')
114  ->from('bar')
115  )
116  ->leftJoin(
117  $q->select('z')
118  ->from('qux')
119  )
120  ->using('i', 'j', 'k')
121  ->as('baz')
122  ->limit(1)
123  )
124  ->then
125  ->string($result)
126  ->isEqualTo(
127  'SELECT a FROM (SELECT a, b FROM bar) LEFT JOIN ' .
128  '(SELECT z FROM qux) USING (i, j, k) AS baz LIMIT 1'
129  );
130  }
131 
132  public function case_sub_wheres()
133  {
134  $this
135  ->given($q = new CUT())
136  ->when(
137  $result = (string) $q->select('a')
138  ->distinct()
139  ->from('foo')
140  ->where('i > 3')
141  ->or
142  ->where('j < 4')
143  ->or
144  ->where(
145  $q->where('sub > statement')
146  ->and
147  ->where('x = y')
148  ->or
149  ->where(
150  $q->where('grr = brouah')
151  )
152  )
153  ->where('k IS NULL')
154  )
155  ->then
156  ->string($result)
157  ->isEqualTo(
158  'SELECT DISTINCT a FROM foo WHERE i > 3 OR j < 4 OR ' .
159  '(sub > statement AND x = y OR (grr = brouah)) AND ' .
160  'k IS NULL'
161  );
162  }
163 
164  public function case_default_insert()
165  {
166  $this
167  ->given($q = new CUT())
168  ->when(
169  $result = (string) $q->insert()
170  ->into('foo')
171  ->defaultValues()
172  )
173  ->then
174  ->string($result)
175  ->isEqualTo(
176  'INSERT INTO foo DEFAULT VALUES'
177  );
178  }
179 
180  public function case_basic_insert()
181  {
182  $this
183  ->given($q = new CUT())
184  ->when(
185  $result = (string) $q->insert()
186  ->or
187  ->rollback()
188  ->into('foo')
189  ->on('a', 'b', 'c')
190  ->values(1, 2, 3)
191  )
192  ->then
193  ->string($result)
194  ->isEqualTo(
195  'INSERT OR ROLLBACK INTO foo (a, b, c) VALUES (1, 2, 3)'
196  );
197  }
198 
199  public function case_insert_select()
200  {
201  $this
202  ->given($q = new CUT())
203  ->when(
204  $result = (string) $q->insert()
205  ->or
206  ->rollback()
207  ->into('foo')
208  ->on('a', 'b', 'c')
209  ->values(
210  $q->select('a', 'b', 'c')
211  ->from('foo')
212  ->limit(3)
213  )
214  )
215  ->then
216  ->string($result)
217  ->isEqualTo(
218  'INSERT OR ROLLBACK INTO foo (a, b, c) ' .
219  'SELECT a, b, c FROM foo LIMIT 3'
220  );
221  }
222 
223  public function case_basic_update()
224  {
225  $this
226  ->given($q = new CUT())
227  ->when(
228  $result = (string) $q->update()
229  ->or
230  ->ignore()
231  ->table('foo')
232  ->set('a', 13)
233  ->set('b', 'bar')
234  ->where('b = 0')
235  ->or
236  ->where(
237  $q->where('b > 10')
238  ->and
239  ->where('b < 100')
240  )
241  )
242  ->then
243  ->string($result)
244  ->isEqualTo(
245  'UPDATE OR IGNORE foo SET a = 13, b = bar ' .
246  'WHERE b = 0 OR (b > 10 AND b < 100)'
247  );
248  }
249 
250  public function case_basic_delete()
251  {
252  $this
253  ->given($q = new CUT())
254  ->when(
255  $result = (string) $q->delete()
256  ->from('foo')
257  ->where('b = 0')
258  ->or
259  ->where(
260  $q->where('b > 10')
261  ->and
262  ->where('b < 100')
263  )
264  )
265  ->then
266  ->string($result)
267  ->isEqualTo(
268  'DELETE FROM foo WHERE b = 0 OR (b > 10 AND b < 100)'
269  );
270  }
271 }