From 017e198228e96bd1ebe1893a8b08edaaf0bda64e Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Thu, 25 Jan 2024 10:42:07 +0900 Subject: [PATCH] feat: Allow pretending for DDL statements (#170) --- CHANGELOG.md | 1 + src/Concerns/ManagesDataDefinitions.php | 9 +++++--- tests/Concerns/ManagesDataDefinitionsTest.php | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19d34496..e9b7c454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Added - `Schema\Grammar::compileDropForeign` to allow dropping foreign key constraints (#163) - `Schema\Builder::dropAllTables` works properly, dropping foreign keys, indexes, then tables in order of interleaving (#161) - Support for inserting and selecting array of DateTime/Numeric objects (#168) +- Allow pretending for DDL statements (#170) Changed - `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156) diff --git a/src/Concerns/ManagesDataDefinitions.php b/src/Concerns/ManagesDataDefinitions.php index 333f1859..7efd8f76 100644 --- a/src/Concerns/ManagesDataDefinitions.php +++ b/src/Concerns/ManagesDataDefinitions.php @@ -63,10 +63,13 @@ public function runDdlBatch(array $statements): mixed } $start = microtime(true); + $result = []; - $result = $this->waitForOperation( - $this->getSpannerDatabase()->updateDdlBatch($statements), - ); + if (!$this->pretending()) { + $result = $this->waitForOperation( + $this->getSpannerDatabase()->updateDdlBatch($statements), + ); + } foreach ($statements as $statement) { $this->logQuery($statement, [], $this->getElapsedTime($start)); diff --git a/tests/Concerns/ManagesDataDefinitionsTest.php b/tests/Concerns/ManagesDataDefinitionsTest.php index 0a512fb3..6c4de413 100644 --- a/tests/Concerns/ManagesDataDefinitionsTest.php +++ b/tests/Concerns/ManagesDataDefinitionsTest.php @@ -38,8 +38,30 @@ public function test_runDdlBatch(): void $this->assertSame([], $result); $this->assertSame($statement, $conn->getQueryLog()[0]['query']); $this->assertCount(1, $conn->getQueryLog()); + Event::assertDispatchedTimes(QueryExecuted::class, 1); + $this->assertContains($newTable, array_map(fn ($d) => $d['name'], $conn->getSchemaBuilder()->getTables())); + } + + public function test_runDdlBatch_within_pretend(): void + { + $conn = $this->getDefaultConnection(); + $conn->setEventDispatcher(Event::fake([QueryExecuted::class])); + $conn->enableQueryLog(); + + $newTable = $this->generateTableName('runDdlBatch'); + $statement = "create table {$newTable} (id int64) primary key (id)"; + + $result = null; + $conn->pretend(function (Connection $conn) use (&$result, $statement) { + $result = $conn->runDdlBatch([$statement]); + }); + + $this->assertSame([], $result); + $this->assertSame([['query' => $statement, 'bindings' => [], 'time' => 0.0]], $conn->getQueryLog()); Event::assertDispatchedTimes(QueryExecuted::class, 1); + + $this->assertFalse($conn->getSchemaBuilder()->hasTable($newTable)); } public function test_runDdlBatch_with_empty_statement(): void