|
2 | 2 |
|
3 | 3 | namespace Skills17\PHPUnit;
|
4 | 4 |
|
| 5 | +use Error; |
5 | 6 | use ReflectionClass;
|
6 | 7 |
|
7 | 8 | class Config
|
8 | 9 | {
|
| 10 | + private static $instance; |
| 11 | + private $config; |
| 12 | + private $defaultConfig = [ |
| 13 | + 'database' => [ |
| 14 | + 'enabled' => false, |
| 15 | + 'dump' => './database.sql', |
| 16 | + 'name' => 'skills17', |
| 17 | + 'user' => 'root', |
| 18 | + 'password' => '', |
| 19 | + 'host' => '127.0.0.1', |
| 20 | + ], |
| 21 | + 'points' => [ |
| 22 | + 'defaultPoints' => 1.0, |
| 23 | + 'strategy' => 'add', |
| 24 | + ], |
| 25 | + 'groups' => [], |
| 26 | + ]; |
| 27 | + |
9 | 28 | /**
|
10 |
| - * Get the test configuration. |
11 |
| - * Default values can be overwritten with environment variables. |
| 29 | + * Create a new config instance and merge it with the default config values. |
12 | 30 | */
|
13 |
| - public static function get() |
| 31 | + private function __construct($config) |
| 32 | + { |
| 33 | + $this->config = $this->mergeConfig($this->defaultConfig, $config); |
| 34 | + $this->validate(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Get the project root folder |
| 39 | + */ |
| 40 | + public static function getProjectRoot(): string |
14 | 41 | {
|
15 | 42 | // get project root
|
16 | 43 | $classLoader = new ReflectionClass(\Composer\Autoload\ClassLoader::class);
|
17 | 44 | $projectRoot = dirname($classLoader->getFileName(), 3);
|
18 | 45 |
|
19 |
| - $configFile = $projectRoot() . 'config.json'; |
| 46 | + return $projectRoot . '/'; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the test configuration instance. |
| 51 | + */ |
| 52 | + public static function getInstance(): Config |
| 53 | + { |
| 54 | + if (self::$instance) { |
| 55 | + return self::$instance; |
| 56 | + } |
| 57 | + |
| 58 | + $configFile = self::getProjectRoot() . 'config.json'; |
20 | 59 |
|
21 | 60 | if (!file_exists($configFile)) {
|
22 |
| - echo "Config file (config.json) does not exist\n"; |
23 |
| - exit(1); |
| 61 | + throw new Error('Config file (' . $configFile . ') does not exist'); |
24 | 62 | }
|
25 | 63 |
|
26 | 64 | $jsonConfig = json_decode(file_get_contents($configFile), true);
|
27 | 65 |
|
28 | 66 | if ($jsonConfig === null) {
|
29 |
| - echo "Could not decode config file (config.json)\n"; |
30 |
| - exit(1); |
| 67 | + throw new Error('Could not decode config file (config.json)'); |
31 | 68 | }
|
32 | 69 |
|
33 |
| - return array_merge($jsonConfig, [ |
34 |
| - 'format' => getenv('FORMAT') ?? 'normal', |
| 70 | + self::$instance = new self($jsonConfig); |
| 71 | + |
| 72 | + return self::$instance; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Checks if a database should be used for the tests. |
| 77 | + */ |
| 78 | + public function hasDatabase(): bool |
| 79 | + { |
| 80 | + return $this->config['database']['enabled']; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets the database config. |
| 85 | + */ |
| 86 | + public function getDatabaseConfig(): array |
| 87 | + { |
| 88 | + $envConfig = array_filter([ |
| 89 | + 'name' => getenv('DB_NAME'), |
| 90 | + 'user' => getenv('DB_USER'), |
| 91 | + 'password' => getenv('DB_PASSWORD'), |
| 92 | + 'host' => getenv('DB_HOST'), |
35 | 93 | ]);
|
| 94 | + |
| 95 | + return array_merge($this->config['database'], $envConfig); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the default points. |
| 100 | + */ |
| 101 | + public function getDefaultPoints(): float |
| 102 | + { |
| 103 | + return $this->config['points']['defaultPoints']; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the points strategy. |
| 108 | + * Can be either 'add' or 'deduct'. |
| 109 | + */ |
| 110 | + public function getPointsStrategy(): string |
| 111 | + { |
| 112 | + return $this->config['points']['strategy']; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Gets all test groups |
| 117 | + */ |
| 118 | + public function getGroups(): array |
| 119 | + { |
| 120 | + return $this->config['groups'] ?? []; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get the output format. |
| 125 | + * Can be either 'json' or 'text'. |
| 126 | + */ |
| 127 | + public function getFormat(): string |
| 128 | + { |
| 129 | + return getenv('FORMAT') ?: 'text'; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Merge two configuration arrays. |
| 134 | + */ |
| 135 | + private function mergeConfig(array $config1, array $config2): array |
| 136 | + { |
| 137 | + $merged = $config1; |
| 138 | + |
| 139 | + foreach ($config2 as $key => & $value) { |
| 140 | + if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
| 141 | + $merged[$key] = $this->mergeConfig($merged[$key], $value); |
| 142 | + } elseif (is_numeric($key)) { |
| 143 | + if (!in_array($value, $merged)) { |
| 144 | + $merged[] = $value; |
| 145 | + } |
| 146 | + } else { |
| 147 | + $merged[$key] = $value; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return $merged; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Validates the current configuration. |
| 156 | + */ |
| 157 | + private function validate() |
| 158 | + { |
| 159 | + $format = $this->getFormat(); |
| 160 | + $strategy = $this->getPointsStrategy(); |
| 161 | + |
| 162 | + // validate if a valid format is specified |
| 163 | + if ($format !== 'text' && $format !== 'json') { |
| 164 | + throw new Error('config.json validation error: Invalid output format: ' . $format); |
| 165 | + } |
| 166 | + |
| 167 | + // validate points strategy |
| 168 | + if ($strategy !== 'add' && $strategy !== 'deduct') { |
| 169 | + throw new Error('config.json validation error: Invalid points strategy: ' . $format); |
| 170 | + } |
| 171 | + |
| 172 | + // validate test groups |
| 173 | + foreach ($this->getGroups() as $groupIndex => $group) { |
| 174 | + if (!isset($group['match'])) { |
| 175 | + throw new Error('config.json validation error: Group #' . $groupIndex . |
| 176 | + ' does not contain a "match" property'); |
| 177 | + } |
| 178 | + |
| 179 | + if (isset($group['strategy']) && $group['strategy'] !== 'add' && $group['strategy'] !== 'deduct') { |
| 180 | + throw new Error('config.json validation error: Invalid points strategy: ' . $format); |
| 181 | + } |
| 182 | + |
| 183 | + foreach (($group['tests'] ?? []) as $testIndex => $test) { |
| 184 | + if (!isset($test['match'])) { |
| 185 | + throw new Error('config.json validation error: Test #' . $testIndex . |
| 186 | + ' in group #' . $groupIndex . ' (' . $group['match'] . ') does not contain a "match" property'); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if ( |
| 191 | + isset($group['maxPoints']) && ( |
| 192 | + (isset($group['strategy']) && $group['strategy'] !== 'deduct') || |
| 193 | + (!isset($group['strategy']) && $this->getPointsStrategy() !== 'deduct') |
| 194 | + ) |
| 195 | + ) { |
| 196 | + throw new Error('config.json validation error: Property "maxPoints" can only be set for strategy ' . |
| 197 | + '"deduct". Found in group #' . $groupIndex . ' (' . $group['match'] . ')'); |
| 198 | + } |
| 199 | + } |
36 | 200 | }
|
37 | 201 | }
|
0 commit comments