|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\Generator\Writer; |
|
4
|
|
|
|
|
5
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsColumn; |
|
6
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsDatabase; |
|
7
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsTable; |
|
8
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\BaseEntityClassWriter; |
|
9
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\WriterConfig; |
|
10
|
|
|
use Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\Generator\Dms\DmsColumnTest; |
|
11
|
|
|
use Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\Generator\Dms\DmsDatabaseTest; |
|
12
|
|
|
use Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\Generator\Dms\DmsTableTest; |
|
13
|
|
|
use Janisbiz\LightOrm\Tests\Unit\Generator\Writer\FileTrait; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
class BaseEntityClassWriterTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
use FileTrait; |
|
19
|
|
|
|
|
20
|
|
|
const WRITER_CONFIG_DIRECTORY_VALUE = JANISBIZ_LIGHT_ORM_ROOT_DIR |
|
21
|
|
|
. 'var' |
|
22
|
|
|
. DIRECTORY_SEPARATOR |
|
23
|
|
|
. 'light-orm' |
|
24
|
|
|
. DIRECTORY_SEPARATOR |
|
25
|
|
|
. 'phpunit' |
|
26
|
|
|
; |
|
27
|
|
|
const WRITER_CONFIG_NAMESPACE_VALUE = 'None\Existent\Namespace'; |
|
28
|
|
|
const WRITER_CONFIG_CLASS_PREFIX_VALUE = 'Base'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var WriterConfig |
|
32
|
|
|
*/ |
|
33
|
|
|
private $writerConfig; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var BaseEntityClassWriter |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $baseEntityClassWriter; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var DmsDatabase |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $dmsDatabase; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var DmsTable |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $dmsTable; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $dmsGeneratedDirectory; |
|
54
|
|
|
|
|
55
|
|
|
public function setUp() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->writerConfig = new WriterConfig( |
|
58
|
|
|
static::WRITER_CONFIG_DIRECTORY_VALUE, |
|
59
|
|
|
static::WRITER_CONFIG_NAMESPACE_VALUE, |
|
60
|
|
|
static::WRITER_CONFIG_CLASS_PREFIX_VALUE |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->baseEntityClassWriter = new BaseEntityClassWriter($this->writerConfig); |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$dmsColumns = []; |
|
67
|
|
|
for ($i = 1; $i <= 3; $i++) { |
|
68
|
|
|
$dmsColumns[] = new DmsColumn( |
|
69
|
|
|
\sprintf('%s_%d', DmsColumnTest::COLUMN_NAME, $i), |
|
70
|
|
|
DmsColumnTest::COLUMN_TYPE, |
|
71
|
|
|
DmsColumnTest::COLUMN_NULLABLE, |
|
72
|
|
|
2 >= $i ? DmsColumnTest::COLUMN_KEY : '', |
|
73
|
|
|
DmsColumnTest::COLUMN_DEFAULT, |
|
74
|
|
|
1 === $i ? DmsColumnTest::COLUMN_EXTRA : null |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->dmsTable = new DmsTable( |
|
79
|
|
|
\sprintf('%s_%d', DmsTableTest::TABLE_NAME, $i), |
|
80
|
|
|
$dmsColumns |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$this->dmsDatabase = new DmsDatabase(DmsDatabaseTest::DATABASE_NAME, [$this->dmsTable]); |
|
84
|
|
|
|
|
85
|
|
|
$this->dmsGeneratedDirectory = \implode( |
|
86
|
|
|
'', |
|
87
|
|
|
[ |
|
88
|
|
|
$this->writerConfig->getDirectory(), |
|
89
|
|
|
DIRECTORY_SEPARATOR, |
|
90
|
|
|
$this->dmsDatabase->getPhpName(), |
|
91
|
|
|
DIRECTORY_SEPARATOR, |
|
92
|
|
|
$this->writerConfig->getClassPrefix(), |
|
93
|
|
|
] |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testConstruct() |
|
98
|
|
|
{ |
|
99
|
|
|
$getWriterConfigMethod = new \ReflectionMethod($this->baseEntityClassWriter, 'getWriterConfig'); |
|
100
|
|
|
$getWriterConfigMethod->setAccessible(true); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertTrue($getWriterConfigMethod->invoke($this->baseEntityClassWriter) instanceof $this->writerConfig); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $files |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testWrite(array &$files = []) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->baseEntityClassWriter->write($this->dmsDatabase, $this->dmsTable, $files); |
|
111
|
|
|
|
|
112
|
|
|
$baseEntityFilePath = \implode( |
|
113
|
|
|
'', |
|
114
|
|
|
[ |
|
115
|
|
|
$this->dmsGeneratedDirectory, |
|
116
|
|
|
DIRECTORY_SEPARATOR, |
|
117
|
|
|
\sprintf('%s%s.php', $this->writerConfig->getClassPrefix(), $this->dmsTable->getPhpName()) |
|
118
|
|
|
] |
|
119
|
|
|
); |
|
120
|
|
|
$this->assertFileExists($baseEntityFilePath); |
|
121
|
|
|
$this->assertEquals( |
|
122
|
|
|
/** @lang PHP */ |
|
123
|
|
|
<<<PHP |
|
124
|
|
|
<?php declare(strict_types=1); |
|
125
|
|
|
|
|
126
|
|
|
namespace None\Existent\Namespace\DatabaseNameSnakeCase\Base; |
|
127
|
|
|
|
|
128
|
|
|
use Janisbiz\LightOrm\Entity\BaseEntity; |
|
129
|
|
|
|
|
130
|
|
|
class BaseTableNameSnakeCase4 extends BaseEntity |
|
131
|
|
|
{ |
|
132
|
|
|
const DATABASE_NAME = 'database_name_snake_case'; |
|
133
|
|
|
const TABLE_NAME = 'database_name_snake_case.table_name_snake_case_4'; |
|
134
|
|
|
|
|
135
|
|
|
const COLUMN_NAME_SNAKE_CASE_1 = 'name_snake_case_1'; |
|
136
|
|
|
const COLUMN_NAME_SNAKE_CASE_2 = 'name_snake_case_2'; |
|
137
|
|
|
const COLUMN_NAME_SNAKE_CASE_3 = 'name_snake_case_3'; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param bool \$isNew |
|
141
|
|
|
*/ |
|
142
|
|
|
public function __construct(\$isNew = true) |
|
143
|
|
|
{ |
|
144
|
|
|
\$this->primaryKeys = [ |
|
145
|
|
|
static::COLUMN_NAME_SNAKE_CASE_1, |
|
146
|
|
|
static::COLUMN_NAME_SNAKE_CASE_2, |
|
147
|
|
|
]; |
|
148
|
|
|
\$this->primaryKeysAutoIncrement = [ |
|
149
|
|
|
static::COLUMN_NAME_SNAKE_CASE_1, |
|
150
|
|
|
]; |
|
151
|
|
|
\$this->columns = [ |
|
152
|
|
|
static::COLUMN_NAME_SNAKE_CASE_1, |
|
153
|
|
|
static::COLUMN_NAME_SNAKE_CASE_2, |
|
154
|
|
|
static::COLUMN_NAME_SNAKE_CASE_3, |
|
155
|
|
|
]; |
|
156
|
|
|
|
|
157
|
|
|
\$this->isNew = \$isNew; |
|
158
|
|
|
if (empty(\$this->data)) { |
|
159
|
|
|
\$this->isNew = true; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return null|string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getNameSnakeCase1(): ?string |
|
167
|
|
|
{ |
|
168
|
|
|
return \$this->data['name_snake_case_1']; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param null|string \$nameSnakeCase1 |
|
173
|
|
|
* |
|
174
|
|
|
* @return \$this |
|
175
|
|
|
*/ |
|
176
|
|
|
public function setNameSnakeCase1(?string \$nameSnakeCase1): BaseTableNameSnakeCase4 |
|
177
|
|
|
{ |
|
178
|
|
|
\$this->data['name_snake_case_1'] = \$nameSnakeCase1; |
|
179
|
|
|
|
|
180
|
|
|
return \$this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return null|string |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getNameSnakeCase2(): ?string |
|
188
|
|
|
{ |
|
189
|
|
|
return \$this->data['name_snake_case_2']; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param null|string \$nameSnakeCase2 |
|
194
|
|
|
* |
|
195
|
|
|
* @return \$this |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setNameSnakeCase2(?string \$nameSnakeCase2): BaseTableNameSnakeCase4 |
|
198
|
|
|
{ |
|
199
|
|
|
\$this->data['name_snake_case_2'] = \$nameSnakeCase2; |
|
200
|
|
|
|
|
201
|
|
|
return \$this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return null|string |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getNameSnakeCase3(): ?string |
|
209
|
|
|
{ |
|
210
|
|
|
return \$this->data['name_snake_case_3']; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param null|string \$nameSnakeCase3 |
|
215
|
|
|
* |
|
216
|
|
|
* @return \$this |
|
217
|
|
|
*/ |
|
218
|
|
|
public function setNameSnakeCase3(?string \$nameSnakeCase3): BaseTableNameSnakeCase4 |
|
219
|
|
|
{ |
|
220
|
|
|
\$this->data['name_snake_case_3'] = \$nameSnakeCase3; |
|
221
|
|
|
|
|
222
|
|
|
return \$this; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
PHP |
|
227
|
|
|
, |
|
228
|
|
|
\file_get_contents($baseEntityFilePath) |
|
229
|
|
|
); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function testWriteWhenFileExists() |
|
233
|
|
|
{ |
|
234
|
|
|
$files = $this->createTestFiles( |
|
235
|
|
|
[ |
|
236
|
|
|
\sprintf('%s%s.php', $this->writerConfig->getClassPrefix(), $this->dmsTable->getPhpName()) |
|
237
|
|
|
], |
|
238
|
|
|
$this->dmsGeneratedDirectory |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
$this->testWrite($files); |
|
242
|
|
|
$this->assertCount(0, $files); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function tearDown() |
|
246
|
|
|
{ |
|
247
|
|
|
$this->removeDirectoryRecursive(\implode( |
|
248
|
|
|
'', |
|
249
|
|
|
[ |
|
250
|
|
|
JANISBIZ_LIGHT_ORM_ROOT_DIR, |
|
251
|
|
|
'var', |
|
252
|
|
|
DIRECTORY_SEPARATOR, |
|
253
|
|
|
'light-orm', |
|
254
|
|
|
] |
|
255
|
|
|
)); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|