Skip to content

Commit 056e26a

Browse files
committed
tests: add phpunit test
1 parent 65241d7 commit 056e26a

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CodeIgniter\Test\CIUnitTestCase;
6+
use CodeIgniter\Test\Filters\CITestStreamFilter;
7+
8+
/**
9+
* @internal
10+
*/
11+
final class UserModelGeneratorTest extends CIUnitTestCase
12+
{
13+
protected $streamFilter;
14+
15+
protected function setUp(): void
16+
{
17+
CITestStreamFilter::$buffer = '';
18+
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
19+
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
20+
parent::setUp();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
stream_filter_remove($this->streamFilter);
26+
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', CITestStreamFilter::$buffer);
27+
28+
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
29+
if (is_file($file)) {
30+
unlink($file);
31+
}
32+
}
33+
34+
protected function getFileContents(string $filepath): string
35+
{
36+
if (! file_exists($filepath)) {
37+
return '';
38+
}
39+
40+
return file_get_contents($filepath) ?: '';
41+
}
42+
43+
public function testGenerateUserModel(): void
44+
{
45+
command('shield:model MyUserModel');
46+
$filepath = APPPATH . 'Models\MyUserModel.php';
47+
48+
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
49+
$this->assertFileExists($filepath);
50+
51+
$this->assertStringContainsString('namespace App\Models;', $this->getFileContents($filepath));
52+
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
53+
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $this->getFileContents($filepath));
54+
$this->assertStringContainsString('protected function initialize(): void', $this->getFileContents($filepath));
55+
}
56+
57+
public function testGenerateUserModelCustomNamespace(): void
58+
{
59+
command('shield:model MyUserModel --namespace CodeIgniter\\\\Shield');
60+
61+
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
62+
$this->assertFileExists($filepath);
63+
64+
$this->assertStringContainsString('namespace CodeIgniter\Shield\Models;', $this->getFileContents($filepath));
65+
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
66+
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $this->getFileContents($filepath));
67+
$this->assertStringContainsString('protected function initialize(): void', $this->getFileContents($filepath));
68+
69+
$filepath = VENDORPATH . 'codeigniter4/shield/src/Models/MyUserModel.php';
70+
if (is_file($filepath)) {
71+
unlink($filepath);
72+
}
73+
}
74+
75+
public function testGenerateUserModelWithForce(): void
76+
{
77+
command('shield:model MyUserModel');
78+
79+
command('shield:model MyUserModel --force');
80+
$this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer);
81+
82+
$file = APPPATH . 'Models/MyUserModel.php';
83+
if (is_file($file)) {
84+
unlink($file);
85+
}
86+
}
87+
88+
public function testGenerateUserModelWithSuffix(): void
89+
{
90+
command('shield:model MyUser --suffix');
91+
$filepath = APPPATH . 'Models\MyUserModel.php';
92+
93+
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
94+
$this->assertFileExists($filepath);
95+
96+
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
97+
}
98+
}

0 commit comments

Comments
 (0)