참고소스 수정본
This commit is contained in:
5
참고/firecrawl-main/apps/php-sdk/tests/Pest.php
Normal file
5
참고/firecrawl-main/apps/php-sdk/tests/Pest.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
uses()->group('unit')->in('Unit');
|
||||
174
참고/firecrawl-main/apps/php-sdk/tests/Unit/ModelsTest.php
Normal file
174
참고/firecrawl-main/apps/php-sdk/tests/Unit/ModelsTest.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Firecrawl\Models\CreditUsage;
|
||||
use Firecrawl\Models\MapData;
|
||||
use Firecrawl\Models\BatchScrapeJob;
|
||||
use Firecrawl\Models\CrawlJob;
|
||||
use Firecrawl\Models\HighlightsFormat;
|
||||
use Firecrawl\Models\QueryFormat;
|
||||
use Firecrawl\Models\QuestionFormat;
|
||||
use Firecrawl\Models\ScrapeOptions;
|
||||
|
||||
it('hydrates CreditUsage from nested data key', function (): void {
|
||||
$response = [
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'remainingCredits' => 500,
|
||||
'planCredits' => 1000,
|
||||
'billingPeriodStart' => '2025-01-01',
|
||||
'billingPeriodEnd' => '2025-02-01',
|
||||
],
|
||||
];
|
||||
|
||||
$usage = CreditUsage::fromArray($response);
|
||||
|
||||
expect($usage->getRemainingCredits())->toBe(500);
|
||||
expect($usage->getPlanCredits())->toBe(1000);
|
||||
expect($usage->getBillingPeriodStart())->toBe('2025-01-01');
|
||||
expect($usage->getBillingPeriodEnd())->toBe('2025-02-01');
|
||||
});
|
||||
|
||||
it('hydrates CreditUsage from flat data', function (): void {
|
||||
$response = [
|
||||
'remainingCredits' => 250,
|
||||
'planCredits' => 500,
|
||||
];
|
||||
|
||||
$usage = CreditUsage::fromArray($response);
|
||||
|
||||
expect($usage->getRemainingCredits())->toBe(250);
|
||||
expect($usage->getPlanCredits())->toBe(500);
|
||||
});
|
||||
|
||||
it('guards MapData links against non-array input', function (): void {
|
||||
$data = ['links' => 'not-an-array'];
|
||||
|
||||
$map = MapData::fromArray($data);
|
||||
|
||||
expect($map->getLinks())->toBe([]);
|
||||
});
|
||||
|
||||
it('normalizes MapData string links', function (): void {
|
||||
$data = [
|
||||
'links' => [
|
||||
'https://example.com',
|
||||
['url' => 'https://example.com/about', 'title' => 'About'],
|
||||
],
|
||||
];
|
||||
|
||||
$map = MapData::fromArray($data);
|
||||
|
||||
expect($map->getLinks())->toHaveCount(2);
|
||||
expect($map->getLinks()[0])->toBe(['url' => 'https://example.com']);
|
||||
expect($map->getLinks()[1])->toBe(['url' => 'https://example.com/about', 'title' => 'About']);
|
||||
});
|
||||
|
||||
it('casts creditsUsed to int in BatchScrapeJob', function (): void {
|
||||
$raw = [
|
||||
'id' => 'batch-123',
|
||||
'status' => 'completed',
|
||||
'completed' => 5,
|
||||
'total' => 5,
|
||||
'creditsUsed' => '42',
|
||||
'data' => [],
|
||||
];
|
||||
|
||||
$job = BatchScrapeJob::fromArray($raw);
|
||||
|
||||
expect($job->getCreditsUsed())->toBe(42);
|
||||
expect($job->getCreditsUsed())->toBeInt();
|
||||
});
|
||||
|
||||
it('preserves null creditsUsed in CrawlJob', function (): void {
|
||||
$raw = [
|
||||
'id' => 'crawl-123',
|
||||
'status' => 'scraping',
|
||||
'data' => [],
|
||||
];
|
||||
|
||||
$job = CrawlJob::fromArray($raw);
|
||||
|
||||
expect($job->getCreditsUsed())->toBeNull();
|
||||
});
|
||||
|
||||
it('preserves positional integration in ScrapeOptions::with', function (): void {
|
||||
$options = ScrapeOptions::with(
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
false,
|
||||
'php-sdk',
|
||||
);
|
||||
|
||||
expect($options->getStoreInCache())->toBeFalse();
|
||||
expect($options->getIntegration())->toBe('php-sdk');
|
||||
expect($options->getLockdown())->toBeNull();
|
||||
expect($options->toArray())->toMatchArray([
|
||||
'storeInCache' => false,
|
||||
'integration' => 'php-sdk',
|
||||
]);
|
||||
});
|
||||
|
||||
it('serializes lockdown in ScrapeOptions', function (): void {
|
||||
$options = ScrapeOptions::with(
|
||||
lockdown: true,
|
||||
integration: 'php-sdk',
|
||||
);
|
||||
|
||||
expect($options->getLockdown())->toBeTrue();
|
||||
expect($options->toArray())->toMatchArray([
|
||||
'lockdown' => true,
|
||||
'integration' => 'php-sdk',
|
||||
]);
|
||||
});
|
||||
|
||||
it('serializes query format mode in ScrapeOptions', function (): void {
|
||||
$options = ScrapeOptions::with(
|
||||
formats: [QueryFormat::with('What is Firecrawl?', QueryFormat::MODE_DIRECT_QUOTE)],
|
||||
);
|
||||
|
||||
expect($options->toArray()['formats'][0])->toMatchArray([
|
||||
'type' => 'query',
|
||||
'prompt' => 'What is Firecrawl?',
|
||||
'mode' => 'directQuote',
|
||||
]);
|
||||
});
|
||||
|
||||
it('serializes question and highlights formats in ScrapeOptions', function (): void {
|
||||
$options = ScrapeOptions::with(
|
||||
formats: [
|
||||
QuestionFormat::with('What is Firecrawl?'),
|
||||
HighlightsFormat::with('What is Firecrawl?'),
|
||||
],
|
||||
);
|
||||
|
||||
expect($options->toArray()['formats'])->toMatchArray([
|
||||
[
|
||||
'type' => 'question',
|
||||
'question' => 'What is Firecrawl?',
|
||||
],
|
||||
[
|
||||
'type' => 'highlights',
|
||||
'query' => 'What is Firecrawl?',
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('rejects invalid query format mode', function (): void {
|
||||
QueryFormat::with('What is Firecrawl?', 'quoted');
|
||||
})->throws(InvalidArgumentException::class, "query mode must be 'freeform' or 'directQuote'");
|
||||
48
참고/firecrawl-main/apps/php-sdk/tests/Unit/ParseTest.php
Normal file
48
참고/firecrawl-main/apps/php-sdk/tests/Unit/ParseTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Firecrawl\Exceptions\FirecrawlException;
|
||||
use Firecrawl\Models\JsonFormat;
|
||||
use Firecrawl\Models\ParseFile;
|
||||
use Firecrawl\Models\ParseOptions;
|
||||
|
||||
it('builds a ParseFile from bytes', function (): void {
|
||||
$file = ParseFile::fromBytes('doc.pdf', 'hello');
|
||||
|
||||
expect($file->getFilename())->toBe('doc.pdf');
|
||||
expect($file->getContent())->toBe('hello');
|
||||
});
|
||||
|
||||
it('rejects empty filename', function (): void {
|
||||
ParseFile::fromBytes(' ', 'hello');
|
||||
})->throws(FirecrawlException::class);
|
||||
|
||||
it('rejects empty content', function (): void {
|
||||
ParseFile::fromBytes('doc.pdf', '');
|
||||
})->throws(FirecrawlException::class);
|
||||
|
||||
it('serializes ParseOptions with JSON format', function (): void {
|
||||
$options = ParseOptions::with(
|
||||
formats: ['markdown', JsonFormat::with(prompt: 'Extract')],
|
||||
onlyMainContent: true,
|
||||
);
|
||||
|
||||
$array = $options->toArray();
|
||||
|
||||
expect($array['formats'][0])->toBe('markdown');
|
||||
expect($array['formats'][1])->toMatchArray(['type' => 'json', 'prompt' => 'Extract']);
|
||||
expect($array['onlyMainContent'])->toBeTrue();
|
||||
});
|
||||
|
||||
it('rejects unsupported parse formats', function (): void {
|
||||
ParseOptions::with(formats: ['screenshot']);
|
||||
})->throws(FirecrawlException::class);
|
||||
|
||||
it('rejects invalid proxy values', function (): void {
|
||||
ParseOptions::with(proxy: 'stealth');
|
||||
})->throws(FirecrawlException::class);
|
||||
|
||||
it('rejects non-positive timeout', function (): void {
|
||||
ParseOptions::with(timeout: 0);
|
||||
})->throws(FirecrawlException::class);
|
||||
Reference in New Issue
Block a user