Skip to content

Commit fe7a73a

Browse files
committed
fixed tests
1 parent cb07fc5 commit fe7a73a

File tree

8 files changed

+175
-87
lines changed

8 files changed

+175
-87
lines changed

src/TestSuite/TestCase.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,22 @@ protected function createBackup(string $filename = 'backup.sql'): string
6868

6969
/**
7070
* Internal method to creates some backup files
71-
* @param bool $sleep If `true`, waits a second for each backup
7271
* @return array
7372
* @uses createBackup()
7473
*/
75-
protected function createSomeBackups(bool $sleep = false): array
74+
protected function createSomeBackups(): array
7675
{
77-
$files[] = $this->createBackup();
76+
$timestamp = time();
7877

79-
$sleep ? sleep(1) : null;
80-
$files[] = $this->createBackup('backup.sql.bz2');
81-
82-
$sleep ? sleep(1) : null;
83-
$files[] = $this->createBackup('backup.sql.gz');
78+
foreach ([
79+
'sql' => $timestamp - 2,
80+
'sql.bz2' => $timestamp - 1,
81+
'sql.gz' => $timestamp,
82+
] as $extension => $timestamp) {
83+
$file = $this->createBackup('backup_test_' . (string)$timestamp . '.' . $extension);
84+
touch($file, $timestamp);
85+
$files[] = $file;
86+
}
8487

8588
return $files;
8689
}

tests/TestCase/Command/DeleteAllCommandTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,36 @@ class DeleteAllCommandTest extends TestCase
2424
{
2525
use ConsoleIntegrationTestTrait;
2626

27+
/**
28+
* @var string
29+
*/
30+
protected $command = 'database_backup.delete_all -v';
31+
2732
/**
2833
* Test for `execute()` method
2934
* @test
3035
*/
3136
public function testExecute()
3237
{
33-
$command = 'database_backup.delete_all -v';
38+
$files = $this->createSomeBackups();
39+
$this->exec($this->command);
40+
$this->assertExitWithSuccess();
41+
foreach ($files as $file) {
42+
$this->assertOutputContains('Backup `' . basename($file) . '` has been deleted');
43+
}
44+
$this->assertOutputContains('<success>Deleted backup files: 3</success>');
45+
}
3446

35-
$this->exec($command);
47+
/**
48+
* Test for `execute()` method, with no backups
49+
* @test
50+
*/
51+
public function testExecuteNoBackups()
52+
{
53+
$this->exec($this->command);
3654
$this->assertExitWithSuccess();
3755
$this->assertOutputContains('Connection: test');
3856
$this->assertOutputContains('Driver: Mysql');
3957
$this->assertOutputContains('No backup has been deleted');
40-
41-
$this->createSomeBackups(true);
42-
$this->exec($command);
43-
$this->assertExitWithSuccess();
44-
$this->assertOutputContains('Backup `backup.sql.gz` has been deleted');
45-
$this->assertOutputContains('Backup `backup.sql.bz2` has been deleted');
46-
$this->assertOutputContains('Backup `backup.sql` has been deleted');
47-
$this->assertOutputContains('<success>Deleted backup files: 3</success>');
4858
}
4959
}

tests/TestCase/Command/ExportCommandTest.php

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
namespace DatabaseBackup\Test\TestCase\Command;
1616

17-
use Cake\Core\Configure;
1817
use DatabaseBackup\TestSuite\TestCase;
1918
use MeTools\TestSuite\ConsoleIntegrationTestTrait;
2019

