1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KamranAhmed\Smasher; |
4
|
|
|
|
5
|
|
|
class PathTest extends \PHPUnit_Framework_TestCase |
6
|
|
|
{ |
7
|
|
|
private $sampleFilePath; |
8
|
|
|
private $sampleDirPath; |
9
|
|
|
private $sampleFileCreate; |
10
|
|
|
private $invalidPath; |
11
|
|
|
|
12
|
|
|
public function setUp() |
13
|
|
|
{ |
14
|
|
|
$this->sampleFilePath = __DIR__ . '/data/scanned-samples/scanned-json.json'; |
15
|
|
|
$this->sampleDirPath = __DIR__ . '/data/output'; |
16
|
|
|
$this->sampleFileCreate = __DIR__ . '/data/output/new-file.txt'; |
17
|
|
|
$this->invalidPath = __DIR__ . '/data/non/existing/path'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testCanSetPath() |
21
|
|
|
{ |
22
|
|
|
$path = new Path($this->sampleFilePath); |
23
|
|
|
|
24
|
|
|
$actualPath = $path->getPath(); |
25
|
|
|
$this->assertEquals($this->sampleFilePath, $actualPath); |
26
|
|
|
|
27
|
|
|
// Check if the path can be updated through `setPath` |
28
|
|
|
$path->setPath($this->sampleDirPath); |
29
|
|
|
$actualPath = $path->getPath(); |
30
|
|
|
|
31
|
|
|
$this->assertEquals($this->sampleDirPath, $actualPath); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testValidPathPassesValidation() |
35
|
|
|
{ |
36
|
|
|
$path = new Path($this->sampleFilePath); |
37
|
|
|
$validated = $path->validate(); |
38
|
|
|
|
39
|
|
|
$this->assertTrue($validated); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @expectedException \KamranAhmed\Smasher\Exceptions\UnreadablePathException |
44
|
|
|
*/ |
45
|
|
|
public function testInvalidPathFailsValidation() |
46
|
|
|
{ |
47
|
|
|
$path = new Path($this->invalidPath); |
48
|
|
|
$path->validate(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testCanCreateFileAndSaveContent() |
52
|
|
|
{ |
53
|
|
|
$path = new Path($this->sampleFileCreate); |
54
|
|
|
$path->saveFileContent("Test content in file"); |
55
|
|
|
$this->assertFileExists($this->sampleFilePath); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException \KamranAhmed\Smasher\Exceptions\InvalidPathException |
60
|
|
|
*/ |
61
|
|
|
public function testCannotWriteContentToDirectoryPath() |
62
|
|
|
{ |
63
|
|
|
$path = new Path($this->sampleDirPath); |
64
|
|
|
$path->saveFileContent("Test content in directory"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testCanGetPath() |
68
|
|
|
{ |
69
|
|
|
$path = new Path($this->sampleFilePath); |
70
|
|
|
$this->assertEquals($this->sampleFilePath, $path->getPath()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @dataProvider itemTypesProvider |
75
|
|
|
*/ |
76
|
|
|
public function testCanCreateItemsAndGetDetails($toCreate, $detail) |
77
|
|
|
{ |
78
|
|
|
$path = new Path($toCreate); |
79
|
|
|
$path->createItem($detail); |
80
|
|
|
|
81
|
|
|
// Check if file was created |
82
|
|
|
$this->assertFileExists($toCreate); |
83
|
|
|
|
84
|
|
|
$detail = $path->getDetail(); |
85
|
|
|
|
86
|
|
|
// Check if the detail has everything that we need |
87
|
|
|
$this->assertArrayHasKey('@name', $detail); |
88
|
|
|
$this->assertArrayHasKey('@path', $detail); |
89
|
|
|
$this->assertArrayHasKey('@type', $detail); |
90
|
|
|
$this->assertArrayHasKey('@size', $detail); |
91
|
|
|
$this->assertArrayHasKey('@mode', $detail); |
92
|
|
|
$this->assertArrayHasKey('@owner', $detail); |
93
|
|
|
$this->assertArrayHasKey('@last_modified', $detail); |
94
|
|
|
$this->assertArrayHasKey('@group', $detail); |
95
|
|
|
|
96
|
|
|
// Verifying the get methods |
97
|
|
|
$this->assertEquals($detail['@name'], $path->getName()); |
98
|
|
|
$this->assertEquals($detail['@type'], $path->getType()); |
99
|
|
|
$this->assertInternalType('int', $path->getSize()); |
100
|
|
|
$this->assertRegexp('/\d{4}/', $path->getMode()); |
101
|
|
|
$this->assertInternalType('array', $path->getOwner()); |
102
|
|
|
$this->assertInternalType('string', $path->getLastModified()); |
103
|
|
|
$this->assertInternalType('array', $path->getGroup()); |
104
|
|
|
$this->assertNotEmpty('string', $path->getRealPath()); |
105
|
|
|
|
106
|
|
|
$type = $path->getType(); |
107
|
|
|
|
108
|
|
|
if ($type === 'file') { |
109
|
|
|
$content = $path->getFileContent(); |
110
|
|
|
$this->assertEquals($detail['@content'], $content); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($type == 'dir') { |
114
|
|
|
rmdir($toCreate); |
115
|
|
|
} elseif ($type == 'file' || $type == 'link') { |
116
|
|
|
unlink($toCreate); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function itemTypesProvider() |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
[ |
124
|
|
|
__DIR__ . '/data/output/test-dir', |
125
|
|
|
[ |
126
|
|
|
'@type' => 'dir', |
127
|
|
|
'@name' => 'test-dir', |
128
|
|
|
], |
129
|
|
|
], [ |
130
|
|
|
__DIR__ . '/data/output/test-file.txt', |
131
|
|
|
[ |
132
|
|
|
'@type' => 'file', |
133
|
|
|
'@content' => 'lorem ipsum', |
134
|
|
|
'@name' => 'test-file.txt', |
135
|
|
|
], |
136
|
|
|
], [ |
137
|
|
|
__DIR__ . '/data/output/test-link', |
138
|
|
|
[ |
139
|
|
|
'@type' => 'link', |
140
|
|
|
'@destination' => __DIR__ . '/data/sample-path', |
141
|
|
|
'@name' => 'test-file.txt', |
142
|
|
|
], |
143
|
|
|
], |
144
|
|
|
]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function tearDown() |
148
|
|
|
{ |
149
|
|
|
if (file_exists($this->sampleFileCreate)) { |
150
|
|
|
unlink($this->sampleFileCreate); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|