#!/usr/bin/env php register('engines', $search_engines); $search = new ImgSearch($folder, $word); $data = $search->getImageData(); echo "Raw JSON output:\n"; echo json_encode($data, JSON_PRETTY_PRINT) . "\n\n"; test('Result is an array', is_array($data)); test('Has result key', isset($data['result'])); if (isset($data['result']) && $data['result'] === true) { test('Search returned results', true); test('Has results array', isset($data['results'])); if (isset($data['results'])) { $count = count($data['results']); echo " Results count: $count\n"; } } else { echo " ⚠ No results returned. Check your API key and configuration.\n"; } } /** * Test Brave search JSON output */ function testBraveSearch() { echo "\n=== Brave Search JSON Test ===\n"; echo "Query: 'test' | Results: 3\n\n"; $brave = new Brave(); $brave->setQuery('test'); $brave->setParam('size', 3); $json = $brave->getData(); echo "Raw JSON output:\n"; echo json_encode($json, JSON_PRETTY_PRINT) . "\n\n"; // Validate structure test('Result is an array', is_array($json)); test('Has result key', isset($json['result'])); if (isset($json['result']) && $json['result'] === true) { test('Search returned results', true); test('Has results array', isset($json['results'])); test('Engine is Brave', isset($json['engine']) && $json['engine'] === 'Brave'); if (isset($json['results'])) { $count = count($json['results']); test("Results count is 3 (got $count)", $count === 3); // Check first result structure if ($count > 0) { $first = $json['results'][0]; test('First result has clickurl', isset($first['clickurl'])); test('First result has thumb', isset($first['thumb'])); test('First result has referer', isset($first['referer'])); } } } else { echo "\n ⚠ No results returned. Check your API key and CX.\n"; } } // Main execution if (in_array('--search', $argv)) { testImgSearch(); } elseif (in_array('--brave', $argv)) { testBraveSearch(); } else { runBasicTests(); // Ask if user wants to run Brave test if (defined('BRAVE_API_KEY') && BRAVE_API_KEY !== 'YOUR_BRAVE_API_KEY') { echo "\nRun Brave search test? (y/n): "; $handle = fopen("php://stdin", "r"); $line = fgets($handle); if (trim(strtolower($line)) === 'y') { testBraveSearch(); } fclose($handle); } else { echo "\n ⚠ Skipping Brave test: API key not configured\n"; } } // Summary echo "\n=== Results ===\n"; echo "Passed: $passed\n"; echo "Failed: $failed\n"; echo ($failed === 0 ? "All tests passed! ✓" : "Some tests failed.") . "\n\n"; exit($failed > 0 ? 1 : 0); ?>