1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
<?php
declare(strict_types=1);
namespace Pheanstalk\Tests\Integration;
use Pheanstalk\Contract\SocketFactoryInterface;
use Pheanstalk\Contract\SocketInterface;
use Pheanstalk\Exception\ConnectionException;
use Pheanstalk\Socket\FsockopenSocket;
use Pheanstalk\Socket\SocketSocket;
use Pheanstalk\Socket\StreamSocket;
use Pheanstalk\SocketFactory;
use Pheanstalk\Values\SocketImplementation;
use Pheanstalk\Values\Timeout;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;
#[CoversClass(SocketFactory::class)]
#[CoversClass(SocketSocket::class)]
#[CoversClass(FsockopenSocket::class)]
#[CoversClass(StreamSocket::class)]
final class SocketFactoryTest extends TestCase
{
/**
* @phpstan-return iterable<array{0: SocketFactory, 1: class-string}>
*/
public static function factoryProvider(): iterable
{
if (SERVER_HOST !== '') {
yield [new SocketFactory(SERVER_HOST, implementation: SocketImplementation::SOCKET), SocketSocket::class];
yield [new SocketFactory(SERVER_HOST, implementation: SocketImplementation::STREAM), StreamSocket::class];
yield [
new SocketFactory(SERVER_HOST, implementation: SocketImplementation::FSOCKOPEN),
FsockopenSocket::class
];
}
if (UNIX_SERVER_HOST !== '') {
yield [
new SocketFactory(UNIX_SERVER_HOST, implementation: SocketImplementation::SOCKET),
SocketSocket::class
];
yield [
new SocketFactory(UNIX_SERVER_HOST, implementation: SocketImplementation::STREAM),
StreamSocket::class
];
yield [
new SocketFactory(UNIX_SERVER_HOST, implementation: SocketImplementation::FSOCKOPEN),
FsockopenSocket::class
];
}
}
/**
* @param class-string<SocketInterface> $expectedImplementationClass
*/
#[RequiresPhpunit('< 11')]
public function testImplementations(SocketFactoryInterface $factory, string $expectedImplementationClass): void
{
Assert::assertInstanceOf($expectedImplementationClass, $factory->create());
}
/**
* @return iterable<array{0: string, 1: SocketImplementation|null}>
*/
public static function invalidHostProvider(): iterable
{
$hosts = ['not-valid', '192.0.2.123', 'unix:///invalid-file'];
$implementations = [...SocketImplementation::cases(), null];
foreach ($hosts as $host) {
foreach ($implementations as $implementation) {
yield [$host, $implementation];
}
}
}
#[DataProvider("invalidHostProvider")]
public function testExceptionOnNonExistentHost(string $host, SocketImplementation|null $implementation): void
{
$factory = new SocketFactory(host: $host, implementation: $implementation, connectTimeout: new Timeout(0, 500000));
$this->expectException(ConnectionException::class);
$factory->create();
}
}
|