Phred/tests/Feature/NewOpportunityRadarTest.php

82 lines
2.6 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Phred\Tests\Feature;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use Phred\Support\Http\CircuitBreakerMiddleware;
use Phred\Providers\Core\StorageServiceProvider;
use PHPUnit\Framework\TestCase;
final class NewOpportunityRadarTest extends TestCase
{
private object $app;
protected function setUp(): void
{
$root = dirname(__DIR__, 2);
$this->app = require $root . '/bootstrap/app.php';
CircuitBreakerMiddleware::clear();
}
public function testCircuitBreakerOpensAfterFailures(): void
{
$mock = new MockHandler([
new Response(500),
new Response(500),
new Response(200), // Won't be reached if threshold is 2
]);
$stack = HandlerStack::create($mock);
// Threshold = 2, Timeout = 60
$stack->push(new CircuitBreakerMiddleware(2, 60.0));
$client = new Client(['handler' => $stack]);
// First failure
$p1 = $client->getAsync('http://example.com');
try { $p1->wait(); } catch (\Throwable) {}
// Second failure -> should open circuit
$p2 = $client->getAsync('http://example.com');
try { $p2->wait(); } catch (\Throwable) {}
// Third call should be rejected by CB immediately
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Circuit breaker is open for host: example.com');
$client->get('http://example.com');
}
public function testS3AdapterResolutionThrowsWhenMissingDependencies(): void
{
2026-01-06 17:02:05 +00:00
$config = $this->createStub(\Phred\Support\Contracts\ConfigInterface::class);
$config->method('get')->willReturnMap([
['storage.default', 'local', 's3'],
['storage.disks.s3', null, [
'driver' => 's3',
'key' => 'key',
'secret' => 'secret',
'region' => 'us-east-1',
'bucket' => 'test',
]],
]);
$provider = new \Phred\Providers\Core\StorageServiceProvider();
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions([
\Phred\Support\Contracts\ConfigInterface::class => $config,
]);
$provider->register($builder, $config);
$container = $builder->build();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('AWS SDK not found');
$container->get(\League\Flysystem\FilesystemOperator::class);
}
}