-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelper.php
More file actions
113 lines (91 loc) · 2.38 KB
/
Helper.php
File metadata and controls
113 lines (91 loc) · 2.38 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
<?php
namespace GT\Input\Test\Helper;
use GT\Input\Input;
use GT\Input\InputData\InputData;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class Helper {
public static function getPostPizza():array {
return [
"toppings" => ["Mozzarella", "Basil", "Mushroom", "Peppers", "Onion", "Spinach"],
"name" => "Greg's pizza",
];
}
public static function getRandomKvp(int $num, string $prefix = ""):array {
$kvp = [];
for($i = 0; $i < $num; $i++) {
$key = "key-$i-$prefix" . uniqid();
$value = "value-$i-$prefix" . str_repeat(uniqid(), rand(1, 10));
$kvp[$key] = $value;
}
return $kvp;
}
public static function getRandomWhenCriteria(Input $input, bool $inInput = true):array {
if($inInput) {
$inputData = $input->getAll();
$inputDataArray = self::convertInputDataToArray($inputData);
$numberToCreate = rand(1, count($inputDataArray));
$keys = array_rand($inputDataArray, $numberToCreate);
}
else {
$inputDataArray = [];
$numberToCreate = rand(1, 100);
for($i = 0; $i < $numberToCreate; $i++) {
$inputDataArray[
"key-$i-madeup" . uniqid()
] = "value-$i-madeup" . uniqid();
}
$keys = array_rand($inputDataArray, $numberToCreate);
}
if(!is_array($keys)) {
$keys = [$keys];
}
$criteria = [];
foreach($keys as $k) {
$criteria[$k] = (string)$inputDataArray[$k];
}
return $criteria;
}
public static function convertInputDataToArray(InputData $inputData):array {
$array = [];
foreach($inputData as $key => $value) {
$array[$key] = $value;
}
return $array;
}
public static function getKeysFromInput(Input $input, int $num):array {
$keys = [];
foreach($input as $key => $value) {
$keys []= $key;
if(count($keys) >= $num) {
break;
}
}
return $keys;
}
public static function cleanUp() {
$testDirectory = Helper::getTestDirectory();
if(!is_dir($testDirectory)) {
return;
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$testDirectory,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileInfo) {
$action = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
$action($fileInfo->getRealPath());
}
rmdir($testDirectory);
}
public static function getTestDirectory() {
return implode(DIRECTORY_SEPARATOR, [
sys_get_temp_dir(),
"phpgt",
"input",
"test",
]);
}
}