Skip to content

Commit 3d9a3e9

Browse files
committed
refactor: streamline output messages in ApiVersionsCommand and update error response structure in tests
1 parent 5b0e77d commit 3d9a3e9

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

src/Console/Commands/ApiVersionsCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function handle(): int
9090
if ($this->option('json')) {
9191
$this->line(json_encode(['routes' => [], 'supported_versions' => $this->versionManager->getSupportedVersions()], JSON_PRETTY_PRINT));
9292
} else {
93-
$this->components->info('No matching routes found.');
93+
$this->info('No matching routes found.');
9494
}
9595

9696
return self::SUCCESS;
@@ -120,8 +120,8 @@ public function handle(): int
120120
$this->table($headers, $rows);
121121

122122
$this->newLine();
123-
$this->components->info('Supported API Versions: '.implode(', ', $this->versionManager->getSupportedVersions()));
124-
$this->components->info('Total Routes: '.count($rows));
123+
$this->info('Supported API Versions: '.implode(', ', $this->versionManager->getSupportedVersions()));
124+
$this->info('Total Routes: '.count($rows));
125125

126126
return self::SUCCESS;
127127
}

tests/Feature/AttributeVersioningTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959

6060
$response->assertStatus(400)
6161
->assertJson([
62-
'error' => 'Unsupported API Version',
62+
'title' => 'Unsupported API Version',
63+
'status' => 400,
6364
]);
6465
});
6566

tests/Unit/AttributeApiVersionMiddlewareTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@
178178
expect($result->getStatusCode())->toBe(400);
179179

180180
$data = $result->getData(true);
181-
expect($data['error'])->toBe('Unsupported API Version');
182-
expect($data['message'])->toBe("API version '3.0' is not supported for this endpoint.");
181+
expect($data['title'])->toBe('Unsupported API Version');
182+
expect($data['detail'])->toBe("API version '3.0' is not supported for this endpoint.");
183+
expect($data['status'])->toBe(400);
183184
expect($data['requested_version'])->toBe('3.0');
184185
expect($data['supported_versions'])->toBe(['1.0', '2.0', '2.1']);
185186
expect($data['endpoint_versions'])->toBe(['1.0', '2.0']);
@@ -211,8 +212,9 @@
211212
expect($result->getStatusCode())->toBe(400);
212213

213214
$data = $result->getData(true);
214-
expect($data['error'])->toBe('Unsupported API Version');
215-
expect($data['message'])->toBe("API version '4.0' is not supported.");
215+
expect($data['title'])->toBe('Unsupported API Version');
216+
expect($data['detail'])->toBe("API version '4.0' is not supported for this endpoint.");
217+
expect($data['status'])->toBe(400);
216218
expect($data['requested_version'])->toBe('4.0');
217219
expect($data['supported_versions'])->toBe(['1.0', '2.0']);
218220
});
@@ -226,18 +228,15 @@
226228
->once()
227229
->andReturn('2.0');
228230

229-
$this->versionManager->shouldReceive('getSupportedVersions')
230-
->once()
231-
->andReturn(['1.0', '2.0']);
232-
233231
$result = $this->middleware->handle($request, fn () => new Response);
234232

235233
expect($result)->toBeInstanceOf(JsonResponse::class);
236-
expect($result->getStatusCode())->toBe(400);
234+
expect($result->getStatusCode())->toBe(404);
237235

238236
$data = $result->getData(true);
239-
expect($data['error'])->toBe('Unsupported API Version');
240-
expect($data['message'])->toBe('Route not found');
237+
expect($data['title'])->toBe('Route Not Found');
238+
expect($data['detail'])->toBe('Route not found');
239+
expect($data['status'])->toBe(404);
241240
});
242241
});
243242

tests/Unit/AttributeVersionResolverTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Routing\Route;
44
use ShahGhasiAdil\LaravelApiVersioning\Examples\SharedController;
55
use ShahGhasiAdil\LaravelApiVersioning\Examples\V1UserController;
6+
use ShahGhasiAdil\LaravelApiVersioning\Services\AttributeCacheService;
67
use ShahGhasiAdil\LaravelApiVersioning\Services\AttributeVersionResolver;
78
use ShahGhasiAdil\LaravelApiVersioning\Services\VersionManager;
89

@@ -19,7 +20,8 @@
1920
];
2021

2122
$this->versionManager = new VersionManager($config);
22-
$this->resolver = new AttributeVersionResolver($this->versionManager);
23+
$this->cache = new AttributeCacheService(enabled: false, ttl: 3600); // Disable cache for tests
24+
$this->resolver = new AttributeVersionResolver($this->versionManager, $this->cache);
2325
});
2426

2527
test('resolves version from controller attribute', function () {

0 commit comments

Comments
 (0)