Skip to content

Commit 2717e3a

Browse files
committed
small fixes
1 parent dba34f3 commit 2717e3a

File tree

4 files changed

+130
-109
lines changed

4 files changed

+130
-109
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
convertErrorsToExceptions="true"
44
convertNoticesToExceptions="true"
55
convertWarningsToExceptions="true"
6-
stopOnFailure="false">
6+
stopOnFailure="true">
77
<testsuites>
88
<testsuite name="Default Test Suite">
99
<directory>./tests</directory>

src/ClickHouseAPI.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,13 @@ public function versionSendQuery()
714714

715715
// hook-function for each requests execute when request is finished
716716
$fn = function($obj, $slot_low, $par) {
717-
$ver = \explode("\n", $this->slot_results[$slot_low]['response']);
718-
$ver = (\count($ver) == 2 && \strlen($ver[0]) < 32) ? $ver[0] : "Unknown";
717+
// at first, checking curl-error
718+
$ver = $this->slot_results[$slot_low]['curl_error'];
719+
if (empty($ver)) {
720+
// if no curl error, checking response
721+
$ver = \explode("\n", $this->slot_results[$slot_low]['response']);
722+
$ver = (\count($ver) == 2 && \strlen($ver[0]) < 32) ? $ver[0] : "Unknown";
723+
}
719724
$is_supported = \is_numeric(\substr($ver,0,1)) && (\count(\explode(".", $ver)) > 2);
720725
if (!$is_supported) {
721726
$this->session_autocreate = false;

src/ClickHouseSlots.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,19 +522,23 @@ public function fromSlot($slot)
522522
}
523523

524524
/**
525-
* Set slot for prepare async request
525+
* Set slot name for prepare async request, or clear slot (set sync-mode)
526526
*
527-
* @param string $slot Slot name
527+
* @param string|false $slot Slot name or false for set sync-mode
528528
* @return $this
529529
* @throws \Exception
530530
*/
531-
public function toSlot($slot)
531+
public function toSlot($slot = false)
532532
{
533-
$slot_low = strtolower($slot);
534-
if (isset($this->multi_ch[$slot_low])) {
535-
throw new \Exception("Slot '$slot' is busy");
533+
if ($slot) {
534+
$slot_low = strtolower($slot);
535+
if (isset($this->multi_ch[$slot_low])) {
536+
throw new \Exception("Slot '$slot' is busy");
537+
}
538+
$this->to_slot = $slot_low;
539+
} else {
540+
$this->to_slot = false;
536541
}
537-
$this->to_slot = $slot_low;
538542
return $this;
539543
}
540544

tests/src/ClickHouseAPITest.php

Lines changed: 111 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,117 @@ public function testSetServerUrlException()
7474
$ch->setServerUrl("ftp://8.8.8.8");
7575
}
7676

77+
/**
78+
* @covers ierusalim\ClickHouse\ClickHouseAPI::doApiCall
79+
* @todo Implement testDoApiCall().
80+
*/
81+
public function testDoApiCall()
82+
{
83+
$ch = $this->object;
84+
85+
// $ch->toSlot()->doApiCall - clear async-mode, than do..
86+
87+
$ans = $ch->toSlot()->doApiCall(0, ['query' => 'SELECT version()']);
88+
89+
$curl_error = $ans['curl_error'];
90+
if ($curl_error) {
91+
echo "\nCURL_ERROR: $curl_error";
92+
$this->assertTrue(empty($curl_error));
93+
} else {
94+
echo "Version response: {$ans['response']}Starting tests...\n";
95+
}
96+
97+
$slot = "tmp1";
98+
$ans = $ch->toSlot($slot)->doApiCall(false, ['query'=>'SELECT 123']);
99+
$this->assertEquals(102, $ans['code']);
100+
101+
$ch->debug = true;
102+
$ch->hook_before_api_call = function ($url, $obj) {
103+
return "https://ierusalim.github.io";
104+
};
105+
106+
$file = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . '.gitignore';
107+
108+
$ans = $ch->doApiCall("empty", [], true, [], $file);
109+
$this->assertEquals($ans['code'], 405);
110+
111+
$this->assertEquals("123\n", $ch->fromSlot($slot)->results);
112+
}
113+
114+
/**
115+
* @covers ierusalim\ClickHouse\ClickHouseAPI::yiDoApiCall
116+
* @todo Implement testYiDoApiCall().
117+
*/
118+
public function testYiDoApiCall()
119+
{
120+
$ch = $this->object;
121+
122+
if ($ch->isSupported('query')) {
123+
$table = "anytabletmp";
124+
125+
$file = 'anyfile.txt';
126+
127+
$file_data = '';
128+
for ($t=1; $t<100; $t++) {
129+
$file_data .= $t . "\t2017-12-12\tAny string data\n";
130+
}
131+
132+
$file_size = file_put_contents($file, $file_data);
133+
134+
$this->assertTrue($file_size > 0);
135+
136+
$fields = '(id, dt, s)';
137+
$structure_excactly = 'id UInt32, dt Date, s String';
138+
139+
$ch->query("DROP TABLE IF EXISTS $table")
140+
141+
->query("CREATE TABLE $table" .
142+
"( $structure_excactly )" .
143+
"ENGINE = MergeTree(dt, (id, dt), 8192)");
144+
145+
$ch->is_windows = true;
146+
147+
$ans = $ch->doApiCall(false,
148+
['query' => "INSERT INTO $table $fields FORMAT TabSeparated"],
149+
true, [], $file, true);
150+
151+
$ch->query("SELECT * FROM $table");
152+
$this->assertEquals($file_data, $ch->results);
153+
154+
$ch->is_windows = false;
155+
156+
try {
157+
$ans = $ch->doApiCall(false,
158+
['query' => "INSERT INTO $table $fields FORMAT TabSeparated"],
159+
true, [], $file, true);
160+
} catch (\Exception $e) {
161+
echo $e->getMessage();
162+
\fclose($ch->fh);
163+
}
164+
165+
166+
$ch->query("DROP TABLE IF EXISTS $table");
167+
unlink($file);
168+
unlink($file . '.gz');
169+
$slot = "tmp1";
170+
$ans = $ch->toSlot($slot)->doApiCall(false, ['query'=>'SELECT 123']);
171+
$this->assertEquals(102, $ans['code']);
172+
173+
$ch->debug = true;
174+
175+
$ch->hook_before_api_call = function ($url, $obj) {
176+
return "https://ierusalim.github.io";
177+
};
178+
179+
$file = dirname(dirname(__DIR__)) . \DIRECTORY_SEPARATOR . '.gitignore';
180+
181+
$ans = $ch->doApiCall("empty", [], true, [], $file);
182+
$this->assertEquals($ans['code'], 405);
183+
184+
$this->assertEquals("123\n", $ch->fromSlot($slot)->results);
185+
}
186+
}
187+
77188
/**
78189
* @covers ierusalim\ClickHouse\ClickHouseAPI::getVersion
79190
*/
@@ -356,105 +467,6 @@ public function testDoQuery()
356467
}
357468
}
358469

