|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Hoa |
| 5 | + * |
| 6 | + * |
| 7 | + * @license |
| 8 | + * |
| 9 | + * New BSD License |
| 10 | + * |
| 11 | + * Copyright © 2007-2014, Ivan Enderlin. All rights reserved. |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without |
| 14 | + * modification, are permitted provided that the following conditions are met: |
| 15 | + * * Redistributions of source code must retain the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer. |
| 17 | + * * Redistributions in binary form must reproduce the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer in the |
| 19 | + * documentation and/or other materials provided with the distribution. |
| 20 | + * * Neither the name of the Hoa nor the names of its contributors may be |
| 21 | + * used to endorse or promote products derived from this software without |
| 22 | + * specific prior written permission. |
| 23 | + * |
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
| 28 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | + * POSSIBILITY OF SUCH DAMAGE. |
| 35 | + */ |
| 36 | + |
| 37 | +namespace Hoa\Database\Test\Unit\Query; |
| 38 | + |
| 39 | +use Hoa\Test; |
| 40 | +use Hoa\Database\Query as CUT; |
| 41 | + |
| 42 | +/** |
| 43 | + * Class \Hoa\Database\Test\Unit\Query\Example. |
| 44 | + * |
| 45 | + * Test suite of some examples. |
| 46 | + * |
| 47 | + * @author Ivan Enderlin <ivan.enderlin@hoa-project.net> |
| 48 | + * @copyright Copyright © 2007-2014 Ivan Enderlin. |
| 49 | + * @license New BSD License |
| 50 | + */ |
| 51 | + |
| 52 | +class Example extends Test\Unit\Suite { |
| 53 | + |
| 54 | + public function case_basic_one ( ) { |
| 55 | + |
| 56 | + $this |
| 57 | + ->given($q = new CUT()) |
| 58 | + ->when( |
| 59 | + $result = (string) $q->select('a')->from('foo') |
| 60 | + ) |
| 61 | + ->then |
| 62 | + ->string($result) |
| 63 | + ->isEqualTo('SELECT a FROM foo'); |
| 64 | + } |
| 65 | + |
| 66 | + public function case_reuse ( ) { |
| 67 | + |
| 68 | + $this |
| 69 | + ->given( |
| 70 | + $q = new CUT(), |
| 71 | + $q->setId('x')->select('a')->from('foo') |
| 72 | + ) |
| 73 | + ->when( |
| 74 | + $result = (string) CUT::get('x')->where('i > 3') |
| 75 | + ) |
| 76 | + ->then |
| 77 | + ->string($result) |
| 78 | + ->isEqualTo('SELECT a FROM foo WHERE i > 3'); |
| 79 | + } |
| 80 | + |
| 81 | + public function case_bigger_one ( ) { |
| 82 | + |
| 83 | + $this |
| 84 | + ->given($q = new CUT()) |
| 85 | + ->when( |
| 86 | + $result = (string) $q->select('a') |
| 87 | + ->select('b') |
| 88 | + ->distinct() |
| 89 | + ->from('foo') |
| 90 | + ->groupBy('a') |
| 91 | + ->having('a > 42') |
| 92 | + ->except() |
| 93 | + ->select('c') |
| 94 | + ->from('bar') |
| 95 | + ->orderBy('c') |
| 96 | + ->limit('2') |
| 97 | + ->offset('1') |
| 98 | + ) |
| 99 | + ->then |
| 100 | + ->string($result) |
| 101 | + ->isEqualTo( |
| 102 | + 'SELECT DISTINCT a, b FROM foo GROUP BY a ' . |
| 103 | + 'HAVING a > 42 EXCEPT ' . |
| 104 | + 'SELECT c FROM bar ORDER BY c LIMIT 2 OFFSET 1' |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public function case_sub_selects ( ) { |
| 109 | + |
| 110 | + $this |
| 111 | + ->given($q = new CUT()) |
| 112 | + ->when( |
| 113 | + $result = (string) $q->select('a') |
| 114 | + ->from( |
| 115 | + $q->select('a', 'b') |
| 116 | + ->from('bar') |
| 117 | + ) |
| 118 | + ->leftJoin( |
| 119 | + $q->select('z') |
| 120 | + ->from('qux') |
| 121 | + ) |
| 122 | + ->using('i', 'j', 'k') |
| 123 | + ->as('baz') |
| 124 | + ->limit(1) |
| 125 | + ) |
| 126 | + ->then |
| 127 | + ->string($result) |
| 128 | + ->isEqualTo( |
| 129 | + 'SELECT a FROM (SELECT a, b FROM bar) LEFT JOIN ' . |
| 130 | + '(SELECT z FROM qux) USING (i, j, k) AS baz LIMIT 1' |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + public function case_sub_wheres ( ) { |
| 135 | + |
| 136 | + $this |
| 137 | + ->given($q = new CUT()) |
| 138 | + ->when( |
| 139 | + $result = (string) $q->select('a') |
| 140 | + ->distinct() |
| 141 | + ->from('foo') |
| 142 | + ->where('i > 3') |
| 143 | + ->or |
| 144 | + ->where('j < 4') |
| 145 | + ->or |
| 146 | + ->where( |
| 147 | + $q->where('sub > statement') |
| 148 | + ->and |
| 149 | + ->where('x = y') |
| 150 | + ->or |
| 151 | + ->where( |
| 152 | + $q->where('grr = brouah') |
| 153 | + ) |
| 154 | + ) |
| 155 | + ->where('k IS NULL') |
| 156 | + ) |
| 157 | + ->then |
| 158 | + ->string($result) |
| 159 | + ->isEqualTo( |
| 160 | + 'SELECT DISTINCT a FROM foo WHERE i > 3 OR j < 4 OR ' . |
| 161 | + '(sub > statement AND x = y OR (grr = brouah)) AND ' . |
| 162 | + 'k IS NULL' |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + public function case_default_insert ( ) { |
| 167 | + |
| 168 | + $this |
| 169 | + ->given($q = new CUT()) |
| 170 | + ->when( |
| 171 | + $result = (string) $q->insert() |
| 172 | + ->into('foo') |
| 173 | + ->defaultValues() |
| 174 | + ) |
| 175 | + ->then |
| 176 | + ->string($result) |
| 177 | + ->isEqualTo( |
| 178 | + 'INSERT INTO foo DEFAULT VALUES' |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + public function case_basic_insert ( ) { |
| 183 | + |
| 184 | + $this |
| 185 | + ->given($q = new CUT()) |
| 186 | + ->when( |
| 187 | + $result = (string) $q->insert() |
| 188 | + ->or |
| 189 | + ->rollback() |
| 190 | + ->into('foo') |
| 191 | + ->on('a', 'b', 'c') |
| 192 | + ->values(1, 2, 3) |
| 193 | + ) |
| 194 | + ->then |
| 195 | + ->string($result) |
| 196 | + ->isEqualTo( |
| 197 | + 'INSERT OR ROLLBACK INTO foo (a, b, c) VALUES (1, 2, 3)' |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + public function case_insert_select ( ) { |
| 202 | + |
| 203 | + $this |
| 204 | + ->given($q = new CUT()) |
| 205 | + ->when( |
| 206 | + $result = (string) $q->insert() |
| 207 | + ->or |
| 208 | + ->rollback() |
| 209 | + ->into('foo') |
| 210 | + ->on('a', 'b', 'c') |
| 211 | + ->values( |
| 212 | + $q->select('a', 'b', 'c') |
| 213 | + ->from('foo') |
| 214 | + ->limit(3) |
| 215 | + ) |
| 216 | + ) |
| 217 | + ->then |
| 218 | + ->string($result) |
| 219 | + ->isEqualTo( |
| 220 | + 'INSERT OR ROLLBACK INTO foo (a, b, c) ' . |
| 221 | + 'SELECT a, b, c FROM foo LIMIT 3' |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + public function case_basic_update ( ) { |
| 226 | + |
| 227 | + $this |
| 228 | + ->given($q = new CUT()) |
| 229 | + ->when( |
| 230 | + $result = (string) $q->update() |
| 231 | + ->or |
| 232 | + ->ignore() |
| 233 | + ->table('foo') |
| 234 | + ->set('a', 13) |
| 235 | + ->set('b', 'bar') |
| 236 | + ->where('b = 0') |
| 237 | + ->or |
| 238 | + ->where( |
| 239 | + $q->where('b > 10') |
| 240 | + ->and |
| 241 | + ->where('b < 100') |
| 242 | + ) |
| 243 | + ) |
| 244 | + ->then |
| 245 | + ->string($result) |
| 246 | + ->isEqualTo( |
| 247 | + 'UPDATE OR IGNORE foo SET a = 13, b = bar ' . |
| 248 | + 'WHERE b = 0 OR (b > 10 AND b < 100)' |
| 249 | + ); |
| 250 | + } |
| 251 | + |
| 252 | + public function case_basic_delete ( ) { |
| 253 | + |
| 254 | + $this |
| 255 | + ->given($q = new CUT()) |
| 256 | + ->when( |
| 257 | + $result = (string) $q->delete() |
| 258 | + ->from('foo') |
| 259 | + ->where('b = 0') |
| 260 | + ->or |
| 261 | + ->where( |
| 262 | + $q->where('b > 10') |
| 263 | + ->and |
| 264 | + ->where('b < 100') |
| 265 | + ) |
| 266 | + ) |
| 267 | + ->then |
| 268 | + ->string($result) |
| 269 | + ->isEqualTo( |
| 270 | + 'DELETE FROM foo WHERE b = 0 OR (b > 10 AND b < 100)' |
| 271 | + ); |
| 272 | + } |
| 273 | +} |
0 commit comments