forked from codeigniter4/shield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupTest.php
More file actions
197 lines (161 loc) · 5.89 KB
/
SetupTest.php
File metadata and controls
197 lines (161 loc) · 5.89 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Tests\Commands;
use CodeIgniter\Shield\Commands\Setup;
use CodeIgniter\Shield\Test\MockInputOutput;
use Config\Email as EmailConfig;
use Config\Services;
use org\bovigo\vfs\vfsStream;
use Tests\Support\TestCase;
/**
* @internal
*/
final class SetupTest extends TestCase
{
private ?MockInputOutput $io = null;
protected function tearDown(): void
{
parent::tearDown();
Setup::resetInputOutput();
}
/**
* Set MockInputOutput and user inputs.
*
* @param array<int, string> $inputs User inputs
* @phpstan-param list<string> $inputs
*/
private function setMockIo(array $inputs): void
{
$this->io = new MockInputOutput();
$this->io->setInputs($inputs);
Setup::setInputOutput($this->io);
}
public function testRun(): void
{
// Set MockIO and your inputs.
$this->setMockIo([
'y',
'admin@example.com',
'y',
'Site Administrator',
'y',
]);
$appFolder = $this->createFilesystem();
$command = new Setup(Services::logger(), Services::commands());
$this->setPrivateProperty($command, 'distPath', $appFolder);
$command->run([]);
$auth = file_get_contents($appFolder . 'Config/Auth.php');
$this->assertIsString($auth);
$this->assertStringContainsString('namespace Config;', $auth);
$this->assertStringContainsString('use CodeIgniter\Shield\Config\Auth as ShieldAuth;', $auth);
$authToken = file_get_contents($appFolder . 'Config/AuthToken.php');
$this->assertIsString($authToken);
$this->assertStringContainsString('namespace Config;', $authToken);
$this->assertStringContainsString('use CodeIgniter\Shield\Config\AuthToken as ShieldAuthToken;', $authToken);
$autoload = file_get_contents($appFolder . 'Config/Autoload.php');
$this->assertIsString($autoload);
$this->assertStringContainsString('$helpers = [\'auth\', \'setting\'];', $autoload);
$routes = file_get_contents($appFolder . 'Config/Routes.php');
$this->assertIsString($routes);
$this->assertStringContainsString('service(\'auth\')->routes($routes);', $routes);
$security = file_get_contents($appFolder . 'Config/Security.php');
$this->assertIsString($security);
$this->assertStringContainsString('$csrfProtection = \'session\';', $security);
$result = $this->getOutputWithoutColorCode();
$this->assertStringContainsString(
' Created: vfs://root/Config/Auth.php
Created: vfs://root/Config/AuthGroups.php
Created: vfs://root/Config/AuthToken.php
Updated: vfs://root/Config/Autoload.php
Updated: vfs://root/Config/Routes.php
Updated: We have updated file \'vfs://root/Config/Security.php\' for security reasons.
Updated: vfs://root/Config/Email.php',
$result,
);
$this->assertStringContainsString(
'Running all new migrations...',
$result,
);
}
public function testRunEmailConfigIsFine(): void
{
// Set MockIO and your inputs.
$this->setMockIo(['y']);
$config = config(EmailConfig::class);
$config->fromEmail = 'admin@example.com';
$config->fromName = 'Site Admin';
$appFolder = $this->createFilesystem();
$command = new Setup(Services::logger(), Services::commands());
$this->setPrivateProperty($command, 'distPath', $appFolder);
$command->run([]);
$result = $this->getOutputWithoutColorCode();
$this->assertStringContainsString(
' Created: vfs://root/Config/Auth.php
Created: vfs://root/Config/AuthGroups.php
Created: vfs://root/Config/AuthToken.php
Updated: vfs://root/Config/Autoload.php
Updated: vfs://root/Config/Routes.php
Updated: We have updated file \'vfs://root/Config/Security.php\' for security reasons.',
$result,
);
}
public function testUpdateAutoloadHelpers(): void
{
$command = new Setup(Services::logger(), Services::commands());
$updateAutoloadHelpers = self::getPrivateMethodInvoker($command, 'updateAutoloadHelpers');
$content = <<<'EOL'
class Autoload extends AutoloadConfig
{
/**
* -------------------------------------------------------------------
* Helpers
* -------------------------------------------------------------------
* Prototype:
* $helpers = [
* 'form',
* ];
*
* @var list<string>
*/
public $helpers = [
'text',
'form',
];
}
EOL;
$helpers = ['text', 'form', 'auth', 'setting'];
$output = $updateAutoloadHelpers($content, $helpers);
$this->assertStringContainsString(
"public \$helpers = ['text', 'form', 'auth', 'setting'];",
$output,
);
}
/**
* @return string app folder path
*/
private function createFilesystem(): string
{
$root = vfsStream::setup('root');
vfsStream::copyFromFileSystem(
APPPATH,
$root,
);
return $root->url() . '/';
}
private function getOutputWithoutColorCode(): string
{
$output = str_replace(["\033[0;32m", "\033[0m"], '', $this->io->getOutputs());
if (is_windows()) {
$output = str_replace("\r\n", "\n", $output);
}
return $output;
}
}