@@ -25,51 +24,79 @@ class ExportCommandTest extends TestCase
2524
{
2625
use ConsoleIntegrationTestTrait;
2726

27+
/**
28+
* @var string
29+
*/
30+
protected $command = 'database_backup.export -v';
31+
2832
/**
2933
* Test for `execute()` method
3034
* @test
3135
*/
3236
public function testExecute()
3337
{
34-
$command = 'database_backup.export -v';
35-
$targetRegex = preg_quote(Configure::read('DatabaseBackup.target') . DS, '/');
36-
37-
//Exports, without params
38-
$this->exec($command);
38+
$this->exec($this->command);
3939
$this->assertExitWithSuccess();
4040
$this->assertOutputContains('Connection: test');
4141
$this->assertOutputContains('Driver: Mysql');
42-
$this->assertOutputRegExp('/Backup `' . $targetRegex . 'backup_test_\d+\.sql` has been exported/');
42+
$this->assertOutputRegExp('/Backup `[\w\-\/]+\/backup_test_\d+\.sql` has been exported/');
43+
}
4344

44-
//Exports, with `compression` param
45-
sleep(1);
46-
$this->exec($command . ' --compression bzip2');
45+
/**
46+
* Test for `execute()` method, with `compression` param
47+
* @test
48+
*/
49+
public function testExecuteCompressionParam()
50+
{
51+
$this->exec($this->command . ' --compression bzip2');
4752
$this->assertExitWithSuccess();
48-
$this->assertOutputRegExp('/Backup `' . $targetRegex . 'backup_test_\d+\.sql\.bz2` has been exported/');
53+
$this->assertOutputRegExp('/Backup `[\w\-\/]+\/backup_test_\d+\.sql\.bz2` has been exported/');
54+
}
4955

50-
//Exports, with `filename` param
51-
sleep(1);
52-
$this->exec($command . ' --filename backup.sql');
56+
/**
57+
* Test for `execute()` method, with `filename` param
58+
* @test
59+
*/
60+
public function testExecuteFilenameParam()
61+
{
62+
$this->exec($this->command . ' --filename backup.sql');
5363
$this->assertExitWithSuccess();
54-
$this->assertOutputRegExp('/Backup `' . $targetRegex . 'backup.sql` has been exported/');
64+
$this->assertOutputRegExp('/Backup `[\w\-\/]+\/backup.sql` has been exported/');
65+
}
5566

56-
//Exports, with `rotate` param
57-
sleep(1);
58-
$this->exec($command . ' --rotate 3 -v');
67+
/**
68+
* Test for `execute()` method, with `rotate` param
69+
* @test
70+
*/
71+
public function testExecuteRotateParam()
72+
{
73+
$files = $this->createSomeBackups();
74+
$this->exec($this->command . ' --rotate 3 -v');
5975
$this->assertExitWithSuccess();
60-
$this->assertOutputRegExp('/Backup `' . $targetRegex . 'backup_test_\d+\.sql` has been exported/');
61-
$this->assertOutputRegExp('/Backup `backup_test_\d+\.sql` has been deleted/');
76+
$this->assertOutputRegExp('/Backup `[\w\-\/]+\/backup_test_\d+\.sql` has been exported/');
77+
$this->assertOutputContains('Backup `' . basename(array_value_first($files)) . '` has been deleted');
6278
$this->assertOutputContains('<success>Deleted backup files: 1</success>');
79+
}
6380

64-
//Exports, with `send` param
65-
sleep(1);
66-
$this->exec($command . ' --send mymail@example.com');
81+
/**
82+
* Test for `execute()` method, with `send` param
83+
* @test
84+
*/
85+
public function testExecuteSendParam()
86+
{
87+
$this->exec($this->command . ' --send mymail@example.com');
6788
$this->assertExitWithSuccess();
68-
$this->assertOutputRegExp('/Backup `' . $targetRegex . 'backup_test_\d+\.sql` has been exported/');
69-
$this->assertOutputRegExp('/Backup `' . $targetRegex . 'backup_test_\d+\.sql` was sent via mail/');
89+
$this->assertOutputRegExp('/Backup `[\w\-\/]+\/backup_test_\d+\.sql` has been exported/');
90+
$this->assertOutputRegExp('/Backup `[\w\-\/]+\/backup_test_\d+\.sql` was sent via mail/');
91+
}
7092

71-
//With an invalid option value
72-
$this->exec($command . ' --filename /noExistingDir/backup.sql');
93+
/**
94+
* Test for `execute()` method, with an invalid option value
95+
* @test
96+
*/
97+
public function testExecuteInvalidOptionValue()
98+
{
99+
$this->exec($this->command . ' --filename /noExistingDir/backup.sql');
73100
$this->assertExitWithError();
74101
}
75102
}

