Skip to content

Commit 92ba9d4

Browse files
committed
tests: add phpunit test
1 parent 65241d7 commit 92ba9d4

File tree

1 file changed

+102
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)