forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelGeneratorTest.php
More file actions
154 lines (125 loc) · 5.01 KB
/
ModelGeneratorTest.php
File metadata and controls
154 lines (125 loc) · 5.01 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
152
153
154
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Generators;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class ModelGeneratorTest extends CIUnitTestCase
{
use StreamFilterTrait;
protected function tearDown(): void
{
parent::tearDown();
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
if (is_file($file)) {
unlink($file);
}
}
private function getFileContent(string $filepath): string
{
if (! is_file($filepath)) {
return '';
}
return file_get_contents($filepath) ?: '';
}
public function testGenerateModel(): void
{
command('make:model user --table users');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$file = APPPATH . 'Models/User.php';
$this->assertFileExists($file);
$this->assertStringContainsString('extends Model', $this->getFileContent($file));
$this->assertStringContainsString('protected $table = \'users\';', $this->getFileContent($file));
$this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file));
}
public function testGenerateModelWithOptionTable(): void
{
command('make:model cars -table utilisateur');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$file = APPPATH . 'Models/Cars.php';
$this->assertFileExists($file);
$this->assertStringContainsString('protected $table = \'utilisateur\';', $this->getFileContent($file));
}
public function testGenerateModelWithOptionDBGroup(): void
{
command('make:model user -dbgroup testing');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$file = APPPATH . 'Models/User.php';
$this->assertFileExists($file);
$this->assertStringContainsString('protected $DBGroup = \'testing\';', $this->getFileContent($file));
}
public function testGenerateModelWithOptionReturnArray(): void
{
command('make:model user --return array');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$file = APPPATH . 'Models/User.php';
$this->assertFileExists($file);
$this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file));
}
public function testGenerateModelWithOptionReturnObject(): void
{
command('make:model user --return object');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$file = APPPATH . 'Models/User.php';
$this->assertFileExists($file);
$this->assertStringContainsString('protected $returnType = \'object\';', $this->getFileContent($file));
}
public function testGenerateModelWithOptionReturnEntity(): void
{
command('make:model user --return entity');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$file = APPPATH . 'Models/User.php';
$this->assertFileExists($file);
$this->assertStringContainsString('protected $returnType = \App\Entities\User::class;', $this->getFileContent($file));
if (is_file($file)) {
unlink($file);
}
$file = APPPATH . 'Entities/User.php';
$this->assertFileExists($file);
$dir = dirname($file);
if (is_file($file)) {
unlink($file);
}
if (is_dir($dir)) {
rmdir($dir);
}
}
public function testGenerateModelWithOptionSuffix(): void
{
command('make:model user --suffix --return entity');
$model = APPPATH . 'Models/UserModel.php';
$entity = APPPATH . 'Entities/UserEntity.php';
$this->assertFileExists($model);
$this->assertFileExists($entity);
unlink($model);
unlink($entity);
rmdir(dirname($entity));
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5050
*/
public function testGenerateModelWithSuffixAndMixedPascalCasedName(): void
{
command('make:model MyTable --suffix --return entity');
$model = APPPATH . 'Models/MyTableModel.php';
$entity = APPPATH . 'Entities/MyTableEntity.php';
$this->assertFileExists($model);
$this->assertFileExists($entity);
unlink($model);
unlink($entity);
rmdir(dirname($entity));
}
}