Skip to content

Commit cbfc3e7

Browse files
committed
test: improve coverage
1 parent 1fb0bbf commit cbfc3e7

File tree

4 files changed

+274
-1
lines changed

4 files changed

+274
-1
lines changed

test/phpunit/Cli/ExecuteCommandTest.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,29 @@ public function getConfigPublic(string $repoBasePath, ?string $defaultPath):Conf
145145
self::assertSame("sqlite", $config->get("database.driver"));
146146
}
147147

148+
public function testGetDefaultPathReturnsNullWhenVendorDefaultDoesNotExist():void {
149+
$project = $this->createProjectDir();
150+
$command = new ExecuteCommand();
151+
$reflectionMethod = new \ReflectionMethod($command, "getDefaultPath");
152+
153+
self::assertNull($reflectionMethod->invoke($command, $project));
154+
}
155+
156+
public function testGetDefaultPathFindsVendorConfigDefaultIni():void {
157+
$project = $this->createProjectDir();
158+
$vendorConfigDir = $project . DIRECTORY_SEPARATOR . "vendor"
159+
. DIRECTORY_SEPARATOR . "phpgt"
160+
. DIRECTORY_SEPARATOR . "webengine";
161+
mkdir($vendorConfigDir, 0775, true);
162+
$defaultConfig = $vendorConfigDir . DIRECTORY_SEPARATOR . "config.default.ini";
163+
file_put_contents($defaultConfig, "[database]\ndriver = sqlite\n");
164+
165+
$command = new ExecuteCommand();
166+
$reflectionMethod = new \ReflectionMethod($command, "getDefaultPath");
167+
168+
self::assertSame($defaultConfig, $reflectionMethod->invoke($command, $project));
169+
}
170+
148171
public function testExecuteMigratesAll():void {
149172
$project = $this->createProjectDir();
150173
$sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db");
@@ -379,6 +402,97 @@ public function testExecuteWithDevMergePromotesDevMigrations():void {
379402
}
380403
}
381404

405+
public function testExecuteWithDevReportsIntegrityErrorWhenAppliedDevFileChanges():void {
406+
$project = $this->createProjectDir();
407+
$sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db");
408+
$this->writeConfigIni($project, $sqlitePath);
409+
$this->createMigrations($project, 1);
410+
$devFiles = $this->createDevMigrations($project, 1);
411+
412+
$cwdBackup = getcwd();
413+
chdir($project);
414+
try {
415+
$cmd = new ExecuteCommand();
416+
$streams = $this->makeStreamFiles();
417+
$cmd->setStream($streams["stream"]);
418+
419+
$args = new ArgumentValueList();
420+
$args->set("dev");
421+
$cmd->run($args);
422+
423+
file_put_contents($devFiles[0], "alter table `test` add `changed_dev_column` varchar(32)");
424+
425+
$errorStreams = $this->makeStreamFiles();
426+
$cmd->setStream($errorStreams["stream"]);
427+
$cmd->run($args);
428+
429+
list("out" => $out) = $this->readFromFiles($errorStreams["out"], $errorStreams["err"]);
430+
self::assertStringContainsString("integrity error migrating dev file", $out);
431+
}
432+
finally {
433+
chdir($cwdBackup);
434+
}
435+
}
436+
437+
public function testExecuteWithDevMergeReportsIntegrityErrorWhenAppliedDevFileChanges():void {
438+
$project = $this->createProjectDir();
439+
$sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db");
440+
$this->writeConfigIni($project, $sqlitePath);
441+
$this->createMigrations($project, 1);
442+
$devFiles = $this->createDevMigrations($project, 1);
443+
444+
$cwdBackup = getcwd();
445+
chdir($project);
446+
try {
447+
$cmd = new ExecuteCommand();
448+
$devStreams = $this->makeStreamFiles();
449+
$cmd->setStream($devStreams["stream"]);
450+
451+
$devArgs = new ArgumentValueList();
452+
$devArgs->set("dev");
453+
$cmd->run($devArgs);
454+
455+
file_put_contents($devFiles[0], "alter table `test` add `changed_dev_column` varchar(32)");
456+
457+
$mergeStreams = $this->makeStreamFiles();
458+
$cmd->setStream($mergeStreams["stream"]);
459+
$mergeArgs = new ArgumentValueList();
460+
$mergeArgs->set("dev-merge");
461+
$cmd->run($mergeArgs);
462+
463+
list("out" => $out) = $this->readFromFiles($mergeStreams["out"], $mergeStreams["err"]);
464+
self::assertStringContainsString("integrity error merging dev migration file", $out);
465+
}
466+
finally {
467+
chdir($cwdBackup);
468+
}
469+
}
470+
471+
public function testExecuteWithDevMergeReportsNoDevMigrationsToMerge():void {
472+
$project = $this->createProjectDir();
473+
$sqlitePath = str_replace("\\", "/", $project . DIRECTORY_SEPARATOR . "cli-test.db");
474+
$this->writeConfigIni($project, $sqlitePath);
475+
$this->createMigrations($project, 1);
476+
477+
$cwdBackup = getcwd();
478+
chdir($project);
479+
try {
480+
$cmd = new ExecuteCommand();
481+
$streams = $this->makeStreamFiles();
482+
$cmd->setStream($streams["stream"]);
483+
484+
$args = new ArgumentValueList();
485+
$args->set("dev-merge");
486+
$cmd->run($args);
487+
488+
list("out" => $out) = $this->readFromFiles($streams["out"], $streams["err"]);
489+
self::assertStringContainsString("No dev migrations to merge.", $out);
490+
}
491+
finally {
492+
chdir($cwdBackup);
493+
}
494+
}
495+
382496
public function testCliArgumentsOverrideConfigValuesWhenBuildingSettings():void {
383497
$repoBasePath = "/tmp/project-root";
384498
$config = new Config(
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}

test/phpunit/Migration/MigratorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ public function testResetMigration(array $fileList) {
420420

421421
/**
422422
* @dataProvider dataMigrationFileList
423-
* @runInSeparateProcess
424423
*/
425424
public function testPerformMigrationGood(array $fileList):void {
426425
$path = $this->getMigrationDirectory();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
namespace Gt\Database\Test\Migration;
3+
4+
use Gt\Database\Migration\SqlStatementSplitter;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class SqlStatementSplitterTest extends TestCase {
8+
public function testSplitSeparatesMultipleStatements():void {
9+
$splitter = new SqlStatementSplitter();
10+
11+
$statements = $splitter->split(implode("\n", [
12+
"create table test(id int);",
13+
"insert into test values(1);",
14+
"update test set id = 2 where id = 1;",
15+
]));
16+
17+
self::assertSame([
18+
"create table test(id int)",
19+
"insert into test values(1)",
20+
"update test set id = 2 where id = 1",
21+
], $statements);
22+
}
23+
24+
public function testSplitIgnoresEmptyStatements():void {
25+
$splitter = new SqlStatementSplitter();
26+
27+
$statements = $splitter->split(" ; ;\nselect 1;; ");
28+
29+
self::assertSame([
30+
"select 1",
31+
], $statements);
32+
}
33+
}

0 commit comments

Comments
 (0)