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