|
| 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