forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandGeneratorTest.php
More file actions
149 lines (127 loc) · 5.28 KB
/
CommandGeneratorTest.php
File metadata and controls
149 lines (127 loc) · 5.28 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
<?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 CommandGeneratorTest extends CIUnitTestCase
{
use StreamFilterTrait;
protected function tearDown(): void
{
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
$dir = dirname($file);
if (is_file($file)) {
unlink($file);
}
if (is_dir($dir) && str_contains($dir, 'Commands')) {
rmdir($dir);
}
}
protected function getFileContents(string $filepath): string
{
if (! is_file($filepath)) {
return '';
}
return file_get_contents($filepath) ?: '';
}
public function testGenerateCommand(): void
{
command('make:command deliver');
$file = APPPATH . 'Commands/Deliver.php';
$this->assertFileExists($file);
$contents = $this->getFileContents($file);
$this->assertStringContainsString('protected $group = \'App\';', $contents);
$this->assertStringContainsString('protected $name = \'command:name\';', $contents);
}
public function testGenerateCommandWithOptionCommand(): void
{
command('make:command deliver -command clear:sessions');
$file = APPPATH . 'Commands/Deliver.php';
$this->assertFileExists($file);
$contents = $this->getFileContents($file);
$this->assertStringContainsString('protected $name = \'clear:sessions\';', $contents);
$this->assertStringContainsString('protected $usage = \'clear:sessions [arguments] [options]\';', $contents);
}
public function testGenerateCommandWithOptionTypeBasic(): void
{
command('make:command deliver -type basic');
$file = APPPATH . 'Commands/Deliver.php';
$this->assertFileExists($file);
$contents = $this->getFileContents($file);
$this->assertStringContainsString('protected $group = \'App\';', $contents);
$this->assertStringContainsString('protected $name = \'command:name\';', $contents);
}
public function testGenerateCommandWithOptionTypeGenerator(): void
{
command('make:command deliver -type generator');
$file = APPPATH . 'Commands/Deliver.php';
$this->assertFileExists($file);
$contents = $this->getFileContents($file);
$this->assertStringContainsString('protected $group = \'Generators\';', $contents);
$this->assertStringContainsString('protected $name = \'command:name\';', $contents);
}
public function testGenerateCommandWithOptionGroup(): void
{
command('make:command deliver -group Deliverables');
$file = APPPATH . 'Commands/Deliver.php';
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$this->assertFileExists($file);
$contents = $this->getFileContents($file);
$this->assertStringContainsString('protected $group = \'Deliverables\';', $contents);
}
public function testGenerateCommandWithOptionSuffix(): void
{
command('make:command publish -suffix');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$file = APPPATH . 'Commands/PublishCommand.php';
$this->assertFileExists($file);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4495
*/
public function testGeneratorPreservesCase(): void
{
command('make:model TestModule');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$this->assertFileExists(APPPATH . 'Models/TestModule.php');
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4495
*/
public function testGeneratorPreservesCaseButChangesComponentName(): void
{
command('make:controller TestModulecontroller');
$this->assertStringContainsString('File created: ', $this->getStreamFilterBuffer());
$this->assertFileExists(APPPATH . 'Controllers/TestModuleController.php');
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4857
*/
public function testGeneratorIsNotConfusedWithNamespaceLikeClassNames(): void
{
$time = time();
$notExists = true;
command('make:migration App_Lesson');
// we got 5 chances to prove that the file created went to app/Database/Migrations
foreach (range(0, 4) as $increment) {
$expectedFile = sprintf('%sDatabase/Migrations/%s_AppLesson.php', APPPATH, gmdate('Y-m-d-His', $time + $increment));
clearstatcache(true, $expectedFile);
$notExists = $notExists && ! is_file($expectedFile);
}
$this->assertFalse($notExists, 'Creating migration file for class "AppLesson" did not go to "app/Database/Migrations"');
}
}