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