359-
/**
360-
* @covers ierusalim\ClickHouse\ClickHouseAPI::doApiCall
361-
* @todo Implement testDoApiCall().
362-
*/
363-
public function testDoApiCall()
364-
{
365-
$ch = $this->object;
366-
367-
$slot = "tmp1";
368-
$ans = $ch->toSlot($slot)->doApiCall(false, ['query'=>'SELECT 123']);
369-
$this->assertEquals(102, $ans['code']);
370-
371-
$ch->debug = true;
372-
$ch->hook_before_api_call = function ($url, $obj) {
373-
return "https://ierusalim.github.io";
374-
};
375-
376-
$file = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . '.gitignore';
377-
378-
$ans = $ch->doApiCall("empty", [], true, [], $file);
379-
$this->assertEquals($ans['code'], 405);
380-
381-
$this->assertEquals("123\n", $ch->fromSlot($slot)->results);
382-
}
383-
384-
/**
385-
* @covers ierusalim\ClickHouse\ClickHouseAPI::yiDoApiCall
386-
* @todo Implement testYiDoApiCall().
387-
*/
388-
public function testYiDoApiCall()
389-
{
390-
$ch = $this->object;
391-
392-
if ($ch->isSupported('query')) {
393-
$table = "anytabletmp";
394-
395-
$file = 'anyfile.txt';
396-
397-
$file_data = '';
398-
for ($t=1; $t<100; $t++) {
399-
$file_data .= $t . "\t2017-12-12\tAny string data\n";
400-
}
401-
402-
$file_size = file_put_contents($file, $file_data);
403-
404-
$this->assertTrue($file_size > 0);
405-
406-
$fields = '(id, dt, s)';
407-
$structure_excactly = 'id UInt32, dt Date, s String';
408-
409-
$ch->query("DROP TABLE IF EXISTS $table")
410-
411-
->query("CREATE TABLE $table" .
412-
"( $structure_excactly )" .
413-
"ENGINE = MergeTree(dt, (id, dt), 8192)");
414-
415-
$ch->is_windows = true;
416-
417-
$ans = $ch->doApiCall(false,
418-
['query' => "INSERT INTO $table $fields FORMAT TabSeparated"],
419-
true, [], $file, true);
420-
421-
$ch->query("SELECT * FROM $table");
422-
$this->assertEquals($file_data, $ch->results);
423-
424-
$ch->is_windows = false;
425-
426-
try {
427-
$ans = $ch->doApiCall(false,
428-
['query' => "INSERT INTO $table $fields FORMAT TabSeparated"],
429-
true, [], $file, true);
430-
} catch (\Exception $e) {
431-
echo $e->getMessage();
432-
\fclose($ch->fh);
433-
}
434-
435-
436-
$ch->query("DROP TABLE IF EXISTS $table");
437-
unlink($file);
438-
unlink($file . '.gz');
439-
$slot = "tmp1";
440-
$ans = $ch->toSlot($slot)->doApiCall(false, ['query'=>'SELECT 123']);
441-
$this->assertEquals(102, $ans['code']);
442-
443-
$ch->debug = true;
444-
445-
$ch->hook_before_api_call = function ($url, $obj) {
446-
return "https://ierusalim.github.io";
447-
};
448-
449-
$file = dirname(dirname(__DIR__)) . \DIRECTORY_SEPARATOR . '.gitignore';
450-
451-
$ans = $ch->doApiCall("empty", [], true, [], $file);
452-
$this->assertEquals($ans['code'], 405);
453-
454-
$this->assertEquals("123\n", $ch->fromSlot($slot)->results);
455-
}
456-
}
457-
458470
/**
459471
* @covers ierusalim\ClickHouse\ClickHouseAPI::setOption
460472
* @todo Implement testSetOption().

0 commit comments

Comments
 (0)