-
-
Notifications
You must be signed in to change notification settings - Fork 745
/
BackupCommandTest.php
293 lines (209 loc) · 9.49 KB
/
BackupCommandTest.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
namespace Spatie\Backup\Tests\Commands;
use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use Spatie\Backup\Events\BackupHasFailed;
use Spatie\Backup\Tests\TestCase;
use Spatie\DbDumper\Compressors\GzipCompressor;
class BackupCommandTest extends TestCase
{
/** @var \Carbon\Carbon */
protected $date;
/** @var string */
protected $expectedZipPath;
public function setUp(): void
{
parent::setUp();
Event::fake();
$this->setNow(2016, 1, 1, 21, 1, 1);
$this->expectedZipPath = 'mysite/2016-01-01-21-01-01.zip';
config()->set('backup.backup.destination.disks', [
'local',
'secondLocal',
]);
config()->set('backup.backup.source.files.include', [base_path()]);
config()->set('backup.backup.source.databases', [
'db1',
'db2',
]);
}
/** @test */
public function it_can_backup_only_the_files()
{
$this->artisan('backup:run --only-files')->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
Storage::disk('secondLocal')->assertExists($this->expectedZipPath);
}
/** @test */
public function it_can_backup_using_a_custom_filename()
{
$this->date = Carbon::create('2016', 1, 1, 9, 1, 1);
Carbon::setTestNow($this->date);
config()->set('backup.backup.destination.filename_prefix', 'custom_name_');
$this->expectedZipPath = 'mysite/custom_name_2016-01-01-09-01-01.zip';
$this->artisan('backup:run', ['--only-files' => true])->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
Storage::disk('secondLocal')->assertExists($this->expectedZipPath);
}
/** @test */
public function it_includes_files_from_the_local_disks_in_the_backup()
{
config()->set('backup.backup.source.files.include', [$this->getDiskRootPath('local')]);
Storage::disk('local')->put('testing-file.txt', 'dummy content');
$this->artisan('backup:run --only-files')->assertExitCode(0);
$this->assertFileExistsInZip('local', $this->expectedZipPath, 'testing-file.txt');
}
/** @test */
public function it_excludes_the_backup_destination_from_the_backup()
{
config()->set('backup.backup.source.files.include', [$this->getDiskRootPath('local')]);
Storage::disk('local')->put('mysite/testing-file.txt', 'dummy content');
$this->artisan('backup:run --only-files');
$this->assertFileDoesntExistsInZip('local', $this->expectedZipPath, 'testing-file.txt');
}
/** @test */
public function it_excludes_the_temporary_directory_from_the_backup()
{
$tempDirectoryPath = storage_path('app/backup-temp/temp');
if (! file_exists($tempDirectoryPath)) {
mkdir($tempDirectoryPath, 0777, true);
}
touch($tempDirectoryPath.DIRECTORY_SEPARATOR.'testing-file-temp.txt');
$this->artisan('backup:run --only-files')->assertExitCode(0);
$this->assertFileDoesntExistsInZip('local', $this->expectedZipPath, 'testing-file-temp.txt');
}
/** @test */
public function it_can_backup_using_a_custom_filename_as_option()
{
$this->date = Carbon::create('2016', 1, 1, 9, 1, 1);
Carbon::setTestNow($this->date);
$filename = 'testing-filename.zip';
$this->expectedZipPath = 'mysite/'.$filename;
$this->artisan('backup:run', ['--only-files' => true, '--filename' => $filename])->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
Storage::disk('secondLocal')->assertExists($this->expectedZipPath);
}
/** @test */
public function it_can_backup_to_a_specific_disk()
{
$this->artisan('backup:run --only-files --only-to-disk=secondLocal')->assertExitCode(0);
Storage::disk('local')->assertMissing($this->expectedZipPath);
Storage::disk('secondLocal')->assertExists($this->expectedZipPath);
}
/** @test */
public function it_can_selectively_backup_db()
{
$this
->artisan('backup:run --only-db --db-name=db1')
->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
$this
->artisan('backup:run --only-db --db-name=db2')
->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
$this
->artisan('backup:run --only-db --db-name=db1 --db-name=db2')
->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
$this
->artisan('backup:run --only-db --db-name=wrongName')
->assertExitCode(1);
}
/** @test */
public function it_can_backup_twice_a_day_at_same_time_in_12h_clock()
{
// first backup
$this->date = Carbon::create('2016', 1, 1, 9, 1, 1);
Carbon::setTestNow($this->date);
$this->expectedZipPath = 'mysite/2016-01-01-09-01-01.zip';
$this->artisan('backup:run --only-files')->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
Storage::disk('secondLocal')->assertExists($this->expectedZipPath);
// second backup
$this->date = Carbon::create('2016', 1, 1, 21, 1, 1);
Carbon::setTestNow($this->date);
$this->expectedZipPath = 'mysite/2016-01-01-21-01-01.zip';
$this->artisan('backup:run --only-files')->assertExitCode(0);
Storage::disk('local')->assertExists($this->expectedZipPath);
Storage::disk('secondLocal')->assertExists($this->expectedZipPath);
}
/** @test */
public function it_will_fail_when_try_to_backup_only_the_files_and_only_the_db()
{
$resultCode = Artisan::call('backup:run --only-files --only-db');
$this->assertEquals(1, $resultCode);
$this->seeInConsoleOutput('Cannot use `only-db` and `only-files` together.');
Storage::disk('local')->assertMissing($this->expectedZipPath);
Storage::disk('secondLocal')->assertMissing($this->expectedZipPath);
}
/** @test */
public function it_will_fail_when_trying_to_backup_a_non_existing_database()
{
//use an invalid db name to trigger failure
Artisan::call('backup:run --only-files --only-db --db-name=wrongName');
$this->seeInConsoleOutput('Backup failed');
}
/** @test */
public function it_will_fail_when_trying_to_backup_to_an_non_existing_diskname()
{
$resultCode = Artisan::call('backup:run --only-to-disk=non-exisiting-disk');
$this->assertEquals(1, $resultCode);
$this->seeInConsoleOutput('There is no backup destination with a disk named');
Storage::disk('local')->assertMissing($this->expectedZipPath);
Storage::disk('secondLocal')->assertMissing($this->expectedZipPath);
}
/** @test */
public function it_will_fail_when_there_are_no_files_to_be_backed_up()
{
config()->set('backup.backup.source.files.include', []);
config()->set('backup.backup.source.databases', []);
Artisan::call('backup:run');
$this->seeInConsoleOutput('There are no files to be backed up');
}
/** @test */
public function it_appends_the_database_type_to_backup_file_name_to_prevent_overwrite()
{
config()->set('backup.backup.source.databases', ['db1', 'db2']);
$this->setUpDatabase($this->app);
$this->artisan('backup:run --only-db')->assertExitCode(0);
$this->assertFileExistsInZip('local', $this->expectedZipPath, 'sqlite-db1-database.sql');
$this->assertFileExistsInZip('local', $this->expectedZipPath, 'sqlite-db2-database.sql');
$this->assertFileExistsInZip('secondLocal', $this->expectedZipPath, 'sqlite-db1-database.sql');
$this->assertFileExistsInZip('secondLocal', $this->expectedZipPath, 'sqlite-db2-database.sql');
/*
* Close the database connection to unlock the sqlite file for deletion.
* This prevents the errors from other tests trying to delete and recreate the folder.
*/
$this->app['db']->disconnect();
}
/** @test */
public function it_should_trigger_the_backup_failed_event()
{
// use an invalid dbname to trigger failure
$this->artisan('backup:run --only-db --db-name=wrongName')->assertExitCode(1);
Event::assertDispatched(BackupHasFailed::class);
}
/** @test */
public function it_should_omit_the_backup_failed_event_with_no_notifications_flag()
{
//use an invalid dbname to trigger failure
$this->artisan('backup:run --only-db --db-name=wrongName --disable-notifications')->assertExitCode(1);
Event::assertNotDispatched(BackupHasFailed::class);
}
/** @test */
public function it_compresses_the_database_dump()
{
config()->set('backup.backup.source.databases', ['sqlite']);
config()->set('backup.backup.database_dump_compressor', GzipCompressor::class);
$this->setUpDatabase($this->app);
$this->artisan('backup:run --only-db')->assertExitCode(0);
$this->assertFileExistsInZip('local', $this->expectedZipPath, 'sqlite-sqlite-database.sql.gz');
/*
* Close the database connection to unlock the sqlite file for deletion.
* This prevents the errors from other tests trying to delete and recreate the folder.
*/
$this->app['db']->disconnect();
}
}