From dc17e447717672691eb59bd4bbf65ac3a9a9a8d7 Mon Sep 17 00:00:00 2001 From: castaneai Date: Thu, 13 Jun 2019 11:38:57 +0900 Subject: [PATCH] Fix connection event order --- src/Colopl/Spanner/Connection.php | 31 ++++++++++++++++--------- tests/Colopl/Spanner/ConnectionTest.php | 20 ++++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/Colopl/Spanner/Connection.php b/src/Colopl/Spanner/Connection.php index 39c930bf..7795397f 100644 --- a/src/Colopl/Spanner/Connection.php +++ b/src/Colopl/Spanner/Connection.php @@ -306,21 +306,30 @@ public function statement($query, $bindings = []): bool */ public function affectingStatement($query, $bindings = []): int { - return $this->run($query, $bindings, function ($query, $bindings) { - if ($this->pretending()) { - return 0; - } + $body = function () use ($query, $bindings) { + return $this->run($query, $bindings, function ($query, $bindings) { + if ($this->pretending()) { + return 0; + } - $queryCall = function() use ($query, $bindings) { - return $this->getCurrentTransaction()->executeUpdate($query, ['parameters' => $this->prepareBindings($bindings)]); - }; + $queryCall = function () use ($query, $bindings) { + return $this->getCurrentTransaction()->executeUpdate($query, ['parameters' => $this->prepareBindings($bindings)]); + }; - $rowCount = $this->inTransaction() ? $queryCall() : $this->transaction($queryCall); + $rowCount = $queryCall(); - $this->recordsHaveBeenModified($rowCount > 0); + $this->recordsHaveBeenModified($rowCount > 0); - return $rowCount; - }); + return $rowCount; + }); + }; + + if ($this->inTransaction()) { + return $body(); + } + + // Create a temporary transaction for single affecting statement + return $this->transaction($body); } /** diff --git a/tests/Colopl/Spanner/ConnectionTest.php b/tests/Colopl/Spanner/ConnectionTest.php index 5f750dec..6dcca48d 100644 --- a/tests/Colopl/Spanner/ConnectionTest.php +++ b/tests/Colopl/Spanner/ConnectionTest.php @@ -342,4 +342,24 @@ public function testStaleReads() $this->assertEquals($uuid, $row['userId']); $this->assertEquals('first', $row['name']); } + + public function testEventListenOrder() + { + $receivedEventClasses = []; + $this->app['events']->listen(TransactionBeginning::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = TransactionBeginning::class; }); + $this->app['events']->listen(QueryExecuted::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = QueryExecuted::class; }); + $this->app['events']->listen(TransactionCommitted::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = TransactionCommitted::class; }); + + $conn = $this->getDefaultConnection(); + + $tableName = self::TABLE_NAME_USER; + $uuid = $this->generateUuid(); + $name = 'test'; + $conn->insert("INSERT INTO ${tableName} (`userId`, `name`) VALUES ('${uuid}', '${name}')"); + + $this->assertCount(3, $receivedEventClasses); + $this->assertEquals(TransactionBeginning::class, $receivedEventClasses[0]); + $this->assertEquals(QueryExecuted::class, $receivedEventClasses[1]); + $this->assertEquals(TransactionCommitted::class, $receivedEventClasses[2]); + } }