-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDumpSqlCommandTest.php
More file actions
145 lines (129 loc) · 5.27 KB
/
DumpSqlCommandTest.php
File metadata and controls
145 lines (129 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace CakeDumpSql\Test\TestCase\Command;
use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Postgres;
use Cake\Database\Driver\Sqlite;
use Cake\Datasource\ConnectionManager;
use Cake\I18n\DateTime;
use Cake\TestSuite\TestCase;
use CakeDumpSql\Error\UnknownDriverException;
use TestApp\Driver\MyDriver;
class DumpSqlCommandTest extends TestCase
{
use ConsoleIntegrationTestTrait;
public function setUp(): void
{
parent::setUp();
// Sets the TestApp namespace to be used instead of App
$this->setAppNamespace();
$this->configApplication(
'TestApp\Application',
[PLUGIN_TESTS . 'test_app' . DS . 'config'],
);
}
public function testCommand(): void
{
$postsTable = $this->fetchTable('Posts');
$entity = $postsTable->newEmptyEntity();
$entity = $postsTable->patchEntity($entity, [
'title' => 'Testtitle',
'created' => new DateTime(),
'modified' => new DateTime(),
]);
$postsTable->save($entity);
$this->exec('dump_sql');
if ($this->isDBType(Sqlite::class)) {
$this->assertOutputContains('CREATE TABLE IF NOT EXISTS "posts"');
$this->assertOutputContains('INSERT INTO posts VALUES(');
} elseif ($this->isDBType(Mysql::class)) {
$this->assertOutputContains('CREATE TABLE `posts` (');
$this->assertOutputContains('INSERT INTO `posts` VALUES');
} elseif ($this->isDBType(Postgres::class)) {
$this->assertOutputContains('CREATE TABLE public.posts');
$this->assertOutputContains('COPY public.posts (id, title, created, modified) FROM stdin;');
}
$this->assertExitCode(0);
}
public function testCommandDataOnly(): void
{
$postsTable = $this->fetchTable('Posts');
$entity = $postsTable->newEmptyEntity();
$entity = $postsTable->patchEntity($entity, [
'title' => 'Testtitle',
'created' => new DateTime(),
'modified' => new DateTime(),
]);
$postsTable->save($entity);
$this->exec('dump_sql --data-only');
if ($this->isDBType(Sqlite::class)) {
$this->assertOutputNotContains('CREATE TABLE IF NOT EXISTS "posts"');
$this->assertOutputContains('INSERT INTO posts VALUES(');
} elseif ($this->isDBType(Mysql::class)) {
$this->assertOutputNotContains('CREATE TABLE `posts` (');
$this->assertOutputContains('INSERT INTO `posts` VALUES');
} elseif ($this->isDBType(Postgres::class)) {
$this->assertOutputNotContains('CREATE TABLE public.posts');
$this->assertOutputContains('COPY public.posts (id, title, created, modified) FROM stdin;');
}
$this->assertExitCode(0);
}
public function testCommandGzipped(): void
{
$postsTable = $this->fetchTable('Posts');
$entity = $postsTable->newEmptyEntity();
$entity = $postsTable->patchEntity($entity, [
'title' => 'Testtitle',
'created' => new DateTime(),
'modified' => new DateTime(),
]);
$postsTable->save($entity);
$this->exec('dump_sql --gzip');
$result = $this->_out?->messages() ?? [];
$sql = gzdecode($result[0]);
if (!$sql) {
$this->fail('Failed to decode gzipped output');
}
if ($this->isDBType(Sqlite::class)) {
$this->assertStringContainsString('CREATE TABLE IF NOT EXISTS "posts"', $sql);
$this->assertStringContainsString('INSERT INTO posts VALUES(', $sql);
} elseif ($this->isDBType(Mysql::class)) {
$this->assertStringContainsString('CREATE TABLE `posts` (', $sql);
$this->assertStringContainsString('INSERT INTO `posts` VALUES', $sql);
} elseif ($this->isDBType(Postgres::class)) {
$this->assertStringContainsString('CREATE TABLE public.posts', $sql);
$this->assertStringContainsString('COPY public.posts (id, title, created, modified) FROM stdin;', $sql);
}
$this->assertExitCode(0);
}
public function testUnknownConnectionName(): void
{
$this->exec('dump_sql unknown');
$this->assertErrorContains('The datasource configuration `unknown` was not found.');
$this->assertExitCode(1);
}
public function testUnknownDriver(): void
{
$this->expectException(UnknownDriverException::class);
$this->expectExceptionMessage('Unknown driver "TestApp\Driver\MyDriver" given.');
ConnectionManager::setConfig('unknown', [
'className' => Connection::class,
'driver' => MyDriver::class,
'persistent' => false,
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
]);
$this->exec('dump_sql unknown');
}
private function isDBType(string $type, string $connection = 'default'): bool
{
$connection = ConnectionManager::get($connection);
$driver = $connection->getDriver();
return get_class($driver) === $type;
}
}