Skip to content

Commit b959cab

Browse files
committed
tests have been optimized and speeded up
1 parent 829f638 commit b959cab

File tree

6 files changed

+149
-141
lines changed

6 files changed

+149
-141
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 2.x branch
22
## 2.6 branch
33
### 2.6.6
4+
* tests have been optimized and speeded up;
45
* APIs are now generated by `phpDocumentor` and no longer by` apigen`.
56

67
### 2.6.5

src/TestSuite/DriverTestCase.php

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
namespace DatabaseBackup\TestSuite;
1515

1616
use Cake\Core\Configure;
17-
use Cake\Database\Connection;
1817
use Cake\Event\EventList;
1918
use DatabaseBackup\TestSuite\TestCase;
20-
use ErrorException;
2119

2220
/**
2321
* DriverTestCase class.
@@ -100,31 +98,6 @@ final protected function getAllRecords()
10098
return $records;
10199
}
102100

103-
/**
104-
* Internal method to mock a driver
105-
* @param array $methods The list of methods to mock
106-
* @return \DatabaseBackup\Driver\Driver|\PHPUnit_Framework_MockObject_MockObject
107-
* @since 2.6.1
108-
* @uses $Driver
109-
*/
110-
final protected function getMockForDriver(array $methods)
111-
{
112-
return $this->getMockBuilder(get_class($this->Driver))
113-
->setMethods($methods)
114-
->setConstructorArgs([$this->getConnection()])
115-
->getMock();
116-
}
117-
118-
/**
119-
* Test for `__construct()` method
120-
* @return void
121-
* @test
122-
*/
123-
public function testConstruct()
124-
{
125-
$this->assertInstanceof(Connection::class, $this->getProperty($this->Driver, 'connection'));
126-
}
127-
128101
/**
129102
* Test for `export()` method
130103
* @return void
@@ -227,50 +200,6 @@ public function testExportExecutableWithCompression()
227200
}
228201
}
229202

230-
/**
231-
* Test for `export()` method on failure
232-
* @return void
233-
* @since 2.6.2
234-
* @test
235-
*/
236-
public function testExportOnFailure()
237-
{
238-
$this->expectException(ErrorException::class);
239-
$this->expectExceptionMessageRegExp('/^Failed with exit code `\d`$/');
240-
//Sets a no existing database
241-
$config = ['database' => 'noExisting'] + $this->Driver->getConfig();
242-
$this->setProperty($this->Driver, 'connection', new Connection($config));
243-
$this->Driver->export($this->getAbsolutePath('example.sql'));
244-
}
245-
246-
/**
247-
* Test for `export()` method. Export is stopped because the
248-
* `beforeExport()` method returns `false`
249-
* @return void
250-
* @test
251-
*/
252-
public function testExportStoppedByBeforeExport()
253-
{
254-
$backup = $this->getAbsolutePath('example.sql');
255-
$Driver = $this->getMockForDriver(['beforeExport']);
256-
$Driver->method('beforeExport')->will($this->returnValue(false));
257-
$this->assertFalse($Driver->export($backup));
258-
$this->assertFileNotExists($backup);
259-
}
260-
261-
/**
262-
* Test for `getConfig()` method
263-
* @return void
264-
* @test
265-
*/
266-
public function testGetConfig()
267-
{
268-
$this->assertNotEmpty($this->Driver->getConfig());
269-
$this->assertIsArray($this->Driver->getConfig());
270-
$this->assertNotEmpty($this->Driver->getConfig('name'));
271-
$this->assertNull($this->Driver->getConfig('noExistingKey'));
272-
}
273-
274203
/**
275204
* Test for `import()` method
276205
* @return void
@@ -318,39 +247,4 @@ public function testImportExecutableWithCompression()
318247
$this->assertEquals($expected, $result);
319248
}
320249
}
321-
322-
/**
323-
* Test for `import()` method on failure
324-
* @return void
325-
* @since 2.6.2
326-
* @test
327-
*/
328-
public function testImportOnFailure()
329-
{
330-
$backup = $this->getAbsolutePath('example.sql');
331-
332-
$this->expectException(ErrorException::class);
333-
$this->expectExceptionMessageRegExp('/^Failed with exit code `\d`$/');
334-
$this->Driver->export($backup);
335-
336-
//Sets a no existing database
337-
$config = ['database' => 'noExisting'] + $this->Driver->getConfig();
338-
$this->setProperty($this->Driver, 'connection', new Connection($config));
339-
$this->Driver->import($backup);
340-
}
341-
342-
/**
343-
* Test for `import()` method. Import is stopped because the
344-
* `beforeImport()` method returns `false`
345-
* @return void
346-
* @test
347-
*/
348-
public function testImportStoppedByBeforeExport()
349-
{
350-
$backup = $this->getAbsolutePath('example.sql');
351-
$Driver = $this->getMockForDriver(['beforeImport']);
352-
$Driver->method('beforeImport')->will($this->returnValue(false));
353-
$this->assertTrue($Driver->export($backup));
354-
$this->assertFalse($Driver->import($backup));
355-
}
356250
}

