Skip to content

Commit 4f4e713

Browse files
committed
Added unit tests for download-db, download-db-ftp, download-db-url, and export-db scripts.
1 parent 9f26dd2 commit 4f4e713

4 files changed

Lines changed: 658 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DrevOps\VortexTooling\Tests\Unit;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Group;
9+
10+
#[Group('scripts')]
11+
class DownloadDbFtpTest extends UnitTestCase {
12+
13+
protected function setUp(): void {
14+
parent::setUp();
15+
16+
$this->envSet('VORTEX_DOWNLOAD_DB_FTP_USER', 'testuser');
17+
$this->envSet('VORTEX_DOWNLOAD_DB_FTP_PASS', 'testpass');
18+
$this->envSet('VORTEX_DOWNLOAD_DB_FTP_HOST', 'ftp.example.com');
19+
$this->envSet('VORTEX_DOWNLOAD_DB_FTP_PORT', '21');
20+
$this->envSet('VORTEX_DOWNLOAD_DB_FTP_FILE', 'backups/db.sql');
21+
$this->envSet('VORTEX_DOWNLOAD_DB_FTP_DB_DIR', self::$tmp . '/data');
22+
$this->envSet('VORTEX_DOWNLOAD_DB_FTP_DB_FILE', 'db.sql');
23+
}
24+
25+
#[DataProvider('dataProviderSuccess')]
26+
public function testSuccess(?\Closure $before, array $expected, ?\Closure $after = NULL): void {
27+
if ($before instanceof \Closure) {
28+
$before($this);
29+
}
30+
31+
$this->mockRequestMultiple([
32+
['url' => 'ftp://ftp.example.com:21/backups/db.sql', 'method' => 'GET', 'response' => []],
33+
]);
34+
35+
$output = $this->runScript('src/download-db-ftp');
36+
37+
foreach ($expected as $str) {
38+
$this->assertStringContainsString($str, $output);
39+
}
40+
41+
if ($after instanceof \Closure) {
42+
$after($this);
43+
}
44+
}
45+
46+
public static function dataProviderSuccess(): array {
47+
return [
48+
'success' => [
49+
'before' => function (self $test): void {
50+
mkdir(self::$tmp . '/data', 0755, TRUE);
51+
},
52+
'expected' => ['Started database dump download from FTP.', 'Finished database dump download from FTP.'],
53+
'after' => function (self $test): void {
54+
$test->assertFileExists(self::$tmp . '/data/db.sql');
55+
},
56+
],
57+
'directory creation' => [
58+
'before' => NULL,
59+
'expected' => ['Finished database dump download from FTP.'],
60+
'after' => function (self $test): void {
61+
$test->assertTrue(is_dir(self::$tmp . '/data'));
62+
},
63+
],
64+
];
65+
}
66+
67+
#[DataProvider('dataProviderError')]
68+
public function testError(\Closure $before, string $expected): void {
69+
$before($this);
70+
71+
$this->runScriptError('src/download-db-ftp', $expected);
72+
}
73+
74+
public static function dataProviderError(): array {
75+
return [
76+
'missing user' => [
77+
'before' => function (self $test): void {
78+
$test->envSet('VORTEX_DOWNLOAD_DB_FTP_USER', '');
79+
},
80+
'expected' => 'Missing required value for VORTEX_DOWNLOAD_DB_FTP_USER',
81+
],
82+
'missing pass' => [
83+
'before' => function (self $test): void {
84+
$test->envSet('VORTEX_DOWNLOAD_DB_FTP_PASS', '');
85+
},
86+
'expected' => 'Missing required value for VORTEX_DOWNLOAD_DB_FTP_PASS',
87+
],
88+
'missing host' => [
89+
'before' => function (self $test): void {
90+
$test->envSet('VORTEX_DOWNLOAD_DB_FTP_HOST', '');
91+
},
92+
'expected' => 'Missing required value for VORTEX_DOWNLOAD_DB_FTP_HOST',
93+
],
94+
'missing port' => [
95+
'before' => function (self $test): void {
96+
$test->envSet('VORTEX_DOWNLOAD_DB_FTP_PORT', '');
97+
},
98+
'expected' => 'Missing required value for VORTEX_DOWNLOAD_DB_FTP_PORT',
99+
],
100+
'missing file' => [
101+
'before' => function (self $test): void {
102+
$test->envSet('VORTEX_DOWNLOAD_DB_FTP_FILE', '');
103+
},
104+
'expected' => 'Missing required value for VORTEX_DOWNLOAD_DB_FTP_FILE',
105+
],
106+
'request fails' => [
107+
'before' => function (self $test): void {
108+
mkdir(self::$tmp . '/data', 0755, TRUE);
109+
$test->mockRequestMultiple([
110+
['url' => 'ftp://ftp.example.com:21/backups/db.sql', 'method' => 'GET', 'response' => ['ok' => FALSE, 'status' => 550]],
111+
]);
112+
},
113+
'expected' => 'Failed to download database dump from FTP',
114+
],
115+
];
116+
}
117+
118+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DrevOps\VortexTooling\Tests\Unit;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Group;
9+
10+
#[Group('scripts')]
11+
class DownloadDbTest extends UnitTestCase {
12+
13+
/**
14+
* Path to the src directory.
15+
*/
16+
protected static string $srcDir;
17+
18+
protected function setUp(): void {
19+
parent::setUp();
20+
21+
self::$srcDir = (string) realpath(__DIR__ . '/../../src');
22+
23+
$this->envSet('VORTEX_DOWNLOAD_DB_SOURCE', 'url');
24+
$this->envSet('VORTEX_DOWNLOAD_DB_FORCE', '');
25+
$this->envSet('VORTEX_DOWNLOAD_DB_PROCEED', '1');
26+
$this->envSet('VORTEX_DOWNLOAD_DB_FILE', 'db.sql');
27+
$this->envSet('VORTEX_DOWNLOAD_DB_DIR', self::$tmp . '/data');
28+
$this->envSet('VORTEX_DOWNLOAD_DB_SEMAPHORE', '');
29+
}
30+
31+
#[DataProvider('dataProviderEarlyPass')]
32+
public function testEarlyPass(\Closure $before, string $expected): void {
33+
$before($this);
34+
35+
$this->runScriptEarlyPass('src/download-db', $expected);
36+
}
37+
38+
public static function dataProviderEarlyPass(): array {
39+
return [
40+
'proceed not set' => [
41+
'before' => function (self $test): void {
42+
$test->envSet('VORTEX_DOWNLOAD_DB_PROCEED', '0');
43+
},
44+
'expected' => 'Skipping database download',
45+
],
46+
'existing dump skips download' => [
47+
'before' => function (self $test): void {
48+
$db_dir = self::$tmp . '/data';
49+
mkdir($db_dir, 0755, TRUE);
50+
file_put_contents($db_dir . '/db.sql', 'fake-dump');
51+
$test->mockPassthru([
52+
'cmd' => 'ls -Alh ' . escapeshellarg($db_dir) . ' 2>/dev/null || true',
53+
'result_code' => 0,
54+
]);
55+
},
56+
'expected' => 'Download will not proceed',
57+
],
58+
];
59+
}
60+
61+
#[DataProvider('dataProviderSuccess')]
62+
public function testSuccess(\Closure $before, array $expected, ?\Closure $after = NULL): void {
63+
$before($this);
64+
65+
$output = $this->runScript('src/download-db');
66+
67+
foreach ($expected as $str) {
68+
$this->assertStringContainsString($str, $output);
69+
}
70+
71+
if ($after instanceof \Closure) {
72+
$after($this);
73+
}
74+
}
75+
76+
public static function dataProviderSuccess(): array {
77+
return [
78+
'default url source' => [
79+
'before' => function (self $test): void {
80+
$db_dir = self::$tmp . '/data';
81+
$test->mockPassthruMultiple([
82+
['cmd' => self::$srcDir . '/download-db-url', 'result_code' => 0],
83+
['cmd' => 'ls -Alh ' . escapeshellarg($db_dir) . ' || true', 'result_code' => 0],
84+
]);
85+
},
86+
'expected' => ['Started database download.', 'Finished database download.'],
87+
'after' => NULL,
88+
],
89+
'ftp source' => [
90+
'before' => function (self $test): void {
91+
$db_dir = self::$tmp . '/data';
92+
$test->envSet('VORTEX_DOWNLOAD_DB_SOURCE', 'ftp');
93+
$test->mockPassthruMultiple([
94+
['cmd' => self::$srcDir . '/download-db-ftp', 'result_code' => 0],
95+
['cmd' => 'ls -Alh ' . escapeshellarg($db_dir) . ' || true', 'result_code' => 0],
96+
]);
97+
},
98+
'expected' => ['Finished database download.'],
99+
'after' => NULL,
100+
],
101+
'existing dump force override' => [
102+
'before' => function (self $test): void {
103+
$db_dir = self::$tmp . '/data';
104+
mkdir($db_dir, 0755, TRUE);
105+
file_put_contents($db_dir . '/db.sql', 'fake-dump');
106+
$test->envSet('VORTEX_DOWNLOAD_DB_FORCE', '1');
107+
$test->mockPassthruMultiple([
108+
['cmd' => 'ls -Alh ' . escapeshellarg($db_dir) . ' 2>/dev/null || true', 'result_code' => 0],
109+
['cmd' => self::$srcDir . '/download-db-url', 'result_code' => 0],
110+
['cmd' => 'ls -Alh ' . escapeshellarg($db_dir) . ' || true', 'result_code' => 0],
111+
]);
112+
},
113+
'expected' => ['Will download a fresh copy', 'Finished database download.'],
114+
'after' => NULL,
115+
],
116+
'semaphore created' => [
117+
'before' => function (self $test): void {
118+
$db_dir = self::$tmp . '/data';
119+
$test->envSet('VORTEX_DOWNLOAD_DB_SEMAPHORE', self::$tmp . '/sem');
120+
$test->mockPassthruMultiple([
121+
['cmd' => self::$srcDir . '/download-db-url', 'result_code' => 0],
122+
['cmd' => 'ls -Alh ' . escapeshellarg($db_dir) . ' || true', 'result_code' => 0],
123+
]);
124+
},
125+
'expected' => ['Finished database download.'],
126+
'after' => function (self $test): void {
127+
$test->assertFileExists(self::$tmp . '/sem');
128+
},
129+
],
130+
];
131+
}
132+
133+
#[DataProvider('dataProviderError')]
134+
public function testError(\Closure $before, string $expected): void {
135+
$before($this);
136+
137+
$this->runScriptError('src/download-db', $expected);
138+
}
139+
140+
public static function dataProviderError(): array {
141+
return [
142+
'invalid source' => [
143+
'before' => function (self $test): void {
144+
$test->envSet('VORTEX_DOWNLOAD_DB_SOURCE', 'invalid');
145+
},
146+
'expected' => 'Invalid database download source',
147+
],
148+
'source script fails' => [
149+
'before' => function (self $test): void {
150+
$test->mockPassthru([
151+
'cmd' => self::$srcDir . '/download-db-url',
152+
'result_code' => 1,
153+
]);
154+
},
155+
'expected' => 'Failed to download database',
156+
],
157+
];
158+
}
159+
160+
}

0 commit comments

Comments
 (0)