|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace bashkarev\clickhouse\tests; |
| 4 | + |
| 5 | +use bashkarev\clickhouse\Query; |
| 6 | + |
| 7 | +class QueryTest extends DatabaseTestCase |
| 8 | +{ |
| 9 | + public function testScalar() |
| 10 | + { |
| 11 | + $db = $this->getConnection(); |
| 12 | + $name = (new Query()) |
| 13 | + ->select('name') |
| 14 | + ->from('{{customer}}') |
| 15 | + ->where([ |
| 16 | + 'id' => 1 |
| 17 | + ]) |
| 18 | + ->scalar($db); |
| 19 | + |
| 20 | + $this->assertEquals('user1', $name); |
| 21 | + } |
| 22 | + |
| 23 | + public function testOne() |
| 24 | + { |
| 25 | + $db = $this->getConnection(); |
| 26 | + $one = (new Query()) |
| 27 | + ->select(['name', 'address']) |
| 28 | + ->from('{{customer}}') |
| 29 | + ->where([ |
| 30 | + 'id' => 1 |
| 31 | + ]) |
| 32 | + ->one($db); |
| 33 | + |
| 34 | + $this->assertEquals(['name' => 'user1', 'address' => 'address1'], $one); |
| 35 | + } |
| 36 | + |
| 37 | + public function testAll() |
| 38 | + { |
| 39 | + $db = $this->getConnection(); |
| 40 | + $rows = (new Query()) |
| 41 | + ->select(['name', 'address']) |
| 42 | + ->from('{{customer}}') |
| 43 | + ->where([ |
| 44 | + 'id' => [1, 2] |
| 45 | + ]) |
| 46 | + ->orderBy(['id' => SORT_ASC]) |
| 47 | + ->all($db); |
| 48 | + |
| 49 | + $this->assertEquals([ |
| 50 | + ['name' => 'user1', 'address' => 'address1'], |
| 51 | + ['name' => 'user2', 'address' => 'address2'] |
| 52 | + ], $rows); |
| 53 | + } |
| 54 | + |
| 55 | + public function testLimitOffset() |
| 56 | + { |
| 57 | + $db = $this->getConnection(); |
| 58 | + $rows = (new Query()) |
| 59 | + ->select('name') |
| 60 | + ->from('{{customer}}') |
| 61 | + ->where([ |
| 62 | + 'id' => [1, 2] |
| 63 | + ]) |
| 64 | + ->orderBy(['id' => SORT_ASC]) |
| 65 | + ->limit(1) |
| 66 | + ->offset(1) |
| 67 | + ->all($db); |
| 68 | + |
| 69 | + $this->assertEquals([['name' => 'user2']], $rows); |
| 70 | + } |
| 71 | + |
| 72 | + public function testGroupBy() |
| 73 | + { |
| 74 | + $db = $this->getConnection(); |
| 75 | + $rows = (new Query()) |
| 76 | + ->select([ |
| 77 | + 'status', |
| 78 | + 'count' => 'COUNT(*)' |
| 79 | + ]) |
| 80 | + ->from('{{customer}}') |
| 81 | + ->where([ |
| 82 | + 'id' => [1, 2, 3] |
| 83 | + ]) |
| 84 | + ->groupBy('status') |
| 85 | + ->orderBy(['status' => SORT_ASC]) |
| 86 | + ->all($db); |
| 87 | + |
| 88 | + $this->assertEquals([ |
| 89 | + ['status' => 1, 'count' => 2], |
| 90 | + ['status' => 2, 'count' => 1] |
| 91 | + ], $rows); |
| 92 | + } |
| 93 | + |
| 94 | + public function testSubQuery() |
| 95 | + { |
| 96 | + $db = $this->getConnection(); |
| 97 | + $subQuery = (new Query()) |
| 98 | + ->select('name') |
| 99 | + ->from('{{customer}}') |
| 100 | + ->where([ |
| 101 | + 'id' => [1] |
| 102 | + ]); |
| 103 | + |
| 104 | + $rows = (new Query()) |
| 105 | + ->select('address') |
| 106 | + ->from('{{customer}}') |
| 107 | + ->where([ |
| 108 | + 'name' => $subQuery |
| 109 | + ]) |
| 110 | + ->all($db); |
| 111 | + |
| 112 | + $this->assertEquals([ |
| 113 | + ['address' => 'address1'], |
| 114 | + ], $rows); |
| 115 | + } |
| 116 | + |
| 117 | + public function testUnionAll() |
| 118 | + { |
| 119 | + $db = $this->getConnection(); |
| 120 | + $secondQuery = (new Query()) |
| 121 | + ->select('name') |
| 122 | + ->from('{{customer}}') |
| 123 | + ->where([ |
| 124 | + 'id' => [1] |
| 125 | + ]); |
| 126 | + |
| 127 | + $rows = (new Query()) |
| 128 | + ->select('name') |
| 129 | + ->from('{{customer}}') |
| 130 | + ->where([ |
| 131 | + 'id' => [2] |
| 132 | + ]) |
| 133 | + ->union($secondQuery, true) |
| 134 | + ->orderBy(['name' => SORT_ASC]) |
| 135 | + ->all($db); |
| 136 | + |
| 137 | + $this->assertEquals([ |
| 138 | + ['name' => 'user1'], |
| 139 | + ['name' => 'user2'] |
| 140 | + ], $rows); |
| 141 | + } |
| 142 | +} |
0 commit comments