Skip to content

Commit f7b3e33

Browse files
Added 'FileTest' and enhanced some other unit tests
1 parent 3efee7d commit f7b3e33

File tree

4 files changed

+370
-8
lines changed

4 files changed

+370
-8
lines changed

tests/phpbu/App/ArgsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ public function testGetOptionsShortH()
2424
$this->assertTrue($options['-h'], 'short option -h must be set');
2525
}
2626

27+
/**
28+
* Test short option -x
29+
*
30+
* @expectedException \phpbu\App\Exception
31+
*/
32+
public function testGetOptionsFail()
33+
{
34+
$args = new Args();
35+
$options = $args->getOptions(array('-x'));
36+
$this->assertFalse(true, 'short option x is invalid');
37+
}
38+
2739
/**
2840
* Test short option -V
2941
*/

tests/phpbu/Backup/CompressorTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
*/
1515
class CompressorTest extends \PHPUnit_Framework_TestCase
1616
{
17+
/**
18+
* Tests Compressor::create
19+
*
20+
* @expectedException \phpbu\App\Exception
21+
*/
22+
public function testCreateInvalid()
23+
{
24+
Compressor::create('/foo/bar');
25+
$this->assertFalse(true, 'Exception should be thrown');
26+
}
1727
/**
1828
* Test gzip compressor
1929
*/
@@ -53,4 +63,14 @@ public function testGzipWithPath()
5363
$this->assertEquals('gz', $gzip->getSuffix());
5464
$this->assertEquals('/usr/local/bin/gzip', $gzip->getCommand());
5565
}
66+
67+
/**
68+
* Tests Compressor::getExec
69+
*/
70+
public function testGetExec()
71+
{
72+
$zip = Compressor::create('zip');
73+
$exec = $zip->getExec(__FILE__, array('-f'));
74+
$this->assertEquals('zip -f \'' . __FILE__ . '\'', $exec->getExec());
75+
}
5676
}

tests/phpbu/Backup/FileTest.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
namespace phpbu\Backup;
3+
4+
/**
5+
* File test
6+
*
7+
* @package phpbu
8+
* @subpackage tests
9+
* @author Sebastian Feldmann <sebastian@phpbu.de>
10+
* @copyright Sebastian Feldmann <sebastian@phpbu.de>
11+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
12+
* @link http://www.phpbu.de/
13+
* @since Class available since Release 1.0.5
14+
*/
15+
class FileTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* Tests File::getFileInfo
19+
*/
20+
public function testGetFileInfo()
21+
{
22+
$spl = $this->getFileInfo();
23+
$file = new File($spl);
24+
$ret = $file->getFileInfo();
25+
26+
$this->assertEquals($spl, $ret, 'should be the same spl injected');
27+
}
28+
29+
/**
30+
* Tests File::getSize
31+
*/
32+
public function testGetSize()
33+
{
34+
$spl = $this->getFileInfo();
35+
$file = new File($spl);
36+
$size = $file->getSize();
37+
38+
$this->assertEquals($spl->getSize(), $size, 'size should match');
39+
}
40+
41+
/**
42+
* Tests File::getFilename
43+
*/
44+
public function testGetFilename()
45+
{
46+
$spl = $this->getFileInfo();
47+
$file = new File($spl);
48+
$filename = $file->getFilename();
49+
50+
$this->assertEquals($spl->getFilename(), $filename);
51+
}
52+
53+
/**
54+
* Tests File::getPath
55+
*/
56+
public function testGetPath()
57+
{
58+
$spl = $this->getFileInfo();
59+
$file = new File($spl);
60+
$path = $file->getPath();
61+
62+
$this->assertEquals($spl->getPath(), $path);
63+
}
64+
65+
/**
66+
* Tests File::getPathname
67+
*/
68+
public function testGetPathname()
69+
{
70+
$spl = $this->getFileInfo();
71+
$file = new File($spl);
72+
$path = $file->getPathname();
73+
74+
$this->assertEquals($spl->getPathname(), $path);
75+
}
76+
77+
/**
78+
* Tests File::getMTime
79+
*/
80+
public function testGetMTime()
81+
{
82+
$spl = $this->getFileInfo();
83+
$file = new File($spl);
84+
$time = $file->getMTime();
85+
86+
$this->assertEquals($spl->getMTime(), $time);
87+
}
88+
89+
/**
90+
* Tests File::isWritable
91+
*/
92+
public function testIsWritable()
93+
{
94+
$spl = $this->getDeletableFileInfo();
95+
$file = new File($spl);
96+
$writable = $file->isWritable();
97+
98+
unlink($spl->getPathname());
99+
100+
$this->assertEquals(true, $writable);
101+
}
102+
103+
/**
104+
* Tests File::unlink
105+
*/
106+
public function testUnink()
107+
{
108+
$spl = $this->getDeletableFileInfo();
109+
$file = new File($spl);
110+
$file->unlink();
111+
112+
$existing = file_exists($spl->getPathname());
113+
114+
$this->assertEquals(false, $existing);
115+
}
116+
117+
/**
118+
* Tests File::unlink
119+
*
120+
* @expectedException \phpbu\App\Exception
121+
*/
122+
public function testUninkFail()
123+
{
124+
$spl = $this->getDeletableFileInfo();
125+
$file = new File($spl);
126+
127+
// delete file so next unlink fails
128+
unlink($spl->getPathname());
129+
130+
$file->unlink();
131+
132+
$this->assertFalse(true, 'exception should be thrown');
133+
}
134+
135+
protected function getFileInfo()
136+
{
137+
$spl = new \SplFileInfo(__FILE__);
138+
return $spl;
139+
}
140+
141+
protected function getDeletableFileInfo()
142+
{
143+
$file = tempnam('.', '');
144+
$spl = new \SplFileInfo($file);
145+
return $spl;
146+
}
147+
}

0 commit comments

Comments
 (0)