tests/TestCase/Command/ImportCommandTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,32 @@ class ImportCommandTest extends TestCase
2424
{
2525
use ConsoleIntegrationTestTrait;
2626

27+
/**
28+
* @var string
29+
*/
30+
protected $command = 'database_backup.import -v';
31+
2732
/**
2833
* Test for `execute()` method
2934
* @test
3035
*/
3136
public function testExecute()
3237
{
33-
$command = 'database_backup.import -v';
3438
$backup = $this->createBackup();
35-
36-
$this->exec($command . ' ' . $backup);
39+
$this->exec($this->command . ' ' . $backup);
3740
$this->assertExitWithSuccess();
3841
$this->assertOutputContains('Connection: test');
3942
$this->assertOutputContains('Driver: Mysql');
4043
$this->assertOutputContains('<success>Backup `' . $backup . '` has been imported</success>');
44+
}
4145

42-
//With a no existing filename
43-
$this->exec($command . ' /noExistingDir/backup.sql');
46+
/**
47+
* Test for `execute()` method, with a no existing file
48+
* @test
49+
*/
50+
public function testExecuteNoExistingFile()
51+
{
52+
$this->exec($this->command . ' /noExistingDir/backup.sql');
4453
$this->assertExitWithError();
4554
}
4655
}

tests/TestCase/Command/IndexCommandTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,36 @@ class IndexCommandTest extends TestCase
2424
{
2525
use ConsoleIntegrationTestTrait;
2626

27+
/**
28+
* @var string
29+
*/
30+
protected $command = 'database_backup.index -v';
31+
2732
/**
2833
* Test for `execute()` method
2934
* @test
3035
*/
3136
public function testExecute()
3237
{
33-
$command = 'database_backup.index -v';
34-
35-
$this->exec($command);
36-
$this->assertExitWithSuccess();
37-
$this->assertOutputContains('Connection: test');
38-
$this->assertOutputContains('Driver: Mysql');
39-
$this->assertOutputContains('Backup files found: 0');
40-
41-
$this->createSomeBackups(true);
42-
$this->exec($command);
38+
$this->createSomeBackups();
39+
$this->exec($this->command);
4340
$this->assertExitWithSuccess();
4441
$this->assertOutputContains('Backup files found: 3');
4542
$this->assertOutputRegExp('/backup\.sql\.gz\s+|\s+sql\.gz\s+|\s+gzip\s+|\s+[\d\.]+ \w+\s+|\s+[\d\/]+, [\d:]+ (AP)M/');
4643
$this->assertOutputRegExp('/backup\.sql\.bz2\s+|\s+sql\.bz2\s+|\s+bzip2\s+|\s+[\d\.]+ \w+\s+|\s+[\d\/]+, [\d:]+ (AP)M/');
4744
$this->assertOutputRegExp('/backup\.sq\s+|\s+sql\s+|\s+|\s+[\d\.]+ \w+\s+|\s+[\d\/]+, [\d:]+ (AP)M/');
4845
}
46+
47+
/**
48+
* Test for `execute()` method, with no backups
49+
* @test
50+
*/
51+
public function testExecuteNoBackups()
52+
{
53+
$this->exec($this->command);
54+
$this->assertExitWithSuccess();
55+
$this->assertOutputContains('Connection: test');
56+
$this->assertOutputContains('Driver: Mysql');
57+
$this->assertOutputContains('Backup files found: 0');
58+
}
4959
}

