Skip to content

Commit

Permalink
Fix getApplicationBasePath naming in testbench (#111)
Browse files Browse the repository at this point in the history
* Fix `getApplicationBasePath` naming in testbench

* Reset 'modelNameResolvers' too

* revert version constraints

* Update DatabaseFactoryHelper.php
  • Loading branch information
inxilpro authored Feb 27, 2025
1 parent 1326b57 commit a8cf59f
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"ext-simplexml": "*",
"ext-dom": "*",
"composer/composer": "^2.1",
"illuminate/support": "^9|^10|^11|12.x-dev|dev-master"
"illuminate/support": "^9|^10|^11|^12|13.x-dev|dev-master|dev-main"
},
"require-dev": {
"orchestra/testbench": ">=7.10|dev-master",
"orchestra/testbench": "^7.52|^8.33|^9.11|dev-master|dev-main",
"friendsofphp/php-cs-fixer": "^3.14",
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5|^10.5",
Expand Down
5 changes: 4 additions & 1 deletion src/Support/DatabaseFactoryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function modelNameResolver(): Closure
// Temporarily disable the modular resolver if we're not in a module
try {
$this->unsetProperty(Factory::class, 'modelNameResolver');
$this->unsetProperty(Factory::class, 'modelNameResolvers');
return $factory->modelName();
} finally {
Factory::guessModelNamesUsing($this->modelNameResolver());
Expand Down Expand Up @@ -80,6 +81,8 @@ protected function getProperty($target, $property)
protected function unsetProperty($target, $property): void
{
$reflection = new ReflectionClass($target);
$reflection->setStaticPropertyValue($property, null);
if ($reflection->hasProperty($property)) {
$reflection->setStaticPropertyValue($property, null);
}
}
}
2 changes: 1 addition & 1 deletion tests/AutoDiscoveryHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function setUp(): void
$this->module1 = $this->makeModule('test-module');
$this->module2 = $this->makeModule('test-module-two');
$this->helper = new AutoDiscoveryHelper(
new ModuleRegistry($this->getBasePath().'/app-modules', ''),
new ModuleRegistry($this->getApplicationBasePath().'/app-modules', ''),
new Filesystem()
);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Commands/Make/MakeModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function test_it_scaffolds_a_new_module(): void
]);

$fs = $this->filesystem();
$module_path = $this->getBasePath().'/app-modules/'.$module_name;
$module_path = $this->getApplicationBasePath().'/app-modules/'.$module_name;

$this->assertTrue($fs->isDirectory($module_path));
$this->assertTrue($fs->isDirectory($module_path.'/database'));
Expand All @@ -48,7 +48,7 @@ public function test_it_scaffolds_a_new_module(): void
$this->assertContains('database/seeds', $composer_contents['autoload']['classmap']);
}

$app_composer_file = $this->getBasePath().'/composer.json';
$app_composer_file = $this->getApplicationBasePath().'/composer.json';
$app_composer_contents = json_decode($fs->get($app_composer_file), true);

$this->assertEquals('*', $app_composer_contents['require']["modules/{$module_name}"]);
Expand Down Expand Up @@ -92,12 +92,12 @@ public function test_it_prompts_on_first_module_if_no_custom_namespace_is_set():

Modules::reload();

$this->assertTrue($fs->isDirectory($this->getBasePath().'/app-modules/test-module'));
$this->assertTrue($fs->isDirectory($this->getApplicationBasePath().'/app-modules/test-module'));

$this->artisan(MakeModule::class, ['name' => 'test-module-two'])
->assertExitCode(0);

$this->assertTrue($fs->isDirectory($this->getBasePath().'/app-modules/test-module-two'));
$this->assertTrue($fs->isDirectory($this->getApplicationBasePath().'/app-modules/test-module-two'));
}

public function test_it_does_not_create_an_empty_directory_if_prompt_on_first_module_if_no_custom_namespace_is_set_is_rejected(): void
Expand All @@ -110,6 +110,6 @@ public function test_it_does_not_create_an_empty_directory_if_prompt_on_first_mo

Modules::reload();

$this->assertFalse($fs->isDirectory($this->getBasePath().'/app-modules/test-module'));
$this->assertFalse($fs->isDirectory($this->getApplicationBasePath().'/app-modules/test-module'));
}
}
4 changes: 2 additions & 2 deletions tests/Commands/Make/MakeSeederTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_it_scaffolds_a_seeder_in_the_module_when_module_option_is_s
$expected_substrings[] = 'namespace Modules\TestModule\Database\Seeders;';
}

$this->filesystem()->deleteDirectory($this->getBasePath().$this->normalizeDirectorySeparators('database/seeds'));
$this->filesystem()->deleteDirectory($this->getApplicationBasePath().$this->normalizeDirectorySeparators('database/seeds'));
$this->filesystem()->deleteDirectory($this->getModulePath('test-module', 'database/seeds'));

$this->assertModuleCommandResults($command, $arguments, $expected_path, $expected_substrings);
Expand All @@ -59,7 +59,7 @@ public function test_it_scaffolds_a_seeder_in_the_app_when_module_option_is_miss
$expected_substrings[] = 'namespace Database\Seeders;';
}

$this->filesystem()->deleteDirectory($this->getBasePath().$this->normalizeDirectorySeparators('database/seeds'));
$this->filesystem()->deleteDirectory($this->getApplicationBasePath().$this->normalizeDirectorySeparators('database/seeds'));

$this->assertBaseCommandResults($command, $arguments, $expected_path, $expected_substrings);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Commands/ModulesCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function test_it_writes_to_cache_file(): void

$this->artisan(ModulesCache::class);

$expected_path = $this->getBasePath().$this->normalizeDirectorySeparators('bootstrap/cache/modules.php');
$expected_path = $this->getApplicationBasePath().$this->normalizeDirectorySeparators('bootstrap/cache/modules.php');

$this->assertFileExists($expected_path);

Expand Down
2 changes: 1 addition & 1 deletion tests/Commands/ModulesClearTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function test_it_writes_to_cache_file(): void
{
$this->artisan(ModulesCache::class);

$expected_path = $this->getBasePath().$this->normalizeDirectorySeparators('bootstrap/cache/modules.php');
$expected_path = $this->getApplicationBasePath().$this->normalizeDirectorySeparators('bootstrap/cache/modules.php');

$this->assertFileExists($expected_path);

Expand Down
2 changes: 1 addition & 1 deletion tests/Concerns/TestsMakeCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function assertModuleFile($expected_path, $expected_substrings = [], $

protected function assertBaseFile($expected_path, $expected_substrings = [])
{
$full_path = $this->getBasePath().$this->normalizeDirectorySeparators($expected_path);
$full_path = $this->getApplicationBasePath().$this->normalizeDirectorySeparators($expected_path);

$directory = dirname($full_path);
$files = implode(', ', glob($directory.'/*') ?? []);
Expand Down

0 comments on commit a8cf59f

Please sign in to comment.