Skip to content

Commit 2987a73

Browse files
committed
Fix tests to be compatible with randomized testing
1 parent 1cd7a35 commit 2987a73

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

tests/Commands/UserModelGeneratorTest.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,88 +12,95 @@
1212
*/
1313
final class UserModelGeneratorTest extends CIUnitTestCase
1414
{
15-
protected $streamFilter;
15+
private $streamFilter;
1616

1717
protected function setUp(): void
1818
{
1919
parent::setUp();
2020

2121
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();
2427
}
2528

2629
protected function tearDown(): void
2730
{
2831
parent::tearDown();
2932

3033
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();
3735
}
3836

39-
protected function getFileContents(string $filepath): string
37+
private function deleteTestFiles(): void
4038
{
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+
}
4350
}
51+
}
4452

45-
return file_get_contents($filepath) ?: '';
53+
private function getFileContents(string $filepath): string
54+
{
55+
return (string) @file_get_contents($filepath);
4656
}
4757

4858
public function testGenerateUserModel(): void
4959
{
5060
command('shield:model MyUserModel');
51-
$filepath = APPPATH . 'Models/MyUserModel.php';
5261

62+
$filepath = APPPATH . 'Models/MyUserModel.php';
5363
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
5464
$this->assertFileExists($filepath);
5565

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);
6071
}
6172

6273
public function testGenerateUserModelCustomNamespace(): void
6374
{
6475
command('shield:model MyUserModel --namespace CodeIgniter\\\\Shield');
65-
$filepath = HOMEPATH . 'src/Models/MyUserModel.php';
6676

77+
$filepath = HOMEPATH . 'src/Models/MyUserModel.php';
6778
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
6879
$this->assertFileExists($filepath);
6980

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);
7886
}
7987

8088
public function testGenerateUserModelWithForce(): void
8189
{
8290
command('shield:model MyUserModel');
83-
8491
command('shield:model MyUserModel --force');
85-
$this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer);
8692

87-
$filepath = APPPATH . 'Models/MyUserModel.php';
88-
$this->assertFileExists($filepath);
93+
$this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer);
94+
$this->assertFileExists(APPPATH . 'Models/MyUserModel.php');
8995
}
9096

9197
public function testGenerateUserModelWithSuffix(): void
9298
{
9399
command('shield:model MyUser --suffix');
94-
$filepath = APPPATH . 'Models/MyUserModel.php';
95100

96101
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
102+
103+
$filepath = APPPATH . 'Models/MyUserModel.php';
97104
$this->assertFileExists($filepath);
98105
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath));
99106
}

0 commit comments

Comments
 (0)