1
|
|
|
<?php |
2
|
|
|
namespace Naneau\FileGen\Test\Generator; |
3
|
|
|
|
4
|
|
|
use Naneau\FileGen\Structure; |
5
|
|
|
use Naneau\FileGen\File; |
6
|
|
|
use Naneau\FileGen\Generator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Test file generation |
10
|
|
|
*/ |
11
|
|
|
class FileTest extends \Naneau\FileGen\Test\Generator\TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Test simple creation |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
**/ |
18
|
|
|
public function testCreation() |
19
|
|
|
{ |
20
|
|
|
$structure = new Structure; |
21
|
|
|
$structure |
22
|
|
|
->file('foo', 'foo contents') |
23
|
|
|
->file('/bar', 'bar contents', 0700) |
24
|
|
|
->file('baz/bar', 'baz/bar contents', 0775); |
25
|
|
|
|
26
|
|
|
$generator = $this->createGenerator(); |
27
|
|
|
$generator->generate($structure); |
28
|
|
|
|
29
|
|
|
// See if structure was generated |
30
|
|
|
$this->assertEquals( |
31
|
|
|
file_get_contents($generator->getRoot() . '/foo'), |
32
|
|
|
'foo contents' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$this->assertEquals( |
36
|
|
|
file_get_contents($generator->getRoot() . '/bar'), |
37
|
|
|
'bar contents' |
38
|
|
|
); |
39
|
|
|
$this->assertEquals( |
40
|
|
|
substr(sprintf('%o', fileperms($generator->getRoot() . '/bar')), -4), |
41
|
|
|
'0700' |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$this->assertEquals( |
45
|
|
|
file_get_contents($generator->getRoot() . '/baz/bar'), |
46
|
|
|
'baz/bar contents' |
47
|
|
|
); |
48
|
|
|
$this->assertEquals( |
49
|
|
|
substr(sprintf('%o', fileperms($generator->getRoot() . '/baz/bar')), -4), |
50
|
|
|
'0775' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test already exists |
56
|
|
|
* |
57
|
|
|
* @expectedException Naneau\FileGen\Generator\Exception\NodeExists |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
**/ |
61
|
|
View Code Duplication |
public function testAlreadyExists() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$structure = new Structure; |
64
|
|
|
$structure->file('foo', 'foo'); |
65
|
|
|
|
66
|
|
|
$generator = $this->createGenerator(); |
67
|
|
|
|
68
|
|
|
// dir exists already... oh noes. |
69
|
|
|
file_put_contents($generator->getRoot() . '/foo', 'foo'); |
70
|
|
|
|
71
|
|
|
$generator->generate($structure); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.