Files
2026-05-01 23:33:15 +03:00

114 lines
3.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\JobApi\Exceptions\ApiException;
use App\Services\JobApi\JobApiClientFactory;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
#[Signature('jobs:test-apis {source?} {--limit=5}')]
#[Description('Test job API integrations by fetching sample jobs')]
class TestJobApis extends Command
{
protected $signature = 'jobs:test-apis {source?} {--limit=5}';
protected $description = 'Test job API integrations by fetching sample jobs';
/**
* Execute the console command.
*/
public function handle(): int
{
$sourceName = $this->argument('source');
$limit = (int) $this->option('limit');
$this->info('Testing Job API Integrations...');
$this->newLine();
try {
if ($sourceName) {
// Test specific source
$this->testSingleSource($sourceName, $limit);
} else {
// Test all active sources
$this->testAllSources($limit);
}
return self::SUCCESS;
} catch (\Exception $e) {
$this->error("Failed to test APIs: {$e->getMessage()}");
return self::FAILURE;
}
}
private function testAllSources(int $limit): void
{
$clients = JobApiClientFactory::makeAll();
if (empty($clients)) {
$this->warn('No active job sources found. Run: php artisan db:seed --class=JobSourceSeeder');
return;
}
$this->info('Testing ' . count($clients) . ' active job sources...');
$this->newLine();
foreach ($clients as $client) {
$this->testClient($client, $limit);
$this->newLine();
}
}
private function testSingleSource(string $sourceName, int $limit): void
{
try {
$client = JobApiClientFactory::makeByName($sourceName);
$this->testClient($client, $limit);
} catch (ApiException $e) {
$this->error("Failed to create client for '{$sourceName}': {$e->getMessage()}");
}
}
private function testClient($client, int $limit): void
{
$sourceName = $client->getSourceName();
$this->line("<fg=cyan>Testing: {$sourceName}</>");
try {
// Fetch sample jobs
$jobs = $client->fetchJobs(['limit' => $limit]);
if (empty($jobs)) {
$this->warn(" No jobs returned from {$sourceName}");
return;
}
$this->info(" ✓ Successfully fetched " . count($jobs) . " jobs");
// Display sample jobs
$this->table(
['ID', 'Title', 'Company', 'Location', 'Remote'],
collect($jobs)->take(3)->map(fn($job) => [
substr($job->externalId, 0, 10) . '...',
substr($job->title, 0, 40),
substr($job->company, 0, 20),
substr($job->location ?? 'N/A', 0, 20),
$job->remoteAllowed ? 'Yes' : 'No',
])->toArray()
);
} catch (ApiException $e) {
$this->error(" ✗ API Error: {$e->getMessage()}");
if ($context = $e->getContext()) {
$this->line(" Context: " . json_encode($context, JSON_PRETTY_PRINT));
}
} catch (\Exception $e) {
$this->error(" ✗ Unexpected Error: {$e->getMessage()}");
$this->line(" " . $e->getTraceAsString());
}
}
}