-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathPackageGeneratorTest.php
More file actions
151 lines (127 loc) · 4.44 KB
/
PackageGeneratorTest.php
File metadata and controls
151 lines (127 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace Pds\Skeleton;
class PackageGeneratorTest
{
public $numErrors = 0;
public static function run()
{
set_error_handler(function ($severity, $message, $file, $line) {
throw new \ErrorException($message, $severity, $severity, $file, $line);
});
$tester = new PackageGeneratorTest();
$tester->testGenerate_WithMissingBin_ReturnsBin();
$tester->testGenerate_WithExistingDir_SkipsDir();
$tester->testGenerate_WithExistingFile_SkipsFile();
$tester->testGenerate_WithBadPermissions_SkipsDirAndThrowsException();
echo __CLASS__ . " errors: {$tester->numErrors}" . PHP_EOL;
}
public function testGenerate_WithMissingBin_ReturnsBin()
{
$validatorResults = [
'bin/' => [
'state' => ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
'expected' => 'bin/',
],
'config/' => [
'state' => ComplianceValidator::STATE_INCORRECT_PRESENT,
'expected' => 'config/',
],
];
$files = (new PackageGenerator())->createFileList($validatorResults);
if (!array_key_exists('bin/', $files)) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected bin/ to be present" . PHP_EOL;
}
if (array_key_exists('config/', $files)) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected config/ to be absent" . PHP_EOL;
}
}
public function testGenerate_WithExistingDir_SkipsDir()
{
$validatorResults = [
'bin/' => [
'state' => ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
'expected' => 'bin/',
],
];
$tmp = __DIR__.'/tmp_allowed';
$bin = $tmp.'/bin';
if (!is_dir($tmp)) {
mkdir($tmp, 0755);
}
if (!is_dir($bin)) {
mkdir($bin, 0755);
}
try {
$createdFiles = (new PackageGenerator())->createFiles($validatorResults, $tmp);
} catch (GeneratorException $e) {
$this->numErrors++;
echo __FUNCTION__ . ": Unexpected exception" . PHP_EOL;
}
if (!array_key_exists('bin/', $createdFiles)) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected to return bin/" . PHP_EOL;
}
rmdir($tmp.'/bin');
rmdir($tmp);
}
public function testGenerate_WithExistingFile_SkipsFile()
{
$validatorResults = [
'LICENSE' => [
'state' => ComplianceValidator::STATE_RECOMMENDED_NOT_PRESENT,
'expected' => 'LICENSE',
],
];
$tmp = __DIR__.'/tmp_allowed';
$file = $tmp.'/LICENSE.md';
if (!is_dir($tmp)) {
mkdir($tmp, 0755);
}
touch($file);
file_put_contents($file, 'Original content');
try {
$createdFiles = (new PackageGenerator())->createFiles($validatorResults, $tmp);
} catch (GeneratorException $e) {
$this->numErrors++;
echo __FUNCTION__ . ': Unexpected exception' . PHP_EOL;
}
if (!array_key_exists('LICENSE', $createdFiles)) {
$this->numErrors++;
echo __FUNCTION__ . ': Expected to return file' . PHP_EOL;
}
if (file_get_contents($file) !== 'Original content') {
$this->numErrors++;
echo __FUNCTION__ . ': Expected to preserve original content' . PHP_EOL;
}
unlink($file);
rmdir($tmp);
}
public function testGenerate_WithBadPermissions_SkipsDirAndThrowsException()
{
$validatorResults = [
'bin/' => [
'state' => ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
'expected' => 'bin/',
],
];
$tmp = __DIR__.'/tmp_forbidden';
if (!is_dir($tmp)) {
mkdir($tmp, 0644);
}
try {
(new PackageGenerator())->createFiles($validatorResults, $tmp);
} catch (GeneratorException $e) {
rmdir($tmp);
return;
}
$this->numErrors++;
echo __FUNCTION__ . ": Expected exception, but none was thrown" . PHP_EOL;
if (!is_dir($tmp.'/bin')) {
$this->numErrors++;
echo __FUNCTION__ . ": Expected to not create a directory, but bin/ was created" . PHP_EOL;
}
rmdir($tmp);
}
}