|
2 | 2 |
|
3 | 3 | namespace InterNACHI\Modular\Tests; |
4 | 4 |
|
| 5 | +use Illuminate\Support\Collection; |
5 | 6 | use InterNACHI\Modular\Support\ModuleConfig; |
6 | 7 | use InterNACHI\Modular\Support\ModuleRegistry; |
7 | 8 | use InterNACHI\Modular\Tests\Concerns\WritesToAppFilesystem; |
8 | 9 |
|
9 | 10 | class ModuleRegistryTest extends TestCase |
10 | 11 | { |
11 | 12 | use WritesToAppFilesystem; |
12 | | - |
| 13 | + |
| 14 | + public function test_module_for_class_prefers_longest_matching_namespace(): void |
| 15 | + { |
| 16 | + $general = new ModuleConfig('general', '/tmp/general', new Collection([ |
| 17 | + '/tmp/general/src' => 'App\\General\\', |
| 18 | + '/tmp/general/tests' => 'App\\General\\Tests', |
| 19 | + ])); |
| 20 | + |
| 21 | + $specific = new ModuleConfig('specific', '/tmp/specific', new Collection([ |
| 22 | + '/tmp/general-specific/src' => 'App\\General\\Specific\\', |
| 23 | + '/tmp/general-specific/tests' => 'App\\General\\Specific\\Tests', |
| 24 | + ])); |
| 25 | + |
| 26 | + $loader_orders = [ |
| 27 | + [$general, $specific], |
| 28 | + [$specific, $general], |
| 29 | + ]; |
| 30 | + |
| 31 | + $specific_model = 'App\\General\\Specific\\Models\\Thing'; |
| 32 | + $specific_test = 'App\\General\\Specific\\Tests\\ThingTest'; |
| 33 | + $general_model = 'App\\General\\Models\\Thing'; |
| 34 | + $general_test = 'App\\General\\Tests\\ThingTest'; |
| 35 | + |
| 36 | + foreach ($loader_orders as $modules) { |
| 37 | + $registry = new ModuleRegistry( |
| 38 | + modules_path: '/tmp', |
| 39 | + modules_loader: fn() => Collection::make($modules)->keyBy('name'), |
| 40 | + ); |
| 41 | + |
| 42 | + $this->assertEquals( |
| 43 | + $specific, |
| 44 | + $registry->moduleForClass($specific_model), |
| 45 | + 'Expected the more-specific module to win regardless of registration order (primary namespace)', |
| 46 | + ); |
| 47 | + |
| 48 | + $this->assertEquals( |
| 49 | + $specific, |
| 50 | + $registry->moduleForClass($specific_test), |
| 51 | + 'Expected the more-specific module to win regardless of registration order (secondary namespace)', |
| 52 | + ); |
| 53 | + |
| 54 | + $this->assertEquals( |
| 55 | + $general, |
| 56 | + $registry->moduleForClass($general_model), |
| 57 | + 'Expected the general module to return when a more-specific namespace does not match (primary namespace)', |
| 58 | + ); |
| 59 | + |
| 60 | + $this->assertEquals( |
| 61 | + $general, |
| 62 | + $registry->moduleForClass($general_test), |
| 63 | + 'Expected the general module to return when a more-specific namespace does not match (secondary namespace)', |
| 64 | + ); |
| 65 | + |
| 66 | + $this->assertNull( |
| 67 | + $registry->moduleForClass('Unrelated\\Thing'), |
| 68 | + 'Expected unrelated class not to match any module regardless of order' |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
13 | 73 | public function test_it_resolves_modules(): void |
14 | 74 | { |
15 | 75 | $this->makeModule('test-module'); |
|
0 commit comments