|
| 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 | + private $streamFilter; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + CITestStreamFilter::$buffer = ''; |
| 22 | + |
| 23 | + $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); |
| 24 | + $this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter'); |
| 25 | + |
| 26 | + if (is_file(HOMEPATH . 'src/Models/UserModel.php')) { |
| 27 | + copy(HOMEPATH . 'src/Models/UserModel.php', HOMEPATH . 'src/Models/UserModel.php.bak'); |
| 28 | + } |
| 29 | + |
| 30 | + $this->deleteTestFiles(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function tearDown(): void |
| 34 | + { |
| 35 | + parent::tearDown(); |
| 36 | + |
| 37 | + stream_filter_remove($this->streamFilter); |
| 38 | + $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 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private function deleteTestFiles(): void |
| 47 | + { |
| 48 | + $possibleFiles = [ |
| 49 | + APPPATH . 'Models/UserModel.php', |
| 50 | + HOMEPATH . 'src/Models/UserModel.php', |
| 51 | + ]; |
| 52 | + |
| 53 | + foreach ($possibleFiles as $file) { |
| 54 | + clearstatcache(true, $file); |
| 55 | + |
| 56 | + if (is_file($file)) { |
| 57 | + unlink($file); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private function getFileContents(string $filepath): string |
| 63 | + { |
| 64 | + return (string) @file_get_contents($filepath); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGenerateUserModel(): void |
| 68 | + { |
| 69 | + command('shield:model UserModel'); |
| 70 | + |
| 71 | + $filepath = APPPATH . 'Models/UserModel.php'; |
| 72 | + $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); |
| 73 | + $this->assertFileExists($filepath); |
| 74 | + |
| 75 | + $contents = $this->getFileContents($filepath); |
| 76 | + $this->assertStringContainsString('namespace App\Models;', $contents); |
| 77 | + $this->assertStringContainsString('class UserModel extends ShieldUserModel', $contents); |
| 78 | + $this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;', $contents); |
| 79 | + $this->assertStringContainsString('protected function initialize(): void', $contents); |
| 80 | + } |
| 81 | + |
| 82 | + public function testGenerateUserModelCustomNamespace(): void |
| 83 | + { |
| 84 | + command('shield:model UserModel --namespace CodeIgniter\\\\Shield'); |
| 85 | + |
| 86 | + $filepath = HOMEPATH . 'src/Models/UserModel.php'; |
| 87 | + $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); |
| 88 | + $this->assertFileExists($filepath); |
| 89 | + |
| 90 | + $contents = $this->getFileContents($filepath); |
| 91 | + $this->assertStringContainsString('namespace CodeIgniter\Shield\Models;', $contents); |
| 92 | + $this->assertStringContainsString('class UserModel extends ShieldUserModel', $contents); |
| 93 | + $this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;', $contents); |
| 94 | + $this->assertStringContainsString('protected function initialize(): void', $contents); |
| 95 | + } |
| 96 | + |
| 97 | + public function testGenerateUserModelWithForce(): void |
| 98 | + { |
| 99 | + command('shield:model UserModel'); |
| 100 | + command('shield:model UserModel --force'); |
| 101 | + |
| 102 | + $this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer); |
| 103 | + $this->assertFileExists(APPPATH . 'Models/UserModel.php'); |
| 104 | + } |
| 105 | + |
| 106 | + public function testGenerateUserModelWithSuffix(): void |
| 107 | + { |
| 108 | + command('shield:model User --suffix'); |
| 109 | + |
| 110 | + $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); |
| 111 | + |
| 112 | + $filepath = APPPATH . 'Models/UserModel.php'; |
| 113 | + $this->assertFileExists($filepath); |
| 114 | + $this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath)); |
| 115 | + } |
| 116 | + |
| 117 | + public function testGenerateUserModelWithoutClassNameInput(): void |
| 118 | + { |
| 119 | + command('shield:model'); |
| 120 | + |
| 121 | + $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); |
| 122 | + |
| 123 | + $filepath = APPPATH . 'Models/UserModel.php'; |
| 124 | + $this->assertFileExists($filepath); |
| 125 | + $this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath)); |
| 126 | + } |
| 127 | + |
| 128 | + public function testGenerateUserCannotAcceptShieldUserModelAsInput(): void |
| 129 | + { |
| 130 | + command('shield:model ShieldUserModel'); |
| 131 | + |
| 132 | + $this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer); |
| 133 | + $this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php'); |
| 134 | + |
| 135 | + CITestStreamFilter::$buffer = ''; |
| 136 | + |
| 137 | + command('shield:model ShieldUser --suffix'); |
| 138 | + |
| 139 | + $this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer); |
| 140 | + $this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php'); |
| 141 | + } |
| 142 | +} |
0 commit comments