scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 namespace Neutron\TemporaryFilesystem\Tests;
4  
5 use Neutron\TemporaryFilesystem\TemporaryFilesystem;
6 use Symfony\Component\Filesystem\Filesystem;
7  
8 class TemporaryFilesystemTest extends \PHPUnit_Framework_TestCase
9 {
10 /**
11 * @var string $workspace
12 */
13 private $workspace = null;
14  
15 /**
16 * @var TemporaryFilesystem
17 */
18 private $filesystem;
19  
20 public function setUp()
21 {
22 parent::setUp();
23  
24 $this->workspace = sys_get_temp_dir().DIRECTORY_SEPARATOR.time().rand(0, 1000);
25 mkdir($this->workspace, 0777, true);
26 $this->workspace = realpath($this->workspace);
27 $this->filesystem = TemporaryFilesystem::create();
28 }
29  
30 public function tearDown()
31 {
32 $this->clean($this->workspace);
33 }
34  
35 public function testCreate()
36 {
37 $this->assertInstanceOf('Neutron\TemporaryFilesystem\TemporaryFilesystem', TemporaryFilesystem::create());
38 }
39  
40 public function testConctruct()
41 {
42 $this->assertInstanceOf('Neutron\TemporaryFilesystem\TemporaryFilesystem', new TemporaryFilesystem(new Filesystem()));
43 }
44  
45 /**
46 * @param string $file
47 */
48 private function clean($file)
49 {
50 if (is_dir($file) && !is_link($file)) {
51 $dir = new \FilesystemIterator($file);
52 foreach ($dir as $childFile) {
53 $this->clean($childFile);
54 }
55  
56 rmdir($file);
57 } else {
58 unlink($file);
59 }
60 }
61  
62 /**
63 * @dataProvider provideFilesToCreate
64 */
65 public function testCreateEmptyFile($prefix, $suffix, $extension, $maxTry, $pattern)
66 {
67 $createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
68 mkdir($createDir);
69  
70 $file = $this->filesystem->createEmptyFile($createDir, $prefix, $suffix, $extension, $maxTry);
71 $this->assertTrue(file_exists($file));
72 $this->assertEquals($createDir, dirname($file));
73 $this->assertEquals(0, filesize($file));
74 $this->assertRegExp($pattern, basename($file));
75 unlink($file);
76 }
77  
78 public function testCreateTemporaryDir()
79 {
80 $dir = $this->filesystem->createTemporaryDirectory();
81 $this->assertTrue(file_exists($dir));
82 $this->assertTrue(is_dir($dir));
83 rmdir($dir);
84 }
85  
86 public function testCreateTemporaryDirWithPrefix()
87 {
88 $dir = $this->filesystem->createTemporaryDirectory(0777, 200, 'neutron');
89 $this->assertTrue(file_exists($dir));
90 $this->assertTrue(is_dir($dir));
91 $this->assertContains('neutron', $dir);
92 rmdir($dir);
93 }
94  
95 public function provideFilesToCreate()
96 {
97 return array(
98 array(null, null, null, 10, '/\w{5}/'),
99 array('romain', null, null, 10, '/romain\w{5}/'),
100 array(null, 'neutron', null, 10, '/\w{5}neutron/'),
101 array(null, null, 'io', 10, '/\w{5}\.io/'),
102 array('romain', null, 'io', 10, '/romain\w{5}\.io/'),
103 array(null, 'neutron', 'io', 10, '/\w{5}neutron\.io/'),
104 array('romain', 'neutron', 'io', 10, '/romain\w{5}neutron\.io/'),
105 );
106 }
107  
108 /**
109 * @expectedException Neutron\TemporaryFilesystem\IOException
110 */
111 public function testCreateEmptyFileInvalidDir()
112 {
113 $createDir = $this->workspace . DIRECTORY_SEPARATOR . 'invalid-book-dir';
114  
115 $this->filesystem->createEmptyFile($createDir);
116 }
117  
118 /**
119 * @expectedException Neutron\TemporaryFilesystem\IOException
120 */
121 public function testCreateEmptyFileInvalidDirSecondMethod()
122 {
123 $createDir = $this->workspace . DIRECTORY_SEPARATOR . 'invalid-book-dir';
124  
125 $this->filesystem->createEmptyFile($createDir, 'romain', 'neutron');
126 }
127  
128 /**
129 * @expectedException Neutron\TemporaryFilesystem\IOException
130 */
131 public function testCreateEmptyFileFails()
132 {
133 $createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
134 mkdir($createDir);
135  
136 $this->filesystem->createEmptyFile($createDir, 'romain', 'neutron', null, 0);
137 }
138  
139 /**
140 * @expectedException Neutron\TemporaryFilesystem\IOException
141 */
142 public function testCreateEmptyFileOnFile()
143 {
144 $createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
145 touch($createDir);
146  
147 $this->filesystem->createEmptyFile($createDir, null, null, null);
148 }
149  
150 /**
151 * @expectedException Neutron\TemporaryFilesystem\IOException
152 */
153 public function testCreateEmptyFileOnFileSecondMethod()
154 {
155 $createDir = $this->workspace . DIRECTORY_SEPARATOR . 'book-dir';
156 touch($createDir);
157  
158 $this->filesystem->createEmptyFile($createDir, 'romain', 'neutron', 'io');
159 }
160  
161 /**
162 * @dataProvider provideFilesToCreate
163 */
164 public function testTemporaryFiles($prefix, $suffix, $extension, $maxTry, $pattern)
165 {
166 $files = $this->filesystem->createTemporaryFiles(3, $prefix, $suffix, $extension, $maxTry);
167 $this->assertEquals(3, count($files));
168  
169 foreach ($files as $file) {
170 $this->assertTrue(file_exists($file));
171 $this->assertEquals(realpath(sys_get_temp_dir()), realpath(dirname($file)));
172 $this->assertEquals(0, filesize($file));
173 $this->assertRegExp($pattern, basename($file));
174 }
175 }
176  
177 /**
178 * @dataProvider provideFilesToCreate
179 */
180 public function testTemporaryFile($prefix, $suffix, $extension, $maxTry, $pattern)
181 {
182 $file = $this->filesystem->createTemporaryFile($prefix, $suffix, $extension, $maxTry);
183 $this->assertInternalType('string', $file);
184  
185 $this->assertTrue(file_exists($file));
186 $this->assertEquals(realpath(sys_get_temp_dir()), realpath(dirname($file)));
187 $this->assertEquals(0, filesize($file));
188 $this->assertRegExp($pattern, basename($file));
189 }
190  
191 /**
192 * @expectedException Neutron\TemporaryFilesystem\IOException
193 */
194 public function testTemporaryFilesFails()
195 {
196 $this->filesystem->createTemporaryFiles(3, 'prefix', 'suffix', null, 0);
197 }
198  
199 /**
200 * @expectedException \InvalidArgumentException
201 */
202 public function testTemporaryFilesInvalidQuantity()
203 {
204 $this->filesystem->createTemporaryFiles(0);
205 }
206 }