forked from codeigniter4/shield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserModelGeneratorTest.php
More file actions
100 lines (77 loc) · 3.55 KB
/
UserModelGeneratorTest.php
File metadata and controls
100 lines (77 loc) · 3.55 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
<?php
declare(strict_types=1);
namespace Tests\Commands;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
/**
* @internal
*/
final class UserModelGeneratorTest extends CIUnitTestCase
{
protected $streamFilter;
protected function setUp(): void
{
parent::setUp();
CITestStreamFilter::$buffer = '';
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
}
protected function tearDown(): void
{
parent::tearDown();
stream_filter_remove($this->streamFilter);
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', CITestStreamFilter::$buffer);
$filepath = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
if (is_file($filepath)) {
unlink($filepath);
}
}
protected function getFileContents(string $filepath): string
{
if (! file_exists($filepath)) {
return '';
}
return file_get_contents($filepath) ?: '';
}
public function testGenerateUserModel(): void
{
command('shield:model MyUserModel');
$filepath = APPPATH . 'Models/MyUserModel.php';
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
$this->assertFileExists($filepath);
$this->assertStringContainsString('namespace App\Models;', $this->getFileContents($filepath));
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $this->getFileContents($filepath));
$this->assertStringContainsString('protected function initialize(): void', $this->getFileContents($filepath));
}
public function testGenerateUserModelCustomNamespace(): void
{
command('shield:model MyUserModel --namespace CodeIgniter\\\\Shield');
$filepath = HOMEPATH . 'src/Models/MyUserModel.php';
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
$this->assertFileExists($filepath);
$this->assertStringContainsString('namespace CodeIgniter\Shield\Models;', $this->getFileContents($filepath));
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $this->getFileContents($filepath));
$this->assertStringContainsString('protected function initialize(): void', $this->getFileContents($filepath));
if (is_file($filepath)) {
unlink($filepath);
}
}
public function testGenerateUserModelWithForce(): void
{
command('shield:model MyUserModel');
command('shield:model MyUserModel --force');
$this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer);
$filepath = APPPATH . 'Models/MyUserModel.php';
$this->assertFileExists($filepath);
}
public function testGenerateUserModelWithSuffix(): void
{
command('shield:model MyUser --suffix');
$filepath = APPPATH . 'Models/MyUserModel.php';
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
$this->assertFileExists($filepath);
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
}
}