tests/TestCase/Command/RotateCommandTest.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,45 @@ class RotateCommandTest extends TestCase
2424
{
2525
use ConsoleIntegrationTestTrait;
2626

27+
/**
28+
* @var string
29+
*/
30+
protected $command = 'database_backup.rotate -v';
31+
2732
/**
2833
* Test for `execute()` method
2934
* @test
3035
*/
3136
public function testExecute()
3237
{
33-
$command = 'database_backup.rotate -v';
38+
$this->createSomeBackups();
39+
$this->exec($this->command . ' 1');
40+
$this->assertExitWithSuccess();
41+
$this->assertOutputRegExp('/Backup `backup_test_\d+\.sql\.bz2` has been deleted/');
42+
$this->assertOutputRegExp('/Backup `backup_test_\d+\.sql` has been deleted/');
43+
$this->assertOutputContains('<success>Deleted backup files: 2</success>');
44+
}
45+
46+
/**
47+
* Test for `execute()` method, with an invalid value
48+
* @test
49+
*/
50+
public function testExecuteInvalidValue()
51+
{
52+
$this->exec($this->command . ' string');
53+
$this->assertExitWithError();
54+
}
3455

35-
$this->exec($command . ' 1');
56+
/**
57+
* Test for `execute()` method, with no backups
58+
* @test
59+
*/
60+
public function testExecuteNoBackups()
61+
{
62+
$this->exec($this->command . ' 1');
3663
$this->assertExitWithSuccess();
3764
$this->assertOutputContains('Connection: test');
3865
$this->assertOutputContains('Driver: Mysql');
3966
$this->assertOutputContains('No backup has been deleted');
40-
41-
$this->createSomeBackups(true);
42-
$this->exec($command . ' 1');
43-
$this->assertExitWithSuccess();
44-
$this->assertOutputContains('Backup `backup.sql.bz2` has been deleted');
45-
$this->assertOutputContains('Backup `backup.sql` has been deleted');
46-
$this->assertOutputContains('<success>Deleted backup files: 2</success>');
47-
48-
//With an invalid value
49-
$this->exec($command . ' string');
50-
$this->assertExitWithError();
5167
}
5268
}

tests/TestCase/Command/SendCommandTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,43 @@ class SendCommandTest extends TestCase
2525
{
2626
use ConsoleIntegrationTestTrait;
2727

28+
/**
29+
* @var string
30+
*/
31+
protected $command = 'database_backup.send -v';
32+
2833
/**
2934
* Test for `execute()` method
3035
* @test
3136
*/
3237
public function testExecute()
3338
{
34-
$command = 'database_backup.send -v';
3539
$file = $this->createBackup();
36-
37-
$this->exec($command . ' ' . $file . ' recipient@example.com');
40+
$this->exec($this->command . ' ' . $file . ' recipient@example.com');
3841
$this->assertExitWithSuccess();
3942
$this->assertOutputContains('Connection: test');
4043
$this->assertOutputContains('Driver: Mysql');
4144
$this->assertOutputContains('<success>Backup `' . $file . '` was sent via mail</success>');
45+
}
4246

43-
//With a no existing filename
44-
$this->exec($command . ' /noExistingDir/backup.sql');
47+
/**
48+
* Test for `execute()` method, with no existing file
49+
* @test
50+
*/
51+
public function testExecuteNoExistingFile()
52+
{
53+
$this->exec($this->command . ' /noExistingDir/backup.sql');
4554
$this->assertExitWithError();
55+
}
4656

47-
//Without a sender in the configuration
57+
/**
58+
* Test for `execute()` method, with no sender configuration
59+
* @test
60+
*/
61+
public function testExecuteNoSender()
62+
{
4863
Configure::write('DatabaseBackup.mailSender', false);
49-
$this->exec($command . ' file.sql recipient@example.com');
64+
$this->exec($this->command . ' file.sql recipient@example.com');
5065
$this->assertExitWithError();
5166
}
5267
}

0 commit comments

Comments
 (0)