26
26
use Colopl \Spanner \TimestampBound \MinReadTimestamp ;
27
27
use Colopl \Spanner \TimestampBound \ReadTimestamp ;
28
28
use Colopl \Spanner \TimestampBound \StrongRead ;
29
+ use Generator ;
29
30
use Google \Auth \FetchAuthTokenInterface ;
30
31
use Google \Cloud \Core \Exception \AbortedException ;
31
32
use Google \Cloud \Core \Exception \NotFoundException ;
33
+ use Google \Cloud \Spanner \Duration ;
32
34
use Google \Cloud \Spanner \KeySet ;
33
35
use Google \Cloud \Spanner \Session \CacheSessionPool ;
34
36
use Google \Cloud \Spanner \SpannerClient ;
39
41
use Illuminate \Database \Events \TransactionCommitted ;
40
42
use Illuminate \Support \Carbon ;
41
43
use Illuminate \Support \Facades \Event ;
42
- use LogicException ;
43
44
use RuntimeException ;
44
45
use Symfony \Component \Cache \Adapter \ArrayAdapter ;
45
46
use function dirname ;
@@ -61,7 +62,7 @@ public function testReconnect(): void
61
62
{
62
63
$ conn = $ this ->getDefaultConnection ();
63
64
$ conn ->reconnect ();
64
- $ this ->assertEquals ([12345 ], $ conn ->selectOne ('SELECT 12345 ' ));
65
+ $ this ->assertSame ([12345 ], $ conn ->selectOne ('SELECT 12345 ' ));
65
66
}
66
67
67
68
public function testQueryLog (): void
@@ -76,6 +77,107 @@ public function testQueryLog(): void
76
77
$ this ->assertCount (2 , $ conn ->getQueryLog ());
77
78
}
78
79
80
+ public function test_select (): void
81
+ {
82
+ $ conn = $ this ->getDefaultConnection ();
83
+ $ values = $ conn ->select ('SELECT 12345 ' );
84
+ $ this ->assertCount (1 , $ values );
85
+ $ this ->assertSame (12345 , $ values [0 ][0 ]);
86
+ }
87
+
88
+ public function test_selectWithOptions (): void
89
+ {
90
+ $ conn = $ this ->getDefaultConnection ();
91
+ $ conn ->table (self ::TABLE_NAME_USER )->insert (['userId ' => $ this ->generateUuid (), 'name ' => __FUNCTION__ ]);
92
+ $ values = $ conn ->selectWithOptions ('SELECT * FROM ' . self ::TABLE_NAME_USER , [], ['exactStaleness ' => new Duration (10 )]);
93
+ $ this ->assertEmpty ($ values );
94
+ }
95
+
96
+ public function test_cursorWithOptions (): void
97
+ {
98
+ $ conn = $ this ->getDefaultConnection ();
99
+ $ conn ->table (self ::TABLE_NAME_USER )->insert (['userId ' => $ this ->generateUuid (), 'name ' => __FUNCTION__ ]);
100
+ $ cursor = $ conn ->cursorWithOptions ('SELECT * FROM ' . self ::TABLE_NAME_USER , [], ['exactStaleness ' => new Duration (10 )]);
101
+ $ this ->assertInstanceOf (Generator::class, $ cursor );
102
+ $ this ->assertNull ($ cursor ->current ());
103
+ }
104
+
105
+ public function test_statement_with_select (): void
106
+ {
107
+ $ executedCount = 0 ;
108
+ $ this ->app ['events ' ]->listen (QueryExecuted::class, function () use (&$ executedCount ) { $ executedCount ++; });
109
+
110
+ $ conn = $ this ->getDefaultConnection ();
111
+ $ res = $ conn ->statement ('SELECT ? ' , ['12345 ' ]);
112
+
113
+ $ this ->assertTrue ($ res );
114
+ $ this ->assertSame (1 , $ executedCount );
115
+ }
116
+
117
+ public function test_statement_with_dml (): void
118
+ {
119
+ $ conn = $ this ->getDefaultConnection ();
120
+ $ userId = $ this ->generateUuid ();
121
+ $ executedCount = 0 ;
122
+ $ this ->app ['events ' ]->listen (QueryExecuted::class, function () use (&$ executedCount ) { $ executedCount ++; });
123
+
124
+ $ res [] = $ conn ->statement ('INSERT ' .self ::TABLE_NAME_USER .' (`userId`, `name`) VALUES (?,?) ' , [$ userId , __FUNCTION__ ]);
125
+ $ res [] = $ conn ->statement ('UPDATE ' .self ::TABLE_NAME_USER .' SET `name`=? WHERE `userId`=? ' , [__FUNCTION__ .'2 ' , $ userId ]);
126
+ $ res [] = $ conn ->statement ('DELETE ' .self ::TABLE_NAME_USER .' WHERE `userId`=? ' , [$ this ->generateUuid ()]);
127
+
128
+ $ this ->assertTrue ($ res [0 ]);
129
+ $ this ->assertTrue ($ res [1 ]);
130
+ $ this ->assertTrue ($ res [2 ]);
131
+ $ this ->assertSame (3 , $ executedCount );
132
+ }
133
+
134
+ public function test_unprepared_with_select (): void
135
+ {
136
+ $ executedCount = 0 ;
137
+ $ this ->app ['events ' ]->listen (QueryExecuted::class, function () use (&$ executedCount ) { $ executedCount ++; });
138
+
139
+ $ conn = $ this ->getDefaultConnection ();
140
+ $ res = $ conn ->unprepared ('SELECT 12345 ' );
141
+
142
+ $ this ->assertTrue ($ res );
143
+ $ this ->assertSame (1 , $ executedCount );
144
+ }
145
+
146
+ public function test_unprepared_with_dml (): void
147
+ {
148
+ $ conn = $ this ->getDefaultConnection ();
149
+ $ userId = $ this ->generateUuid ();
150
+ $ executedCount = 0 ;
151
+ $ this ->app ['events ' ]->listen (QueryExecuted::class, function () use (&$ executedCount ) { $ executedCount ++; });
152
+
153
+ $ res [] = $ conn ->unprepared ('INSERT ' .self ::TABLE_NAME_USER .' (`userId`, `name`) VALUES ( \'' .$ userId .'\', \'' .__FUNCTION__ .'\') ' );
154
+ $ res [] = $ conn ->unprepared ('UPDATE ' .self ::TABLE_NAME_USER .' SET `name`= \'' .__FUNCTION__ .'2 ' .'\' WHERE `userId`= \'' .$ userId .'\'' );
155
+ $ res [] = $ conn ->unprepared ('DELETE ' .self ::TABLE_NAME_USER .' WHERE `userId`= \'' .$ userId .'\'' );
156
+
157
+ $ this ->assertTrue ($ res [0 ]);
158
+ $ this ->assertTrue ($ res [1 ]);
159
+ $ this ->assertTrue ($ res [2 ]);
160
+ $ this ->assertSame (3 , $ executedCount );
161
+ }
162
+
163
+ public function test_pretend (): void
164
+ {
165
+ $ executedCount = 0 ;
166
+ $ this ->app ['events ' ]->listen (QueryExecuted::class, function () use (&$ executedCount ) { $ executedCount ++; });
167
+
168
+ $ resSelect = null ;
169
+ $ resInsert = null ;
170
+ $ conn = $ this ->getDefaultConnection ();
171
+ $ conn ->pretend (function (Connection $ conn ) use (&$ resSelect , &$ resInsert ) {
172
+ $ resSelect = $ conn ->select ('SELECT 12345 ' );
173
+ $ resInsert = $ conn ->table (self ::TABLE_NAME_USER )->insert (['userId ' => $ this ->generateUuid (), 'name ' => __FUNCTION__ ]);
174
+ });
175
+
176
+ $ this ->assertSame ([], $ resSelect );
177
+ $ this ->assertTrue ($ resInsert );
178
+ $ this ->assertSame (2 , $ executedCount );
179
+ }
180
+
79
181
public function testInsertUsingMutationWithTransaction (): void
80
182
{
81
183
Event::fake ();
0 commit comments