Skip to content

Commit fbadd42

Browse files
committed
Use UserModel as test class name
1 parent 4afdab0 commit fbadd42

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

docs/concepts.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ A default model is provided for you by the `CodeIgniter\Shield\Models\UserModel`
2929
this in the `Config\Auth::$userProvider` setting. The only requirement is that your new class
3030
MUST extend the provided `UserModel`.
3131

32-
Shield has a CLI command to quickly create a custom `MyUserModel` class by running the following
32+
Shield has a CLI command to quickly create a custom `UserModel` class by running the following
3333
command in the terminal:
3434

3535
```console
36-
php spark shield:model MyUserModel
36+
php spark shield:model UserModel
3737
```
3838

3939
You should set `Config\Auth::$userProvider` as follows:
4040

4141
```php
42-
public string $userProvider = \App\Models\MyUserModel::class;
42+
public string $userProvider = \App\Models\UserModel::class;
4343
```
4444

4545
## User Identities

src/Commands/Generators/Views/usermodel.tpl.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
namespace {namespace};
66

7-
use CodeIgniter\Shield\Models\UserModel;
7+
use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;
88

9-
class {class} extends UserModel
9+
class {class} extends ShieldUserModel
1010
{
1111
protected function initialize(): void
1212
{
1313
$this->allowedFields = [
1414
...$this->allowedFields,
15-
// Add here your custom fields
15+
1616
// 'first_name',
1717
];
1818
}
1919
}
20-

tests/Commands/UserModelGeneratorTest.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ protected function setUp(): void
2323
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
2424
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
2525

26+
if (is_file(HOMEPATH . 'src/Models/UserModel.php')) {
27+
copy(HOMEPATH . 'src/Models/UserModel.php', HOMEPATH . 'src/Models/UserModel.php.bak');
28+
}
29+
2630
$this->deleteTestFiles();
2731
}
2832

@@ -32,13 +36,18 @@ protected function tearDown(): void
3236

3337
stream_filter_remove($this->streamFilter);
3438
$this->deleteTestFiles();
39+
40+
if (is_file(HOMEPATH . 'src/Models/UserModel.php.bak')) {
41+
copy(HOMEPATH . 'src/Models/UserModel.php.bak', HOMEPATH . 'src/Models/UserModel.php');
42+
unlink(HOMEPATH . 'src/Models/UserModel.php.bak');
43+
}
3544
}
3645

3746
private function deleteTestFiles(): void
3847
{
3948
$possibleFiles = [
40-
APPPATH . 'Models/MyUserModel.php',
41-
HOMEPATH . 'src/Models/MyUserModel.php',
49+
APPPATH . 'Models/UserModel.php',
50+
HOMEPATH . 'src/Models/UserModel.php',
4251
];
4352

4453
foreach ($possibleFiles as $file) {
@@ -57,51 +66,51 @@ private function getFileContents(string $filepath): string
5766

5867
public function testGenerateUserModel(): void
5968
{
60-
command('shield:model MyUserModel');
69+
command('shield:model UserModel');
6170

62-
$filepath = APPPATH . 'Models/MyUserModel.php';
71+
$filepath = APPPATH . 'Models/UserModel.php';
6372
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
6473
$this->assertFileExists($filepath);
6574

6675
$contents = $this->getFileContents($filepath);
6776
$this->assertStringContainsString('namespace App\Models;', $contents);
68-
$this->assertStringContainsString('class MyUserModel extends UserModel', $contents);
69-
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $contents);
77+
$this->assertStringContainsString('class UserModel extends ShieldUserModel', $contents);
78+
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;', $contents);
7079
$this->assertStringContainsString('protected function initialize(): void', $contents);
7180
}
7281

7382
public function testGenerateUserModelCustomNamespace(): void
7483
{
75-
command('shield:model MyUserModel --namespace CodeIgniter\\\\Shield');
84+
command('shield:model UserModel --namespace CodeIgniter\\\\Shield');
7685

77-
$filepath = HOMEPATH . 'src/Models/MyUserModel.php';
86+
$filepath = HOMEPATH . 'src/Models/UserModel.php';
7887
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
7988
$this->assertFileExists($filepath);
8089

8190
$contents = $this->getFileContents($filepath);
8291
$this->assertStringContainsString('namespace CodeIgniter\Shield\Models;', $contents);
83-
$this->assertStringContainsString('class MyUserModel extends UserModel', $contents);
84-
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $contents);
92+
$this->assertStringContainsString('class UserModel extends ShieldUserModel', $contents);
93+
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;', $contents);
8594
$this->assertStringContainsString('protected function initialize(): void', $contents);
8695
}
8796

8897
public function testGenerateUserModelWithForce(): void
8998
{
90-
command('shield:model MyUserModel');
91-
command('shield:model MyUserModel --force');
99+
command('shield:model UserModel');
100+
command('shield:model UserModel --force');
92101

93102
$this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer);
94-
$this->assertFileExists(APPPATH . 'Models/MyUserModel.php');
103+
$this->assertFileExists(APPPATH . 'Models/UserModel.php');
95104
}
96105

97106
public function testGenerateUserModelWithSuffix(): void
98107
{
99-
command('shield:model MyUser --suffix');
108+
command('shield:model User --suffix');
100109

101110
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
102111

103-
$filepath = APPPATH . 'Models/MyUserModel.php';
112+
$filepath = APPPATH . 'Models/UserModel.php';
104113
$this->assertFileExists($filepath);
105-
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
114+
$this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath));
106115
}
107116
}

0 commit comments

Comments
 (0)