From 5ee2b8966f2d2f19c1eac3599dae375146769ca5 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 18 Jan 2020 05:03:49 +0100 Subject: [PATCH] introduce query value object to wrap sql queries --- lib/Doctrine/Migrations/AbstractMigration.php | 8 +- lib/Doctrine/Migrations/DbalMigrator.php | 7 +- lib/Doctrine/Migrations/FileQueryWriter.php | 3 +- .../Generator/ConcatenationFileBuilder.php | 7 +- .../Migrations/Generator/FileBuilder.php | 3 +- lib/Doctrine/Migrations/Migrator.php | 5 +- lib/Doctrine/Migrations/Query/Query.php | 53 ++++++++++++ lib/Doctrine/Migrations/QueryWriter.php | 4 +- .../Migrations/Version/DbalExecutor.php | 84 +++++-------------- .../Migrations/Version/ExecutionResult.php | 37 ++------ lib/Doctrine/Migrations/Version/Executor.php | 7 +- .../Tests/AbstractMigrationTest.php | 3 +- .../Migrations/Tests/FileQueryWriterTest.php | 5 +- .../ConcatenationFileBuilderTest.php | 25 +++--- .../Migrations/Tests/MigratorTest.php | 20 +++-- .../Migrations/Tests/Query/QueryTest.php | 21 +++++ .../Tests/Version/ExecutionResultTest.php | 27 +++--- .../Migrations/Tests/Version/ExecutorTest.php | 35 +++++--- 18 files changed, 195 insertions(+), 159 deletions(-) create mode 100644 lib/Doctrine/Migrations/Query/Query.php create mode 100644 tests/Doctrine/Migrations/Tests/Query/QueryTest.php diff --git a/lib/Doctrine/Migrations/AbstractMigration.php b/lib/Doctrine/Migrations/AbstractMigration.php index 380f5467ba..957bbcf094 100644 --- a/lib/Doctrine/Migrations/AbstractMigration.php +++ b/lib/Doctrine/Migrations/AbstractMigration.php @@ -11,8 +11,8 @@ use Doctrine\Migrations\Exception\AbortMigration; use Doctrine\Migrations\Exception\IrreversibleMigration; use Doctrine\Migrations\Exception\SkipMigration; +use Doctrine\Migrations\Query\Query; use Psr\Log\LoggerInterface; -use function func_get_args; /** * The AbstractMigration class is for end users to extend from when creating migrations. Extend this class @@ -32,7 +32,7 @@ abstract class AbstractMigration /** @var LoggerInterface */ private $logger; - /** @var mixed[] */ + /** @var Query[] */ private $plannedSql = []; public function __construct(Connection $connection, LoggerInterface $logger) @@ -122,11 +122,11 @@ protected function addSql( array $params = [], array $types = [] ) : void { - $this->plannedSql[] = func_get_args(); + $this->plannedSql[] = new Query($sql, $params, $types); } /** - * @return mixed[] + * @return Query[] */ public function getSql() : array { diff --git a/lib/Doctrine/Migrations/DbalMigrator.php b/lib/Doctrine/Migrations/DbalMigrator.php index c884f061f6..c2cef468c4 100644 --- a/lib/Doctrine/Migrations/DbalMigrator.php +++ b/lib/Doctrine/Migrations/DbalMigrator.php @@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection; use Doctrine\Migrations\Metadata\MigrationPlanList; +use Doctrine\Migrations\Query\Query; use Doctrine\Migrations\Tools\BytesFormatter; use Doctrine\Migrations\Version\Executor; use Psr\Log\LoggerInterface; @@ -51,7 +52,7 @@ public function __construct( } /** - * @return string[][] + * @return array */ private function executeMigrations( MigrationPlanList $migrationsPlan, @@ -85,7 +86,7 @@ private function executeMigrations( } /** - * @return array + * @return array */ private function executePlan(MigrationPlanList $migrationsPlan, MigratorConfiguration $migratorConfiguration) : array { @@ -110,7 +111,7 @@ private function executePlan(MigrationPlanList $migrationsPlan, MigratorConfigur } /** - * @param string[][] $sql + * @param array $sql */ private function endMigrations( StopwatchEvent $stopwatchEvent, diff --git a/lib/Doctrine/Migrations/FileQueryWriter.php b/lib/Doctrine/Migrations/FileQueryWriter.php index 3c06650ab6..df089d85c9 100644 --- a/lib/Doctrine/Migrations/FileQueryWriter.php +++ b/lib/Doctrine/Migrations/FileQueryWriter.php @@ -7,6 +7,7 @@ use DateTimeImmutable; use DateTimeInterface; use Doctrine\Migrations\Generator\FileBuilder; +use Doctrine\Migrations\Query\Query; use Psr\Log\LoggerInterface; use function file_put_contents; use function is_dir; @@ -34,7 +35,7 @@ public function __construct( } /** - * @param mixed[] $queriesByVersion + * @param array $queriesByVersion */ public function write( string $path, diff --git a/lib/Doctrine/Migrations/Generator/ConcatenationFileBuilder.php b/lib/Doctrine/Migrations/Generator/ConcatenationFileBuilder.php index 8bbeca6b0c..b5d977be0f 100644 --- a/lib/Doctrine/Migrations/Generator/ConcatenationFileBuilder.php +++ b/lib/Doctrine/Migrations/Generator/ConcatenationFileBuilder.php @@ -6,6 +6,7 @@ use DateTimeImmutable; use DateTimeInterface; +use Doctrine\Migrations\Query\Query; use function sprintf; /** @@ -15,7 +16,7 @@ */ final class ConcatenationFileBuilder implements FileBuilder { - /** @param string[][] $queriesByVersion */ + /** @param array $queriesByVersion */ public function buildMigrationFile( array $queriesByVersion, string $direction, @@ -25,12 +26,10 @@ public function buildMigrationFile( $string = sprintf("-- Doctrine Migration File Generated on %s\n", $now->format('Y-m-d H:i:s')); foreach ($queriesByVersion as $version => $queries) { - $version = (string) $version; - $string .= "\n-- Version " . $version . "\n"; foreach ($queries as $query) { - $string .= $query . ";\n"; + $string .= $query->getStatement() . ";\n"; } } diff --git a/lib/Doctrine/Migrations/Generator/FileBuilder.php b/lib/Doctrine/Migrations/Generator/FileBuilder.php index 917315983a..71a5368794 100644 --- a/lib/Doctrine/Migrations/Generator/FileBuilder.php +++ b/lib/Doctrine/Migrations/Generator/FileBuilder.php @@ -5,6 +5,7 @@ namespace Doctrine\Migrations\Generator; use DateTimeInterface; +use Doctrine\Migrations\Query\Query; /** * The ConcatenationFileBuilder class is responsible for building a migration SQL file from an array of queries per version. @@ -13,6 +14,6 @@ */ interface FileBuilder { - /** @param string[][] $queriesByVersion */ + /** @param array $queriesByVersion */ public function buildMigrationFile(array $queriesByVersion, string $direction, ?DateTimeInterface $now = null) : string; } diff --git a/lib/Doctrine/Migrations/Migrator.php b/lib/Doctrine/Migrations/Migrator.php index 092d0b6069..c3cbf617a7 100644 --- a/lib/Doctrine/Migrations/Migrator.php +++ b/lib/Doctrine/Migrations/Migrator.php @@ -5,16 +5,17 @@ namespace Doctrine\Migrations; use Doctrine\Migrations\Metadata\MigrationPlanList; +use Doctrine\Migrations\Query\Query; /** - * The DbalMigrator class is responsible for generating and executing the SQL for a migration. + * The Migrator interface is responsible for generating and executing the SQL for a migration. * * @internal */ interface Migrator { /** - * @return array A list of SQL statements executed, grouped by migration version + * @return array A list of SQL statements executed, grouped by migration version */ public function migrate(MigrationPlanList $migrationsPlan, MigratorConfiguration $migratorConfiguration) : array; } diff --git a/lib/Doctrine/Migrations/Query/Query.php b/lib/Doctrine/Migrations/Query/Query.php new file mode 100644 index 0000000000..e577979123 --- /dev/null +++ b/lib/Doctrine/Migrations/Query/Query.php @@ -0,0 +1,53 @@ +statement = $statement; + $this->parameters = $parameters; + $this->types = $types; + } + + public function __toString() : string + { + return $this->statement; + } + + public function getStatement() : string + { + return $this->statement; + } + + /** @return mixed[] */ + public function getParameters() : array + { + return $this->parameters; + } + + /** @return mixed[] */ + public function getTypes() : array + { + return $this->types; + } +} diff --git a/lib/Doctrine/Migrations/QueryWriter.php b/lib/Doctrine/Migrations/QueryWriter.php index 501f772051..6b75668c42 100644 --- a/lib/Doctrine/Migrations/QueryWriter.php +++ b/lib/Doctrine/Migrations/QueryWriter.php @@ -4,6 +4,8 @@ namespace Doctrine\Migrations; +use Doctrine\Migrations\Query\Query; + /** * The QueryWriter defines the interface used for writing migration SQL queries to a file on disk. * @@ -12,7 +14,7 @@ interface QueryWriter { /** - * @param string[][] $queriesByVersion + * @param array $queriesByVersion */ public function write( string $path, diff --git a/lib/Doctrine/Migrations/Version/DbalExecutor.php b/lib/Doctrine/Migrations/Version/DbalExecutor.php index 58d494a684..e50303626a 100644 --- a/lib/Doctrine/Migrations/Version/DbalExecutor.php +++ b/lib/Doctrine/Migrations/Version/DbalExecutor.php @@ -16,6 +16,7 @@ use Doctrine\Migrations\MigratorConfiguration; use Doctrine\Migrations\ParameterFormatter; use Doctrine\Migrations\Provider\SchemaDiffProvider; +use Doctrine\Migrations\Query\Query; use Doctrine\Migrations\Stopwatch; use Doctrine\Migrations\Tools\BytesFormatter; use Psr\Log\LoggerInterface; @@ -42,15 +43,9 @@ final class DbalExecutor implements Executor /** @var Stopwatch */ private $stopwatch; - /** @var array */ + /** @var Query[] */ private $sql = []; - /** @var mixed[] */ - private $params = []; - - /** @var mixed[] */ - private $types = []; - /** @var MetadataStorage */ private $metadataStorage; @@ -79,42 +74,16 @@ public function __construct( } /** - * @return string[] + * @return Query[] */ public function getSql() : array { return $this->sql; } - /** - * @return mixed[] - */ - public function getParams() : array + public function addSql(Query $sqlQuery) : void { - return $this->params; - } - - /** - * @return mixed[] - */ - public function getTypes() : array - { - return $this->types; - } - - /** - * @param mixed[] $params - * @param mixed[] $types - */ - public function addSql(string $sql, array $params = [], array $types = []) : void - { - $this->sql[] = $sql; - - if (count($params) === 0) { - return; - } - - $this->addQueryParams($params, $types); + $this->sql[] = $sqlQuery; } public function execute( @@ -132,7 +101,7 @@ public function execute( $configuration ); - $result->setSql($this->sql, $this->params, $this->types); + $result->setSql($this->sql); } catch (SkipMigration $e) { $result->setSkipped(true); @@ -152,9 +121,7 @@ private function startMigration( MigrationPlan $plan, MigratorConfiguration $configuration ) : void { - $this->sql = []; - $this->params = []; - $this->types = []; + $this->sql = []; $this->dispatcher->dispatchVersionEvent( Events::onMigrationsVersionExecuting, @@ -196,20 +163,20 @@ private function executeMigration( $migration->$direction($toSchema); - foreach ($migration->getSql() as $sqlData) { - $this->addSql(...$sqlData); + foreach ($migration->getSql() as $sqlQuery) { + $this->addSql($sqlQuery); } foreach ($this->schemaProvider->getSqlDiffToMigrate($fromSchema, $toSchema) as $sql) { - $this->addSql($sql); + $this->addSql(new Query($sql)); } if (count($this->sql) !== 0) { if (! $configuration->isDryRun()) { $this->executeResult($configuration); } else { - foreach ($this->sql as $idx => $query) { - $this->outputSqlQuery($idx, $query); + foreach ($this->sql as $query) { + $this->outputSqlQuery($query); } } } else { @@ -329,12 +296,12 @@ private function executeResult(MigratorConfiguration $configuration) : void foreach ($this->sql as $key => $query) { $stopwatchEvent = $this->stopwatch->start('query'); - $this->outputSqlQuery($key, $query); + $this->outputSqlQuery($query); - if (! isset($this->params[$key])) { - $this->connection->executeQuery($query); + if (count($query->getParameters()) === 0) { + $this->connection->executeQuery($query->getStatement()); } else { - $this->connection->executeQuery($query, $this->params[$key], $this->types[$key]); + $this->connection->executeQuery($query->getStatement(), $query->getParameters(), $query->getTypes()); } $stopwatchEvent->stop(); @@ -349,26 +316,15 @@ private function executeResult(MigratorConfiguration $configuration) : void } } - /** - * @param mixed[]|int $params - * @param mixed[]|int $types - */ - private function addQueryParams($params, $types) : void - { - $index = count($this->sql) - 1; - $this->params[$index] = $params; - $this->types[$index] = $types; - } - - private function outputSqlQuery(int $idx, string $query) : void + private function outputSqlQuery(Query $query) : void { $params = $this->parameterFormatter->formatParameters( - $this->params[$idx] ?? [], - $this->types[$idx] ?? [] + $query->getParameters(), + $query->getTypes() ); $this->logger->debug('{query} {params}', [ - 'query' => $query, + 'query' => $query->getStatement(), 'params' => $params, ]); } diff --git a/lib/Doctrine/Migrations/Version/ExecutionResult.php b/lib/Doctrine/Migrations/Version/ExecutionResult.php index fb7af9f235..ea8d203c45 100644 --- a/lib/Doctrine/Migrations/Version/ExecutionResult.php +++ b/lib/Doctrine/Migrations/Version/ExecutionResult.php @@ -6,6 +6,7 @@ use DateTimeImmutable; use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\Query\Query; use RuntimeException; use Throwable; use function count; @@ -17,15 +18,9 @@ */ final class ExecutionResult { - /** @var string[] */ + /** @var Query[] */ private $sql = []; - /** @var mixed[] */ - private $params = []; - - /** @var mixed[] */ - private $types = []; - /** @var float|null */ private $time; @@ -89,7 +84,7 @@ public function hasSql() : bool } /** - * @return string[] + * @return Query[] */ public function getSql() : array { @@ -97,31 +92,11 @@ public function getSql() : array } /** - * @param string[] $sql - * @param mixed[] $params - * @param int[] $types - */ - public function setSql(array $sql, array $params = [], array $types = []) : void - { - $this->sql = $sql; - $this->params = $params; - $this->types = $types; - } - - /** - * @return mixed[] - */ - public function getParams() : array - { - return $this->params; - } - - /** - * @return mixed[] + * @param Query[] $sql */ - public function getTypes() : array + public function setSql(array $sql) : void { - return $this->types; + $this->sql = $sql; } public function getTime() : ?float diff --git a/lib/Doctrine/Migrations/Version/Executor.php b/lib/Doctrine/Migrations/Version/Executor.php index ccc867811d..b7a56b4ff3 100644 --- a/lib/Doctrine/Migrations/Version/Executor.php +++ b/lib/Doctrine/Migrations/Version/Executor.php @@ -6,6 +6,7 @@ use Doctrine\Migrations\Metadata\MigrationPlan; use Doctrine\Migrations\MigratorConfiguration; +use Doctrine\Migrations\Query\Query; /** * The Executor defines the interface used for adding sql for a migration and executing that sql. @@ -14,11 +15,7 @@ */ interface Executor { - /** - * @param mixed[] $params - * @param mixed[] $types - */ - public function addSql(string $sql, array $params = [], array $types = []) : void; + public function addSql(Query $sqlQuery) : void; public function execute(MigrationPlan $plan, MigratorConfiguration $migratorConfiguration) : ExecutionResult; } diff --git a/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php b/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php index 6fab4bcf1a..3bc19a618e 100644 --- a/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php +++ b/tests/Doctrine/Migrations/Tests/AbstractMigrationTest.php @@ -7,6 +7,7 @@ use Doctrine\Migrations\Exception\AbortMigration; use Doctrine\Migrations\Exception\IrreversibleMigration; use Doctrine\Migrations\Exception\SkipMigration; +use Doctrine\Migrations\Query\Query; use Doctrine\Migrations\Tests\Stub\AbstractMigrationStub; class AbstractMigrationTest extends MigrationTestCase @@ -33,7 +34,7 @@ public function testAddSql() : void { $this->migration->exposedAddSql('SELECT 1', [1], [2]); - self::assertSame([['SELECT 1', [1], [2]]], $this->migration->getSql()); + self::assertEquals([new Query('SELECT 1', [1], [2])], $this->migration->getSql()); } public function testWarnIfOutputMessage() : void diff --git a/tests/Doctrine/Migrations/Tests/FileQueryWriterTest.php b/tests/Doctrine/Migrations/Tests/FileQueryWriterTest.php index 5792e031ea..9a2a5c1e53 100644 --- a/tests/Doctrine/Migrations/Tests/FileQueryWriterTest.php +++ b/tests/Doctrine/Migrations/Tests/FileQueryWriterTest.php @@ -6,6 +6,7 @@ use Doctrine\Migrations\FileQueryWriter; use Doctrine\Migrations\Generator\FileBuilder; +use Doctrine\Migrations\Query\Query; use Doctrine\Migrations\Version\Direction; use Psr\Log\LoggerInterface; use function file_get_contents; @@ -39,7 +40,7 @@ public function testWrite() : void $migrationFileBuilder ->expects(self::atLeastOnce()) ->method('buildMigrationFile') - ->with(['A'], Direction::UP) + ->with(['Foo' => [new Query('A')]], Direction::UP) ->willReturn('foo'); $logger = $this->createMock(LoggerInterface::class); @@ -52,7 +53,7 @@ public function testWrite() : void $logger ); - self::assertTrue($writer->write($path, Direction::UP, ['A'])); + self::assertTrue($writer->write($path, Direction::UP, ['Foo' => [new Query('A')]])); $files = $this->getSqlFilesList($path); diff --git a/tests/Doctrine/Migrations/Tests/Generator/ConcatenationFileBuilderTest.php b/tests/Doctrine/Migrations/Tests/Generator/ConcatenationFileBuilderTest.php index bee61a0ca1..18c8892f7e 100644 --- a/tests/Doctrine/Migrations/Tests/Generator/ConcatenationFileBuilderTest.php +++ b/tests/Doctrine/Migrations/Tests/Generator/ConcatenationFileBuilderTest.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\Migrations\Generator\ConcatenationFileBuilder; use Doctrine\Migrations\Generator\FileBuilder; +use Doctrine\Migrations\Query\Query; use Doctrine\Migrations\Version\Direction; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -23,17 +24,17 @@ class ConcatenationFileBuilderTest extends TestCase public function testBuildMigrationFile() : void { $queriesByVersion = [ - '1' => [ - 'SELECT 1', - 'SELECT 2', + 'foo' => [ + new Query('SELECT 1'), + new Query('SELECT 2'), ], - '2' => [ - 'SELECT 3', - 'SELECT 4', + 'bar' => [ + new Query('SELECT 3'), + new Query('SELECT 4'), ], - '3' => [ - 'SELECT 5', - 'SELECT 6', + 'baz' => [ + new Query('SELECT 5'), + new Query('SELECT 6'), ], ]; @@ -48,15 +49,15 @@ public function testBuildMigrationFile() : void $expected = <<<'FILE' -- Doctrine Migration File Generated on 2018-09-01 00:00:00 --- Version 1 +-- Version foo SELECT 1; SELECT 2; --- Version 2 +-- Version bar SELECT 3; SELECT 4; --- Version 3 +-- Version baz SELECT 5; SELECT 6; diff --git a/tests/Doctrine/Migrations/Tests/MigratorTest.php b/tests/Doctrine/Migrations/Tests/MigratorTest.php index 013b4d057b..79bb2287e8 100644 --- a/tests/Doctrine/Migrations/Tests/MigratorTest.php +++ b/tests/Doctrine/Migrations/Tests/MigratorTest.php @@ -26,6 +26,7 @@ use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Stopwatch\Stopwatch as SymfonyStopwatch; use Throwable; +use function array_map; use const DIRECTORY_SEPARATOR; class MigratorTest extends MigrationTestCase @@ -72,9 +73,12 @@ public function testGetSql() : void $sql = $migrator->migrate($planList, $this->migratorConfiguration); - self::assertSame([ - 'Doctrine\\Migrations\\Tests\\Stub\\Functional\\MigrateNotTouchingTheSchema' => ['SELECT 1'], - ], $sql); + self::assertCount(1, $sql); + self::assertArrayHasKey('Doctrine\\Migrations\\Tests\\Stub\\Functional\\MigrateNotTouchingTheSchema', $sql); + self::assertSame( + ['SELECT 1'], + array_map('strval', $sql['Doctrine\\Migrations\\Tests\\Stub\\Functional\\MigrateNotTouchingTheSchema']) + ); } public function testEmptyPlanShowsMessage() : void @@ -130,9 +134,13 @@ public function testMigrateAllOrNothing() : void $planList = new MigrationPlanList([$plan], Direction::UP); $sql = $migrator->migrate($planList, $this->migratorConfiguration); - self::assertSame([ - 'Doctrine\\Migrations\\Tests\\Stub\\Functional\\MigrateNotTouchingTheSchema' => ['SELECT 1'], - ], $sql); + + self::assertCount(1, $sql); + self::assertArrayHasKey('Doctrine\\Migrations\\Tests\\Stub\\Functional\\MigrateNotTouchingTheSchema', $sql); + self::assertSame( + ['SELECT 1'], + array_map('strval', $sql['Doctrine\\Migrations\\Tests\\Stub\\Functional\\MigrateNotTouchingTheSchema']) + ); } public function testMigrateAllOrNothingRollback() : void diff --git a/tests/Doctrine/Migrations/Tests/Query/QueryTest.php b/tests/Doctrine/Migrations/Tests/Query/QueryTest.php new file mode 100644 index 0000000000..a968590a32 --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Query/QueryTest.php @@ -0,0 +1,21 @@ +getStatement()); + self::assertSame('foo', (string) $query); + self::assertSame(['bar', 'baz'], $query->getParameters()); + self::assertSame(['qux', 'quux'], $query->getTypes()); + } +} diff --git a/tests/Doctrine/Migrations/Tests/Version/ExecutionResultTest.php b/tests/Doctrine/Migrations/Tests/Version/ExecutionResultTest.php index a651207412..0b7fc4ee43 100644 --- a/tests/Doctrine/Migrations/Tests/Version/ExecutionResultTest.php +++ b/tests/Doctrine/Migrations/Tests/Version/ExecutionResultTest.php @@ -6,6 +6,7 @@ use DateTimeImmutable; use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\Query\Query; use Doctrine\Migrations\Version\Direction; use Doctrine\Migrations\Version\ExecutionResult; use Doctrine\Migrations\Version\Version; @@ -25,13 +26,19 @@ public function testHasSql() : void public function testGetSetSql() : void { - self::assertSame(['SELECT 1'], $this->versionExecutionResult->getSql()); - - $this->versionExecutionResult->setSql(['SELECT 2'], [2], [3]); - - self::assertSame(['SELECT 2'], $this->versionExecutionResult->getSql()); - self::assertSame([2], $this->versionExecutionResult->getParams()); - self::assertSame([3], $this->versionExecutionResult->getTypes()); + $queries = $this->versionExecutionResult->getSql(); + self::assertCount(1, $queries); + self::assertSame('SELECT 1', $queries[0]->getStatement()); + self::assertSame([1], $queries[0]->getParameters()); + self::assertSame([2], $queries[0]->getTypes()); + + $this->versionExecutionResult->setSql([new Query('SELECT 2', [2], [3])]); + + $queries = $this->versionExecutionResult->getSql(); + self::assertCount(1, $queries); + self::assertSame('SELECT 2', $queries[0]->getStatement()); + self::assertSame([2], $queries[0]->getParameters()); + self::assertSame([3], $queries[0]->getTypes()); } public function testGetSetTime() : void @@ -120,10 +127,6 @@ protected function setUp() : void new Version('foo'), Direction::UP ); - $this->versionExecutionResult->setSql( - ['SELECT 1'], - [1], - [2] - ); + $this->versionExecutionResult->setSql([new Query('SELECT 1', [1], [2])]); } } diff --git a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php index cc82827bba..a21d452d69 100644 --- a/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php +++ b/tests/Doctrine/Migrations/Tests/Version/ExecutorTest.php @@ -15,6 +15,7 @@ use Doctrine\Migrations\MigratorConfiguration; use Doctrine\Migrations\ParameterFormatter; use Doctrine\Migrations\Provider\SchemaDiffProvider; +use Doctrine\Migrations\Query\Query; use Doctrine\Migrations\Stopwatch; use Doctrine\Migrations\Tests\TestLogger; use Doctrine\Migrations\Version\DbalExecutor; @@ -65,11 +66,11 @@ class ExecutorTest extends TestCase public function testAddSql() : void { - $this->versionExecutor->addSql('SELECT 1', [1], [2]); + $query = new Query('SELECT 1', [1], [2]); + $this->versionExecutor->addSql($query); - self::assertSame(['SELECT 1'], $this->versionExecutor->getSql()); - self::assertSame([[1]], $this->versionExecutor->getParams()); - self::assertSame([[2]], $this->versionExecutor->getTypes()); + self::assertCount(1, $this->versionExecutor->getSql()); + self::assertSame($query, $this->versionExecutor->getSql()[0]); } public function testExecuteUp() : void @@ -92,9 +93,16 @@ public function testExecuteUp() : void $migratorConfiguration ); - self::assertSame(['SELECT 1', 'SELECT 2'], $result->getSql()); - self::assertSame([[1]], $result->getParams()); - self::assertSame([[3]], $result->getTypes()); + $queries = $result->getSql(); + self::assertCount(2, $queries); + self::assertSame('SELECT 1', $queries[0]->getStatement()); + self::assertSame([1], $queries[0]->getParameters()); + self::assertSame([3], $queries[0]->getTypes()); + + self::assertSame('SELECT 2', $queries[1]->getStatement()); + self::assertSame([], $queries[1]->getParameters()); + self::assertSame([], $queries[1]->getTypes()); + self::assertNotNull($result->getTime()); self::assertSame(State::NONE, $result->getState()); self::assertTrue($this->migration->preUpExecuted); @@ -148,9 +156,16 @@ public function testExecuteDown() : void $migratorConfiguration ); - self::assertSame(['SELECT 3', 'SELECT 4'], $result->getSql()); - self::assertSame([[5], [6]], $result->getParams()); - self::assertSame([[7], [8]], $result->getTypes()); + $queries = $result->getSql(); + self::assertCount(2, $queries); + self::assertSame('SELECT 3', $queries[0]->getStatement()); + self::assertSame([5], $queries[0]->getParameters()); + self::assertSame([7], $queries[0]->getTypes()); + + self::assertSame('SELECT 4', $queries[1]->getStatement()); + self::assertSame([6], $queries[1]->getParameters()); + self::assertSame([8], $queries[1]->getTypes()); + self::assertNotNull($result->getTime()); self::assertSame(State::NONE, $result->getState()); self::assertFalse($this->migration->preUpExecuted);