Skip to content

Commit f4d702d

Browse files
committed
Wrote unit tests and added more validation for config entries & more output messages.
1 parent ef20169 commit f4d702d

26 files changed

+1294
-50
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ index.php
4040

4141
/tests/DbFiles/blog.sqlite
4242
*.sqlite
43+
44+
/tests/test-files/file-with-two-lines.txt

bin/generate-leanorm-classes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252
echo "************************************************" . PHP_EOL;
5353
echo "Successfully loaded LeanOrm Cli .............." . PHP_EOL;
5454
echo "************************************************" . PHP_EOL . PHP_EOL;
55+
56+
if($tableOrViewNameIfAny === '') {
57+
58+
echo "Classes will be generated for all tables and views (apart from tables & views specified to be skipped in the config file)." . PHP_EOL . PHP_EOL;
59+
60+
} else {
61+
62+
echo "Classes will be generated for only `{$tableOrViewNameIfAny}` (as long as it's not specified to be skipped in the config file)." . PHP_EOL . PHP_EOL;
63+
}
64+
5565
$code = $command($tableOrViewNameIfAny);
5666
exit($code);
5767

src/FileIoUtils.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22
declare(strict_types=1);
3-
43
namespace LeanOrmCli;
54

65
/**
7-
*
86
* This code was lifted from https://github.com/atlasphp/Atlas.Cli/blob/2.x/src/Fsio.php
97
*/
108
class FileIoUtils {

src/OrmClassesGenerator.php

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,6 @@ class OrmClassesGenerator {
3333
*/
3434
public const TEMPLATE_RECORD_FILE_NAME = 'TypeRecord.php.tpl';
3535

36-
/**
37-
* @var string
38-
*/
39-
public const DEFAULT_COLLECTION_CLASS_TO_EXTEND = '\\LeanOrm\\Model\\Collection';
40-
41-
/**
42-
* @var string
43-
*/
44-
public const DEFAULT_MODEL_CLASS_TO_EXTEND = '\\LeanOrm\\Model';
45-
46-
/**
47-
* @var string
48-
*/
49-
public const DEFAULT_RECORD_CLASS_TO_EXTEND = '\\LeanOrm\\Model\\Record';
50-
5136
protected array $defaultConfig = [];
5237

5338
protected string $defaultTemplatesDirectory = '';
@@ -85,12 +70,14 @@ public function __construct(array $config) {
8570

8671
throw new \Exception('`pdo` entry is missing in config!');
8772

88-
} elseif (!is_array($this->config['pdo'])) {
73+
} elseif (!is_array($this->config['pdo']) && !($this->config['pdo'] instanceof \PDO)) {
8974

90-
throw new \Exception('`pdo` entry in config is not an array!');
75+
throw new \Exception('`pdo` entry in config is not an array & is also not a PDO instance!');
9176
}
9277

93-
$this->pdo = new \PDO(...$this->config['pdo']);
78+
$this->pdo = ($this->config['pdo'] instanceof \PDO)
79+
? $this->config['pdo']
80+
: new \PDO(...$this->config['pdo']);
9481

9582
// fill config with defaults
9683
foreach ($this->defaultConfig as $key=>$val) {
@@ -100,68 +87,83 @@ public function __construct(array $config) {
10087
$this->config[$key] = $val;
10188
}
10289
}
90+
91+
////////////////////////////////////////////////////////////////////////
92+
// VALIDATE THE REMAINING ENTRIES IN THE CONFIG
93+
////////////////////////////////////////////////////////////////////////
94+
95+
if(!is_string($this->config['namespace']) && $this->config['namespace']!== null) {
96+
97+
throw new \Exception('`namespace` entry in config is not a string!');
98+
}
10399

104100
if(!is_string($this->config['directory'])) {
105101

106102
throw new \Exception('`directory` entry in config is not a string!');
107103
}
104+
105+
if(!FileIoUtils::isDir($this->config['directory'])) {
108106

109-
$this->destinationDirectory = $this->config['directory'];
107+
throw new \Exception('`directory` entry in config is not a valid directory!');
108+
}
110109

111-
if(OtherUtils::isNonEmptyString($this->config['custom_templates_directory'])) {
110+
$this->destinationDirectory = $this->config['directory'];
112111

112+
if(
113+
OtherUtils::isNonEmptyString($this->config['custom_templates_directory'])
114+
) {
113115
if(!FileIoUtils::isDir($this->config['custom_templates_directory'])) {
114116

115117
throw new \Exception('`custom_templates_directory` entry in config is not a valid directory!');
116118
}
117119

118120
$this->customTemplatesDirectory = $this->config['custom_templates_directory'];
121+
122+
} elseif (
123+
!is_string($this->config['custom_templates_directory'])
124+
&& $this->config['custom_templates_directory'] !== null
125+
) {
126+
throw new \Exception('`custom_templates_directory` entry in config is not a string!');
119127
}
120-
128+
121129
if(!is_array($this->config['tables_to_skip'])) {
122130

123-
$this->config['tables_to_skip'] = [];
131+
throw new \Exception('`tables_to_skip` entry in config is not an array of strings (names of tables & views to skip)!');
124132
}
133+
134+
if(!is_string($this->config['collection_class_to_extend'])) {
125135

126-
if(!OtherUtils::isNonEmptyString($this->config['collection_class_to_extend'])) {
127-
128-
$this->config['collection_class_to_extend'] = static::DEFAULT_COLLECTION_CLASS_TO_EXTEND;
136+
throw new \Exception('`collection_class_to_extend` entry in config is not a string!');
129137
}
138+
139+
if(!is_string($this->config['model_class_to_extend'])) {
130140

131-
if(!OtherUtils::isNonEmptyString($this->config['model_class_to_extend'])) {
132-
133-
$this->config['model_class_to_extend'] = static::DEFAULT_MODEL_CLASS_TO_EXTEND;
141+
throw new \Exception('`model_class_to_extend` entry in config is not a string!');
134142
}
143+
144+
if(!is_string($this->config['record_class_to_extend'])) {
135145

136-
if(!OtherUtils::isNonEmptyString($this->config['record_class_to_extend'])) {
137-
138-
$this->config['record_class_to_extend'] = static::DEFAULT_RECORD_CLASS_TO_EXTEND;
146+
throw new \Exception('`record_class_to_extend` entry in config is not a string!');
139147
}
148+
149+
if(!is_string($this->config['created_timestamp_column_name']) && $this->config['created_timestamp_column_name']!== null) {
140150

141-
if(
142-
$this->config['created_timestamp_column_name'] !== null
143-
&& !OtherUtils::isNonEmptyString($this->config['created_timestamp_column_name'])
144-
) {
145-
$this->config['created_timestamp_column_name'] = null;
151+
throw new \Exception('`created_timestamp_column_name` entry in config is not a string!');
146152
}
153+
154+
if(!is_string($this->config['updated_timestamp_column_name']) && $this->config['updated_timestamp_column_name']!== null) {
147155

148-
if(
149-
$this->config['updated_timestamp_column_name'] !== null
150-
&& !OtherUtils::isNonEmptyString($this->config['updated_timestamp_column_name'])
151-
) {
152-
$this->config['updated_timestamp_column_name'] = null;
156+
throw new \Exception('`updated_timestamp_column_name` entry in config is not a string!');
153157
}
154-
158+
155159
if(!is_callable($this->config['table_name_to_record_class_prefix_transformer'])) {
156160

157-
$this->config['table_name_to_record_class_prefix_transformer'] =
158-
$this->defaultConfig['table_name_to_record_class_prefix_transformer'];
161+
throw new \Exception('`table_name_to_record_class_prefix_transformer` entry in config is not a callable!');
159162
}
160-
163+
161164
if(!is_callable($this->config['table_name_to_collection_and_model_class_prefix_transformer'])) {
162165

163-
$this->config['table_name_to_collection_and_model_class_prefix_transformer'] =
164-
$this->defaultConfig['table_name_to_collection_and_model_class_prefix_transformer'];
166+
throw new \Exception('`table_name_to_collection_and_model_class_prefix_transformer` entry in config is not a callable!');
165167
}
166168
}
167169

src/SchemaUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static function getCurrentConnectionInfo(\PDO $pdo): string {
116116

117117
} catch (\Exception $e) {
118118

119-
$result .= "`{$key}`: " . 'Unsupported attribute for the current PDO driver';
119+
$result .= "`{$key}`: " . 'Unsupported attribute for the current PDO driver'.PHP_EOL;
120120
continue;
121121
}
122122

0 commit comments

Comments
 (0)