|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Dcat\Admin\Tests\Unit; |
| 4 | + |
| 5 | +use Dcat\Admin\Application; |
| 6 | +use Dcat\Admin\Tests\TestCase; |
| 7 | +use ReflectionProperty; |
| 8 | + |
| 9 | +class ApplicationTest extends TestCase |
| 10 | +{ |
| 11 | + protected function getProtectedProperty(object $object, string $property) |
| 12 | + { |
| 13 | + $reflection = new ReflectionProperty($object, $property); |
| 14 | + $reflection->setAccessible(true); |
| 15 | + |
| 16 | + return $reflection->getValue($object); |
| 17 | + } |
| 18 | + |
| 19 | + protected function setProtectedProperty(object $object, string $property, mixed $value): void |
| 20 | + { |
| 21 | + $reflection = new ReflectionProperty($object, $property); |
| 22 | + $reflection->setAccessible(true); |
| 23 | + $reflection->setValue($object, $value); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_constructor_sets_container(): void |
| 27 | + { |
| 28 | + $app = new Application($this->app); |
| 29 | + |
| 30 | + $this->assertSame($this->app, $this->getProtectedProperty($app, 'container')); |
| 31 | + } |
| 32 | + |
| 33 | + public function test_default_constant(): void |
| 34 | + { |
| 35 | + $this->assertEquals('admin', Application::DEFAULT); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_get_name_returns_default_when_no_name_set(): void |
| 39 | + { |
| 40 | + $app = new Application($this->app); |
| 41 | + |
| 42 | + $this->assertEquals('admin', $app->getName()); |
| 43 | + } |
| 44 | + |
| 45 | + public function test_with_name_sets_name(): void |
| 46 | + { |
| 47 | + $app = new Application($this->app); |
| 48 | + $app->withName('backend'); |
| 49 | + |
| 50 | + $this->assertEquals('backend', $app->getName()); |
| 51 | + } |
| 52 | + |
| 53 | + public function test_get_route_prefix_with_default(): void |
| 54 | + { |
| 55 | + $app = new Application($this->app); |
| 56 | + |
| 57 | + $this->assertEquals('dcat.admin.', $app->getRoutePrefix()); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_get_route_prefix_with_custom_app(): void |
| 61 | + { |
| 62 | + $app = new Application($this->app); |
| 63 | + |
| 64 | + $this->assertEquals('dcat.api.', $app->getRoutePrefix('api')); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_get_route_prefix_uses_current_name(): void |
| 68 | + { |
| 69 | + $app = new Application($this->app); |
| 70 | + $app->withName('dashboard'); |
| 71 | + |
| 72 | + $this->assertEquals('dcat.dashboard.', $app->getRoutePrefix()); |
| 73 | + } |
| 74 | + |
| 75 | + public function test_get_api_route_prefix_default(): void |
| 76 | + { |
| 77 | + $app = new Application($this->app); |
| 78 | + |
| 79 | + $this->assertEquals('dcat.admin.dcat-api.', $app->getApiRoutePrefix()); |
| 80 | + } |
| 81 | + |
| 82 | + public function test_get_api_route_prefix_with_custom_app(): void |
| 83 | + { |
| 84 | + $app = new Application($this->app); |
| 85 | + |
| 86 | + $this->assertEquals('dcat.backend.dcat-api.', $app->getApiRoutePrefix('backend')); |
| 87 | + } |
| 88 | + |
| 89 | + public function test_get_current_api_route_prefix(): void |
| 90 | + { |
| 91 | + $app = new Application($this->app); |
| 92 | + |
| 93 | + $this->assertEquals('dcat.admin.dcat-api.', $app->getCurrentApiRoutePrefix()); |
| 94 | + } |
| 95 | + |
| 96 | + public function test_get_current_api_route_prefix_after_name_change(): void |
| 97 | + { |
| 98 | + $app = new Application($this->app); |
| 99 | + $app->withName('dashboard'); |
| 100 | + |
| 101 | + $this->assertEquals('dcat.dashboard.dcat-api.', $app->getCurrentApiRoutePrefix()); |
| 102 | + } |
| 103 | + |
| 104 | + public function test_get_apps_returns_config(): void |
| 105 | + { |
| 106 | + $this->app['config']->set('admin.multi_app', ['api' => true, 'shop' => false]); |
| 107 | + |
| 108 | + $app = new Application($this->app); |
| 109 | + $apps = $app->getApps(); |
| 110 | + |
| 111 | + $this->assertIsArray($apps); |
| 112 | + $this->assertArrayHasKey('api', $apps); |
| 113 | + $this->assertArrayHasKey('shop', $apps); |
| 114 | + $this->assertTrue($apps['api']); |
| 115 | + $this->assertFalse($apps['shop']); |
| 116 | + } |
| 117 | + |
| 118 | + public function test_get_enabled_apps_filters_disabled(): void |
| 119 | + { |
| 120 | + $this->app['config']->set('admin.multi_app', ['api' => true, 'shop' => false]); |
| 121 | + |
| 122 | + $app = new Application($this->app); |
| 123 | + $enabled = $app->getEnabledApps(); |
| 124 | + |
| 125 | + $this->assertArrayHasKey('api', $enabled); |
| 126 | + $this->assertArrayNotHasKey('shop', $enabled); |
| 127 | + } |
| 128 | + |
| 129 | + public function test_get_apps_returns_empty_array_when_null(): void |
| 130 | + { |
| 131 | + $this->app['config']->set('admin.multi_app', null); |
| 132 | + |
| 133 | + $app = new Application($this->app); |
| 134 | + $apps = $app->getApps(); |
| 135 | + |
| 136 | + $this->assertIsArray($apps); |
| 137 | + $this->assertEmpty($apps); |
| 138 | + } |
| 139 | + |
| 140 | + public function test_get_apps_caches_result(): void |
| 141 | + { |
| 142 | + $this->app['config']->set('admin.multi_app', ['api' => true]); |
| 143 | + |
| 144 | + $app = new Application($this->app); |
| 145 | + $first = $app->getApps(); |
| 146 | + |
| 147 | + // Change config after first call - should still return cached result |
| 148 | + $this->app['config']->set('admin.multi_app', ['api' => true, 'shop' => true]); |
| 149 | + $second = $app->getApps(); |
| 150 | + |
| 151 | + $this->assertSame($first, $second); |
| 152 | + $this->assertArrayNotHasKey('shop', $second); |
| 153 | + } |
| 154 | + |
| 155 | + public function test_get_enabled_apps_returns_empty_when_all_disabled(): void |
| 156 | + { |
| 157 | + $this->app['config']->set('admin.multi_app', ['api' => false, 'shop' => false]); |
| 158 | + |
| 159 | + $app = new Application($this->app); |
| 160 | + $enabled = $app->getEnabledApps(); |
| 161 | + |
| 162 | + $this->assertEmpty($enabled); |
| 163 | + } |
| 164 | +} |
0 commit comments