Files
InvoiceShelf/tests/Feature/Company/Modules/ModuleMakeStubTest.php
Darko Gjorgjijoski 93b04a0c2a test(modules): integration tests for company surfaces and stub generator
End-to-end coverage for the new module APIs and the custom module:make
stubs shipped from invoiceshelf/modules. Each test file is hermetic — uses
\InvoiceShelf\Modules\Registry::flush() in setup/teardown to prevent
cross-test contamination, and ModuleMakeStubTest cleans up generated test
artifacts (the throwaway scaffold directory and the storage statuses entry).

- CompanyModulesIndexTest: 4 tests covering only-enabled-modules filter,
  has_settings flag computed against the real Registry, menu inclusion, and
  the empty-state response.

- ModuleSettingsControllerTest: 7 tests covering 404 for unregistered slug,
  show schema + defaults round-trip, persistence with the
  module.{slug}.{key} prefix, missing-required-field rejection, unknown-key
  silent-drop, update 404, and per-company isolation (the load-bearing
  multi-tenancy guarantee).

- BootstrapModuleMenuTest: 3 tests covering Registry-driven module_menu
  population on the company-context bootstrap branch, the empty default
  when nothing is registered, and the absence of module_menu on the
  super-admin-mode branch.

- ModuleMakeStubTest: 3 tests that actually run
  Artisan::call('module:make', ['name' => ['ScaffoldProbe']]) against a
  throwaway module name and assert the generated ServiceProvider contains
  use InvoiceShelf\Modules\Registry, the generated composer.json requires
  invoiceshelf/modules: ^3.0, and starter lang/en/{menu,settings}.php exist.
  Validates that the custom stubs shipped from the package are picked up
  via Stub::setBasePath().
2026-04-09 00:30:24 +02:00

83 lines
3.4 KiB
PHP

<?php
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
/**
* Exercises the custom generator stubs shipped from the invoiceshelf/modules
* package. These override nwidart's defaults so that a fresh
* `php artisan module:make` scaffold already includes the Registry boilerplate
* and the starter i18n files that the boilerplate references.
*
* The test generates a throwaway module, inspects the generated files, then
* cleans up (including nwidart's status entry) so the rest of the suite is
* unaffected.
*/
beforeEach(function () {
$this->scaffoldModule = 'ScaffoldProbe';
$this->scaffoldPath = base_path('Modules/'.$this->scaffoldModule);
// Make sure no leftover from a previous crashed test.
if (File::isDirectory($this->scaffoldPath)) {
File::deleteDirectory($this->scaffoldPath);
}
});
afterEach(function () {
if (File::isDirectory($this->scaffoldPath)) {
File::deleteDirectory($this->scaffoldPath);
}
// nwidart writes module activation state to storage/app/modules_statuses.json
// when module:make auto-enables the new module. Remove our scaffold entry
// so the file doesn't accumulate stale test data across runs.
$statusesFile = storage_path('app/modules_statuses.json');
if (File::exists($statusesFile)) {
$statuses = json_decode(File::get($statusesFile), true) ?? [];
unset($statuses[$this->scaffoldModule]);
File::put($statusesFile, json_encode($statuses, JSON_PRETTY_PRINT));
}
});
test('module:make generates a ServiceProvider that uses InvoiceShelf\\Modules\\Registry', function () {
Artisan::call('module:make', ['name' => [$this->scaffoldModule]]);
$providerPath = $this->scaffoldPath.'/app/Providers/'.$this->scaffoldModule.'ServiceProvider.php';
expect(File::exists($providerPath))->toBeTrue();
$contents = File::get($providerPath);
expect($contents)->toContain('use InvoiceShelf\\Modules\\Registry as ModuleRegistry;');
expect($contents)->toContain('ModuleRegistry::registerMenu(');
expect($contents)->toContain('ModuleRegistry::registerSettings(');
expect($contents)->toContain("protected string \$name = '{$this->scaffoldModule}';");
expect($contents)->toContain('Str::kebab($this->name)');
});
test('module:make generates a composer.json that requires invoiceshelf/modules', function () {
Artisan::call('module:make', ['name' => [$this->scaffoldModule]]);
$composerPath = $this->scaffoldPath.'/composer.json';
expect(File::exists($composerPath))->toBeTrue();
$manifest = json_decode(File::get($composerPath), true);
expect($manifest['require'] ?? [])->toHaveKey('invoiceshelf/modules');
expect($manifest['require']['invoiceshelf/modules'])->toBe('^3.0');
});
test('module:make generates starter lang files for menu and settings', function () {
Artisan::call('module:make', ['name' => [$this->scaffoldModule]]);
expect(File::exists($this->scaffoldPath.'/lang/en/menu.php'))->toBeTrue();
expect(File::exists($this->scaffoldPath.'/lang/en/settings.php'))->toBeTrue();
$menu = require $this->scaffoldPath.'/lang/en/menu.php';
expect($menu)->toHaveKey('title');
expect($menu['title'])->toBe('ScaffoldProbe');
$settings = require $this->scaffoldPath.'/lang/en/settings.php';
expect($settings)->toHaveKey('general_section');
expect($settings)->toHaveKey('enabled');
});