src/TestSuite/TestCase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ protected function createSomeBackups($sleep = false)
8282

8383
return $files;
8484
}
85+
86+
/**
87+
* Internal method to mock a driver
88+
* @param string $className Driver class name
89+
* @param array $methods The list of methods to mock
90+
* @return \DatabaseBackup\Driver\Driver|\PHPUnit_Framework_MockObject_MockObject
91+
*/
92+
protected function getMockForDriver($className, array $methods)
93+
{
94+
return $this->getMockBuilder($className)
95+
->setMethods($methods)
96+
->setConstructorArgs([$this->getConnection('test')])
97+
->getMock();
98+
}
8599
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
/**
4+
* This file is part of cakephp-database-backup.
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Mirko Pagliai
11+
* @link https://github.com/mirko-pagliai/cakephp-database-backup
12+
* @license https://opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
15+
namespace DatabaseBackup\Test\TestCase\Driver;
16+
17+
use Cake\Database\Connection;
18+
use DatabaseBackup\Driver\Mysql;
19+
use DatabaseBackup\TestSuite\TestCase;
20+
use ErrorException;
21+
22+
/**
23+
* DriverTest class
24+
*/
25+
class DriverTest extends TestCase
26+
{
27+
/**
28+
* `Driver` instance
29+
* @var \DatabaseBackup\Driver\Driver
30+
*/
31+
protected $Driver;
32+
33+
/**
34+
* Called before every test method
35+
* @return void
36+
*/
37+
public function setUp()
38+
{
39+
parent::setUp();
40+
41+
$this->Driver = new Mysql($this->getConnection('test'));
42+
}
43+
44+
/**
45+
* Test for `__construct()` method
46+
* @return void
47+
* @test
48+
*/
49+
public function testConstruct()
50+
{
51+
$this->assertInstanceof(Connection::class, $this->getProperty($this->Driver, 'connection'));
52+
}
53+
54+
/**
55+
* Test for `export()` method on failure
56+
* @return void
57+
* @since 2.6.2
58+
* @test
59+
*/
60+
public function testExportOnFailure()
61+
{
62+
$this->expectException(ErrorException::class);
63+
$this->expectExceptionMessageRegExp('/^Failed with exit code `\d`$/');
64+
//Sets a no existing database
65+
$config = ['database' => 'noExisting'] + $this->Driver->getConfig();
66+
$this->setProperty($this->Driver, 'connection', new Connection($config));
67+
$this->Driver->export($this->getAbsolutePath('example.sql'));
68+
}
69+
70+
/**
71+
* Test for `export()` method. Export is stopped because the
72+
* `beforeExport()` method returns `false`
73+
* @return void
74+
* @test
75+
*/
76+
public function testExportStoppedByBeforeExport()
77+
{
78+
$backup = $this->getAbsolutePath('example.sql');
79+
$Driver = $this->getMockForDriver(Mysql::class, ['beforeExport']);
80+
$Driver->method('beforeExport')->will($this->returnValue(false));
81+
$this->assertFalse($Driver->export($backup));
82+
$this->assertFileNotExists($backup);
83+
}
84+
85+
/**
86+
* Test for `getConfig()` method
87+
* @return void
88+
* @test
89+
*/
90+
public function testGetConfig()
91+
{
92+
$this->assertNotEmpty($this->Driver->getConfig());
93+
$this->assertIsArray($this->Driver->getConfig());
94+
$this->assertNotEmpty($this->Driver->getConfig('name'));
95+
$this->assertNull($this->Driver->getConfig('noExistingKey'));
96+
}
97+
98+
/**
99+
* Test for `import()` method on failure
100+
* @return void
101+
* @since 2.6.2
102+
* @test
103+
*/
104+
public function testImportOnFailure()
105+
{
106+
$backup = $this->getAbsolutePath('example.sql');
107+
108+
$this->expectException(ErrorException::class);
109+
$this->expectExceptionMessageRegExp('/^Failed with exit code `\d`$/');
110+
$this->Driver->export($backup);
111+
112+
//Sets a no existing database
113+
$config = ['database' => 'noExisting'] + $this->Driver->getConfig();
114+
$this->setProperty($this->Driver, 'connection', new Connection($config));
115+
$this->Driver->import($backup);
116+
}
117+
118+
/**
119+
* Test for `import()` method. Import is stopped because the
120+
* `beforeImport()` method returns `false`
121+
* @return void
122+
* @test
123+
*/
124+
public function testImportStoppedByBeforeExport()
125+
{
126+
$backup = $this->getAbsolutePath('example.sql');
127+
$Driver = $this->getMockForDriver(Mysql::class, ['beforeImport']);
128+
$Driver->method('beforeImport')->will($this->returnValue(false));
129+
$this->assertTrue($Driver->export($backup));
130+
$this->assertFalse($Driver->import($backup));
131+
}
132+
}

