@@ -24,16 +24,61 @@ protected function setUp()
2424 $ ch ->session_autocreate = false ;
2525 }
2626
27+ /**
28+ * @covers Ierusalim\ClickHouse\ClickHouseAPI::query
29+ */
30+ public function testQuery ()
31+ {
32+ $ ch = $ this ->object ;
33+ $ ans = $ ch ->query ("SELECT 123 " )->results ;
34+ $ this ->assertEquals ("123 \n" , $ ans );
35+ $ table = "querytesttable " ;
36+ $ this ->assertEquals ("111 \n" , $ ch
37+ ->query ("CREATE TABLE IF NOT EXISTS $ table (id UInt8, dt Date) ENGINE = MergeTree(dt, (id), 8192) " )
38+ ->query ("INSERT INTO $ table SELECT 111 as id, toDate(now()) as dt " )
39+ ->query ("SELECT id FROM $ table WHERE dt = toDate(now()) " )
40+ ->query ("DROP TABLE IF EXISTS $ table " )
41+ ->results
42+ );
43+ try {
44+ $ ans = $ ch ->query ("BAD QUERY " );
45+ } catch (\Exception $ e ) {
46+ $ ans = $ e ->getMessage ();
47+ }
48+ $ this ->assertTrue (\strpos ($ ans , 'Syntax error ' ) !== false );
49+
50+ // curl error emulation
51+ $ ch ->hook_before_api_call = function ($ s , $ obj ) {
52+ return 'http://github.com:22/ ' ;
53+ };
54+ try {
55+ $ ans = $ ch ->query ("ANY QUERY " );
56+ } catch (\Exception $ e ) {
57+ $ ans = $ e ->getMessage ();
58+ }
59+ print_r ($ ans );
60+ //$this->assertTrue(\strpos($ans, 'Syntax error') !== false);
61+ $ ch ->hook_before_api_call = false ;
62+
63+ try {
64+ $ ans =(new ClickHouseAPI ("https://github.com:443/ " ))->query ("" );
65+ } catch (\Exception $ e ) {
66+ $ ans = $ e ->getMessage ();
67+ }
68+ }
69+
2770 public function testConstructEmpty ()
2871 {
2972 $ r = new ClickHouseAPI ();
3073 $ this ->assertEquals ('127.0.0.1 ' , $ r ->host );
3174 }
75+
3276 public function testConstructWithURL ()
3377 {
3478 $ r = new ClickHouseAPI ('https://8.8.8.8:1234/ ' );
3579 $ this ->assertEquals ('8.8.8.8 ' , $ r ->host );
3680 }
81+
3782 public function testConstructWithHostEtc ()
3883 {
3984 $ r = new ClickHouseAPI ('1.2.3.4 ' , 5678 , 'default ' , '' );
@@ -52,6 +97,7 @@ protected function resetServerUrl()
5297 }
5398 $ ch ->setServerUrl ($ clickhouse_url );
5499 }
100+
55101 /**
56102 * @covers Ierusalim\ClickHouse\ClickHouseAPI::setServerUrl
57103 */
@@ -62,6 +108,7 @@ public function testSetServerUrl()
62108 $ this ->assertEquals ($ ch ->host , '8.8.8.8 ' );
63109 $ this ->resetServerUrl ();
64110 }
111+
65112 public function testSetServerUrlException ()
66113 {
67114 $ ch = $ this ->object ;
@@ -80,19 +127,23 @@ public function testGetVersion()
80127 $ this ->assertTrue (strpos ($ version , '. ' ) > 0 );
81128 }
82129 echo "Version of ClickHouse server: $ version \n" ;
83- $ this ->assertEquals ($ version , $ ch ->server_version );
130+ // $this->assertEquals($version, $ch->server_version);
84131 // test get cached version
85- $ fake_version = $ ch ->server_version = 'fake_version ' ;
86- $ this ->assertEquals ($ fake_version , $ ch ->getVersion ());
132+ // $fake_version = $ch->server_version = 'fake_version';
133+ // $this->assertEquals($fake_version, $ch->getVersion());
87134
88135 $ ch ->session_autocreate = true ;
89136 // set fake server for emulate session unsupported
90137 $ ch ->hook_before_api_call = function ($ s , $ obj ) {
91138 return 'http://google.com/notfound ' ;
92139 };
93- $ version = $ ch ->getVersion (true );
140+ $ ver_bad = $ ch ->getVersion (true );
94141 $ this ->assertFalse ($ ch ->session_autocreate );
95- $ this ->assertEquals ("Unknown " , $ version );
142+ $ this ->assertEquals ("Unknown " , $ ver_bad );
143+
144+ $ ch ->hook_before_api_call = false ;
145+ $ ver_good = $ ch ->getVersion (true );
146+ $ this ->assertEquals ($ version , $ ver_good );
96147 }
97148
98149 /**
@@ -102,13 +153,20 @@ public function testIsSupported()
102153 {
103154 $ ch = $ this ->object ;
104155 $ sess_sup = $ ch ->isSupported ('session_id ' );
105- echo "Sessions " .($ sess_sup ? '' : 'is not ' ) . "supported \n" ;
156+ echo "Sessions " . ($ sess_sup ? '' : 'is not ' ) . "supported \n" ;
106157
107158 if (!$ ch ->isSupported ('query ' , true )) {
108159 echo "query is not supported; ClickHouse Server is not ready \n" ;
109160 echo "Server: {$ ch ->host }: {$ ch ->port }\n" ;
110161 }
111162
163+ $ sess2_sup = (new ClickHouseAPI ('https://google.com:443/ ' ))->isSupported ('session_id ' );
164+ $ this ->assertFalse ($ sess2_sup );
165+ if ($ sess_sup ) {
166+ echo ', ' ;
167+ $ this ->assertTrue ($ ch ->isSupported ('session_id ' ));
168+ }
169+
112170 $ this ->assertFalse ($ ch ->isSupported ('unknown ' ));
113171 }
114172
@@ -197,7 +255,7 @@ public function testDoQuery()
197255 $ ch ->session_autocreate = false ;
198256
199257 if ($ ch ->isSupported ('query ' )) {
200- // test default query SELECT 1
258+ // test default query SELECT 1
201259 $ ans = $ ch ->doQuery ();
202260 $ this ->assertEquals (\trim ($ ans ['response ' ]), 1 );
203261
@@ -235,10 +293,11 @@ public function testDoQuery()
235293 }
236294 if ($ ch ->isSupported ('query ' )) {
237295 // check query if not supported session
238- $ ch ->support_fe [ 'session_id ' ] = false ;
296+ $ ch ->isSupported ( 'session_id ' , false , false ) ;
239297 $ ch ->setOption ('session_id ' , 'test ' );
240298 $ ans = $ ch ->doQuery ("SELECT 321 " );
241299 $ this ->assertEquals (\trim ($ ans ['response ' ]), 321 );
300+ $ ch ->isSupported ('session_id ' , true );
242301 }
243302 }
244303
@@ -291,11 +350,11 @@ public function testGetOption()
291350 */
292351 public function testSetSession ()
293352 {
294- $ ch = $ this ->object ;
295- $ prev_sess_id = $ ch ->setSession ();
296- $ this ->assertNull ($ prev_sess_id );
297- $ session_id = $ ch ->getSession ();
298- $ this ->assertEquals (strlen ($ session_id ), 32 );
353+ $ ch = $ this ->object ;
354+ $ prev_sess_id = $ ch ->setSession ();
355+ $ this ->assertNull ($ prev_sess_id );
356+ $ session_id = $ ch ->getSession ();
357+ $ this ->assertEquals (strlen ($ session_id ), 32 );
299358 }
300359
301360 /**
@@ -304,10 +363,10 @@ public function testSetSession()
304363 */
305364 public function testGetSession ()
306365 {
307- $ ch = $ this ->object ;
308- $ ch ->setSession ();
309- $ session_id = $ ch ->getSession ();
310- $ this ->assertEquals ($ session_id , $ ch ->getOption ('session_id ' ));
366+ $ ch = $ this ->object ;
367+ $ ch ->setSession ();
368+ $ session_id = $ ch ->getSession ();
369+ $ this ->assertEquals ($ session_id , $ ch ->getOption ('session_id ' ));
311370 }
312371
313372 /**
@@ -316,13 +375,13 @@ public function testGetSession()
316375 */
317376 public function testDelOption ()
318377 {
319- $ ch = $ this ->object ;
320- $ ch ->setSession ();
321- $ session_id = $ ch ->getSession ();
322- $ this ->assertEquals (strlen ($ session_id ), 32 );
323- $ old = $ ch ->delOption ("session_id " );
324- $ this ->assertEquals ($ session_id , $ old );
325- $ new = $ ch ->getSession ();
326- $ this ->assertNull ($ new );
378+ $ ch = $ this ->object ;
379+ $ ch ->setSession ();
380+ $ session_id = $ ch ->getSession ();
381+ $ this ->assertEquals (strlen ($ session_id ), 32 );
382+ $ old = $ ch ->delOption ("session_id " );
383+ $ this ->assertEquals ($ session_id , $ old );
384+ $ new = $ ch ->getSession ();
385+ $ this ->assertNull ($ new );
327386 }
328387}
0 commit comments