|
| 1 | +<?php namespace NumenCode\SyncOps\Tests\Console; |
| 2 | + |
| 3 | +use Mockery; |
| 4 | +use Carbon\Carbon; |
| 5 | +use PluginTestCase; |
| 6 | +use NumenCode\SyncOps\Console\DbPush; |
| 7 | +use Illuminate\Support\Facades\Storage; |
| 8 | + |
| 9 | +class DbPushTest extends PluginTestCase |
| 10 | +{ |
| 11 | + public function setUp(): void |
| 12 | + { |
| 13 | + parent::setUp(); |
| 14 | + |
| 15 | + // Provide minimal DB config the command reads directly |
| 16 | + config()->set('database.default', 'mysql'); |
| 17 | + config()->set('database.connections.mysql', [ |
| 18 | + 'username' => 'user', |
| 19 | + 'password' => 'pass', |
| 20 | + 'database' => 'db', |
| 21 | + ]); |
| 22 | + |
| 23 | + // Stable timestamp |
| 24 | + Carbon::setTestNow(Carbon::create(2024, 1, 2, 3, 4, 5)); |
| 25 | + } |
| 26 | + |
| 27 | + public function tearDown(): void |
| 28 | + { |
| 29 | + // Cleanup any dump files created in cwd |
| 30 | + foreach (['2024-01-02_03_04_05.sql.gz', '2024-01-02_03_04_05.sql'] as $f) { |
| 31 | + if (is_file($f)) { |
| 32 | + @unlink($f); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Cleanup any moved files/directories created by tests |
| 37 | + $movedDir = 'local/backups/'; |
| 38 | + $movedFile = $movedDir . '2024-01-02_03_04_05.sql.gz'; |
| 39 | + |
| 40 | + if (is_file($movedFile)) { |
| 41 | + @unlink($movedFile); |
| 42 | + } |
| 43 | + |
| 44 | + if (is_dir($movedDir)) { |
| 45 | + // Attempt to remove directory if empty |
| 46 | + @rmdir($movedDir); |
| 47 | + // Also try removing parent 'local' if empty |
| 48 | + @rmdir('local'); |
| 49 | + } |
| 50 | + |
| 51 | + Mockery::close(); |
| 52 | + parent::tearDown(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test function: handle |
| 57 | + * With no cloud specified, creates gzip dump and returns SUCCESS without moving/deleting. |
| 58 | + */ |
| 59 | + public function testHandleCreatesGzipDumpLocally(): void |
| 60 | + { |
| 61 | + $cmd = Mockery::mock(DbPush::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
| 62 | + |
| 63 | + // options/args defaults |
| 64 | + $cmd->shouldReceive('option')->with('folder')->andReturnNull(); |
| 65 | + $cmd->shouldReceive('option')->with('timestamp')->andReturnNull(); |
| 66 | + $cmd->shouldReceive('option')->with('no-gzip')->andReturnNull(); // gzip enabled |
| 67 | + $cmd->shouldReceive('argument')->with('cloud')->andReturnNull(); |
| 68 | + |
| 69 | + // Expect mysqldump with gzip to the known filename |
| 70 | + $expectedFile = '2024-01-02_03_04_05.sql.gz'; |
| 71 | + $cmd->shouldReceive('runLocalCommand')->once()->with( |
| 72 | + "mysqldump -uuser -ppass db | gzip > {$expectedFile}" |
| 73 | + )->andReturnUsing(function () use ($expectedFile) { |
| 74 | + file_put_contents($expectedFile, 'dump'); |
| 75 | + return ''; |
| 76 | + }); |
| 77 | + |
| 78 | + // Allow console output noise |
| 79 | + $cmd->shouldReceive('newLine')->atLeast()->once(); |
| 80 | + $cmd->shouldReceive('line')->atLeast()->once(); |
| 81 | + $cmd->shouldReceive('comment')->atLeast()->once(); |
| 82 | + $cmd->shouldReceive('info')->atLeast()->once(); |
| 83 | + |
| 84 | + $result = $cmd->handle(); |
| 85 | + |
| 86 | + $this->assertSame(DbPush::SUCCESS, $result); |
| 87 | + $this->assertFileExists($expectedFile); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test function: handle |
| 92 | + * Uploads to cloud then deletes local file by default. |
| 93 | + */ |
| 94 | + public function testHandleUploadsToCloudAndDeletesLocal(): void |
| 95 | + { |
| 96 | + $cmd = Mockery::mock(DbPush::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
| 97 | + |
| 98 | + // Provide folder option; default gzip |
| 99 | + $cmd->shouldReceive('option')->with('folder')->andReturn('backups'); |
| 100 | + $cmd->shouldReceive('option')->with('timestamp')->andReturn('Y-m-d_H_i_s'); |
| 101 | + $cmd->shouldReceive('option')->with('no-gzip')->andReturnNull(); |
| 102 | + $cmd->shouldReceive('option')->with('no-delete')->andReturnNull(); |
| 103 | + $cmd->shouldReceive('argument')->with('cloud')->andReturn('s3'); |
| 104 | + |
| 105 | + $expectedFile = '2024-01-02_03_04_05.sql.gz'; |
| 106 | + $expectedKey = 'backups/' . $expectedFile; |
| 107 | + |
| 108 | + // runLocalCommand creates the file we will upload |
| 109 | + $cmd->shouldReceive('runLocalCommand')->once()->with( |
| 110 | + "mysqldump -uuser -ppass db | gzip > {$expectedFile}" |
| 111 | + )->andReturnUsing(function () use ($expectedFile) { |
| 112 | + file_put_contents($expectedFile, 'dump'); |
| 113 | + return ''; |
| 114 | + }); |
| 115 | + |
| 116 | + // Mock Storage facade to return a disk mock that receives the upload once |
| 117 | + $cloud = Mockery::mock(); |
| 118 | + $cloud->shouldReceive('put')->once()->with($expectedKey, Mockery::type('resource'))->andReturnTrue(); |
| 119 | + Storage::shouldReceive('disk')->once()->with('s3')->andReturn($cloud); |
| 120 | + |
| 121 | + // Allow console outputs |
| 122 | + $cmd->shouldReceive('newLine')->atLeast()->once(); |
| 123 | + $cmd->shouldReceive('line')->atLeast()->once(); |
| 124 | + $cmd->shouldReceive('comment')->atLeast()->once(); |
| 125 | + $cmd->shouldReceive('info')->atLeast()->once(); |
| 126 | + |
| 127 | + $result = $cmd->handle(); |
| 128 | + |
| 129 | + $this->assertSame(DbPush::SUCCESS, $result); |
| 130 | + $this->assertFileDoesNotExist($expectedFile); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Test function: handle + moveFile |
| 135 | + * Uploads to cloud, keeps local file (no-delete) and moves it into provided folder. |
| 136 | + */ |
| 137 | + public function testHandleUploadsWithoutDeleteThenMovesIntoFolder(): void |
| 138 | + { |
| 139 | + $cmd = Mockery::mock(DbPush::class)->makePartial()->shouldAllowMockingProtectedMethods(); |
| 140 | + |
| 141 | + $cmd->shouldReceive('option')->with('folder')->andReturn('local/backups/'); |
| 142 | + $cmd->shouldReceive('option')->with('timestamp')->andReturnNull(); |
| 143 | + $cmd->shouldReceive('option')->with('no-gzip')->andReturnNull(); |
| 144 | + $cmd->shouldReceive('option')->with('no-delete')->andReturnTrue(); |
| 145 | + $cmd->shouldReceive('argument')->with('cloud')->andReturn('s3'); |
| 146 | + |
| 147 | + $expectedFile = '2024-01-02_03_04_05.sql.gz'; |
| 148 | + $expectedKey = 'local/backups/' . $expectedFile; |
| 149 | + |
| 150 | + $cmd->shouldReceive('runLocalCommand')->once()->with( |
| 151 | + "mysqldump -uuser -ppass db | gzip > {$expectedFile}" |
| 152 | + )->andReturnUsing(function () use ($expectedFile) { |
| 153 | + file_put_contents($expectedFile, 'dump'); |
| 154 | + return ''; |
| 155 | + }); |
| 156 | + |
| 157 | + // Mock cloud upload |
| 158 | + $cloud = Mockery::mock(); |
| 159 | + $cloud->shouldReceive('put')->once()->with($expectedKey, Mockery::type('resource'))->andReturnTrue(); |
| 160 | + Storage::shouldReceive('disk')->once()->with('s3')->andReturn($cloud); |
| 161 | + |
| 162 | + // Allow console outputs |
| 163 | + $cmd->shouldReceive('newLine')->atLeast()->once(); |
| 164 | + $cmd->shouldReceive('line')->atLeast()->once(); |
| 165 | + $cmd->shouldReceive('comment')->atLeast()->once(); |
| 166 | + $cmd->shouldReceive('info')->atLeast()->once(); |
| 167 | + |
| 168 | + $result = $cmd->handle(); |
| 169 | + |
| 170 | + $this->assertSame(DbPush::SUCCESS, $result); |
| 171 | + // Ensure file was moved locally into the folder |
| 172 | + $this->assertFileExists($expectedKey); |
| 173 | + $this->assertFileDoesNotExist($expectedFile); |
| 174 | + } |
| 175 | +} |
0 commit comments