72 lines
2.4 KiB
PHP
72 lines
2.4 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use App\Services\WikipediaService;
|
||
|
|
use Illuminate\Console\Attributes\Description;
|
||
|
|
use Illuminate\Console\Attributes\Signature;
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
|
||
|
|
#[Signature('test:wikipedia')]
|
||
|
|
#[Description('Test Wikipedia API integration')]
|
||
|
|
class TestWikipediaCommand extends Command
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private WikipediaService $wikipediaService
|
||
|
|
) {
|
||
|
|
parent::__construct();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the console command.
|
||
|
|
*/
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$this->info('Testing Wikipedia API integration...');
|
||
|
|
$this->newLine();
|
||
|
|
|
||
|
|
// Test 1: Search for cities
|
||
|
|
$this->info('Test 1: Searching for "Paris"');
|
||
|
|
try {
|
||
|
|
$results = $this->wikipediaService->searchCities('Paris');
|
||
|
|
$this->line('Found ' . count($results) . ' results:');
|
||
|
|
foreach (array_slice($results, 0, 3) as $result) {
|
||
|
|
$this->line(' - ' . $result['title'] . ': ' . $result['description']);
|
||
|
|
}
|
||
|
|
$this->newLine();
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->error('Search failed: ' . $e->getMessage());
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Test 2: Get city summary
|
||
|
|
$this->info('Test 2: Getting summary for "Paris"');
|
||
|
|
try {
|
||
|
|
$cityData = $this->wikipediaService->getCitySummary('Paris');
|
||
|
|
if ($cityData) {
|
||
|
|
$this->line('Title: ' . $cityData['title']);
|
||
|
|
$this->line('Summary: ' . substr($cityData['summary'], 0, 200) . '...');
|
||
|
|
$this->line('Coordinates: ' . $cityData['latitude'] . ', ' . $cityData['longitude']);
|
||
|
|
$this->line('URL: ' . $cityData['url']);
|
||
|
|
} else {
|
||
|
|
$this->warn('No data found for Paris');
|
||
|
|
}
|
||
|
|
$this->newLine();
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
$this->error('Summary fetch failed: ' . $e->getMessage());
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Test 3: Test caching
|
||
|
|
$this->info('Test 3: Testing cache (fetching Paris again)');
|
||
|
|
$start = microtime(true);
|
||
|
|
$this->wikipediaService->getCitySummary('Paris');
|
||
|
|
$elapsed = (microtime(true) - $start) * 1000;
|
||
|
|
$this->line('Fetch time: ' . round($elapsed, 2) . 'ms (should be fast if cached)');
|
||
|
|
$this->newLine();
|
||
|
|
|
||
|
|
$this->info('✓ All tests completed successfully!');
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|