tests/TestCase/Driver/MysqlTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testImportExecutableWithCompression()
8888
*/
8989
public function testAfterExport()
9090
{
91-
$driver = $this->getMockForDriver(['deleteAuthFile']);
91+
$driver = $this->getMockForDriver(MySql::class, ['deleteAuthFile']);
9292
$driver->expects($this->once())->method('deleteAuthFile');
9393
$this->assertNull($driver->afterExport());
9494
}
@@ -99,7 +99,7 @@ public function testAfterExport()
9999
*/
100100
public function testAfterImport()
101101
{
102-
$driver = $this->getMockForDriver(['deleteAuthFile']);
102+
$driver = $this->getMockForDriver(MySql::class, ['deleteAuthFile']);
103103
$driver->expects($this->once())->method('deleteAuthFile');
104104
$this->assertNull($driver->afterImport());
105105
}

tests/TestCase/Driver/SqliteTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,46 +60,13 @@ public function testImportExecutable()
6060
$this->assertEquals($expected, $this->invokeMethod($this->Driver, '_importExecutable'));
6161
}
6262

63-
/**
64-
* Test for `export()` method on failure
65-
* @test
66-
*/
67-
public function testExportOnFailure()
68-
{
69-
$this->Driver = $this->getMockForDriver(['_exportExecutableWithCompression']);
70-
$this->Driver->method('_exportExecutableWithCompression')
71-
->will($this->returnValue(sprintf(
72-
'%s %s .dump noExistingDir/dump.sql' . REDIRECT_TO_DEV_NULL,
73-
$this->getBinary('sqlite3'),
74-
$this->Driver->getConfig('database')
75-
)));
76-
parent::testExportOnFailure();
77-
}
78-
7963
/**
8064
* Test for `import()` method
8165
* @test
8266
*/
8367
public function testImport()
8468
{
8569
$this->loadFixtures();
86-
8770
parent::testImport();
8871
}
89-
90-
/**
91-
* Test for `import()` method on failure
92-
* @test
93-
*/
94-
public function testImportOnFailure()
95-
{
96-
$this->Driver = $this->getMockForDriver(['_importExecutableWithCompression', 'beforeImport']);
97-
$this->Driver->method('beforeImport')->will($this->returnValue(true));
98-
$this->Driver->method('_importExecutableWithCompression')->will($this->returnValue(sprintf(
99-
'%s %s .dump noExisting' . REDIRECT_TO_DEV_NULL,
100-
$this->getBinary('sqlite3'),
101-
$this->Driver->getConfig('database')
102-
)));
103-
parent::testImportOnFailure();
104-
}
10572
}

0 commit comments

Comments
 (0)