11<?php
2+
23/**
34 * Copyright 2016 Google Inc.
45 *
2122use Google \Cloud \BigQuery \Job ;
2223use Google \Cloud \BigQuery \Numeric ;
2324use Google \Cloud \BigQuery \QueryResults ;
25+ use Google \Cloud \BigQuery \Timestamp ;
2426use Google \Cloud \BigQuery \ValueMapper ;
2527use Google \Cloud \Core \Testing \TestHelpers ;
2628use PHPUnit \Framework \TestCase ;
@@ -41,7 +43,7 @@ class QueryResultsTest extends TestCase
4143 'jobComplete ' => true ,
4244 'jobReference ' => ['location ' => 123 ],
4345 'rows ' => [
44- ['f ' => [['v ' => 'Alton ' ], ['v ' => 1 ]]]
46+ ['f ' => [['v ' => 'Alton ' ], ['v ' => 1 ], [ ' v ' => ' 40969200000000 ' ] ]]
4547 ],
4648 'schema ' => [
4749 'fields ' => [
@@ -53,6 +55,10 @@ class QueryResultsTest extends TestCase
5355 'name ' => 'numeric_value ' ,
5456 'type ' => 'NUMERIC '
5557 ],
58+ [
59+ 'name ' => 'timestamp ' ,
60+ 'type ' => 'TIMESTAMP '
61+ ]
5662 ]
5763 ]
5864 ];
@@ -96,6 +102,28 @@ public function testGetsRowsWithoutToken()
96102
97103 public function testGetsRowsWithToken ()
98104 {
105+ $ this ->connection ->getQueryResults (Argument::allOf (
106+ Argument::withEntry ('projectId ' , $ this ->projectId ),
107+ Argument::withEntry ('jobId ' , $ this ->jobId ),
108+ Argument::withEntry ('formatOptions.useInt64Timestamp ' , true )
109+ ))
110+ ->willReturn ($ this ->queryData )
111+ ->shouldBeCalledTimes (1 );
112+
113+ $ queryResults = $ this ->getQueryResults (
114+ $ this ->connection ,
115+ $ this ->queryData + ['pageToken ' => 'abcd ' ]
116+ );
117+ $ rows = iterator_to_array ($ queryResults ->rows (['formatOptions.useInt64Timestamp ' => true ]));
118+
119+ $ this ->assertEquals ('Alton ' , $ rows [1 ]['first_name ' ]);
120+ $ this ->assertInstanceOf (Numeric::class, $ rows [1 ]['numeric_value ' ]);
121+ $ this ->assertEquals (new Timestamp (new \DateTime ('1971-04-20T04:20:00.000000Z ' )), $ rows [1 ]['timestamp ' ]);
122+ }
123+
124+ public function testGetsRowWithFloatTimestamp ()
125+ {
126+ $ this ->queryData ['rows ' ][0 ]['f ' ][2 ]['v ' ] = '1.438712914E9 ' ;
99127 $ this ->connection ->getQueryResults (Argument::allOf (
100128 Argument::withEntry ('projectId ' , $ this ->projectId ),
101129 Argument::withEntry ('jobId ' , $ this ->jobId )
@@ -111,6 +139,7 @@ public function testGetsRowsWithToken()
111139
112140 $ this ->assertEquals ('Alton ' , $ rows [1 ]['first_name ' ]);
113141 $ this ->assertInstanceOf (Numeric::class, $ rows [1 ]['numeric_value ' ]);
142+ $ this ->assertEquals (new Timestamp (new \DateTime ('2015-08-04 18:28:34Z ' )), $ rows [1 ]['timestamp ' ]);
114143 }
115144
116145 public function testReturnRawResults ()
@@ -123,6 +152,7 @@ public function testReturnRawResults()
123152
124153 $ this ->assertEquals ('Alton ' , $ rows [0 ]['first_name ' ]);
125154 $ this ->assertEquals (1 , $ rows [0 ]['numeric_value ' ]);
155+ $ this ->assertEquals ('40969200000000 ' , $ rows [0 ]['timestamp ' ]);
126156 }
127157
128158 public function testReturnRawResultsIsFalse ()
0 commit comments