|
| 1 | +<?php |
| 2 | +namespace Gt\Database\Test\Migration; |
| 3 | + |
| 4 | +use Gt\Database\Connection\Settings; |
| 5 | +use Gt\Database\Migration\AbstractMigrator; |
| 6 | +use Gt\Database\Test\Helper\Helper; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use SplFileObject; |
| 9 | + |
| 10 | +class AbstractMigratorTest extends TestCase { |
| 11 | + private function createWorkspace():string { |
| 12 | + $dir = Helper::getTmpDir() . DIRECTORY_SEPARATOR . uniqid("abstract-migrator-"); |
| 13 | + mkdir($dir, 0775, true); |
| 14 | + return $dir; |
| 15 | + } |
| 16 | + |
| 17 | + private function createSettings(string $baseDirectory, string $driver, string $schema):Settings { |
| 18 | + return new Settings( |
| 19 | + $baseDirectory, |
| 20 | + $driver, |
| 21 | + $schema |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + private function createProbe(Settings $settings, string $path, ?string $tableName = null):AbstractMigrator { |
| 26 | + return new class($settings, $path, $tableName) extends AbstractMigrator { |
| 27 | + protected function getDefaultTableName():string { |
| 28 | + return "_probe"; |
| 29 | + } |
| 30 | + |
| 31 | + public function getTableNamePublic():string { |
| 32 | + return $this->tableName; |
| 33 | + } |
| 34 | + |
| 35 | + public function nowExpressionPublic():string { |
| 36 | + return $this->nowExpression(); |
| 37 | + } |
| 38 | + |
| 39 | + public function setDriverPublic(string $driver):void { |
| 40 | + $this->driver = $driver; |
| 41 | + } |
| 42 | + |
| 43 | + public function executeSqlFilePublic(string $file):string { |
| 44 | + return $this->executeSqlFile($file); |
| 45 | + } |
| 46 | + |
| 47 | + public function outputPublic(string $message, string $streamName = self::STREAM_OUT):void { |
| 48 | + $this->output($message, $streamName); |
| 49 | + } |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + public function testConstructorUsesDefaultTableNameWhenNotProvided():void { |
| 54 | + $dir = $this->createWorkspace(); |
| 55 | + $settings = $this->createSettings($dir, Settings::DRIVER_SQLITE, $dir . "/probe.sqlite"); |
| 56 | + $probe = $this->createProbe($settings, $dir); |
| 57 | + |
| 58 | + self::assertSame("_probe", $probe->getTableNamePublic()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testGetMigrationFileListReturnsEmptyForMissingDirectory():void { |
| 62 | + $dir = $this->createWorkspace(); |
| 63 | + $settings = $this->createSettings($dir, Settings::DRIVER_SQLITE, $dir . "/probe.sqlite"); |
| 64 | + $probe = $this->createProbe($settings, $dir . "/missing-dir"); |
| 65 | + |
| 66 | + self::assertSame([], $probe->getMigrationFileList()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testNowExpressionUsesDriverSpecificValue():void { |
| 70 | + $dir = $this->createWorkspace(); |
| 71 | + $sqliteProbe = $this->createProbe( |
| 72 | + $this->createSettings($dir, Settings::DRIVER_SQLITE, $dir . "/probe.sqlite"), |
| 73 | + $dir |
| 74 | + ); |
| 75 | + $mysqlProbe = $this->createProbe( |
| 76 | + $this->createSettings($dir, Settings::DRIVER_SQLITE, $dir . "/probe-mysql.sqlite"), |
| 77 | + $dir |
| 78 | + ); |
| 79 | + $mysqlProbe->setDriverPublic(Settings::DRIVER_MYSQL); |
| 80 | + |
| 81 | + self::assertSame("datetime('now')", $sqliteProbe->nowExpressionPublic()); |
| 82 | + self::assertSame("now()", $mysqlProbe->nowExpressionPublic()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testExecuteSqlFileRunsEachStatementAndReturnsHash():void { |
| 86 | + $dir = Helper::getTmpDir() . DIRECTORY_SEPARATOR . uniqid("probe-"); |
| 87 | + mkdir($dir, 0775, true); |
| 88 | + $databasePath = $dir . DIRECTORY_SEPARATOR . "probe.sqlite"; |
| 89 | + $sqlFile = $dir . DIRECTORY_SEPARATOR . "001-probe.sql"; |
| 90 | + file_put_contents($sqlFile, implode("\n", [ |
| 91 | + "create table test(id int primary key, name text);", |
| 92 | + "insert into test values(1, 'alpha');", |
| 93 | + ])); |
| 94 | + |
| 95 | + $probe = $this->createProbe( |
| 96 | + $this->createSettings($dir, Settings::DRIVER_SQLITE, $databasePath), |
| 97 | + $dir |
| 98 | + ); |
| 99 | + |
| 100 | + $hash = $probe->executeSqlFilePublic($sqlFile); |
| 101 | + self::assertSame(md5_file($sqlFile), $hash); |
| 102 | + |
| 103 | + $db = new \Gt\Database\Database( |
| 104 | + $this->createSettings($dir, Settings::DRIVER_SQLITE, $databasePath) |
| 105 | + ); |
| 106 | + $row = $db->executeSql("select name from test where id = 1")->fetch(); |
| 107 | + self::assertSame("alpha", $row?->getString("name")); |
| 108 | + } |
| 109 | + |
| 110 | + public function testOutputWritesToErrorStreamWhenRequested():void { |
| 111 | + $dir = $this->createWorkspace(); |
| 112 | + $probe = $this->createProbe( |
| 113 | + $this->createSettings($dir, Settings::DRIVER_SQLITE, $dir . "/probe.sqlite"), |
| 114 | + $dir |
| 115 | + ); |
| 116 | + |
| 117 | + $out = new SplFileObject("php://memory", "w+"); |
| 118 | + $err = new SplFileObject("php://memory", "w+"); |
| 119 | + $probe->setOutput($out, $err); |
| 120 | + $probe->outputPublic("problem", AbstractMigrator::STREAM_ERROR); |
| 121 | + |
| 122 | + $out->rewind(); |
| 123 | + $err->rewind(); |
| 124 | + self::assertSame("", $out->fread(128)); |
| 125 | + self::assertSame("problem\n", $err->fread(128)); |
| 126 | + } |
| 127 | +} |
0 commit comments