assertTrue($rc->implementsInterface(DriverFactoryInterface::class)); } public function testShouldBeFinal() { $rc = new \ReflectionClass(DriverFactory::class); $this->assertTrue($rc->isFinal()); } public function testCouldBeConstructedWithoutAnyArguments() { new DriverFactory(); } public function testThrowIfPackageThatSupportSchemeNotInstalled() { $scheme = 'scheme5b7aa7d7cd213'; $class = 'ConnectionClass5b7aa7d7cd213'; Resources::addDriver($class, [$scheme], [], ['thePackage', 'theOtherPackage']); $this->expectException(\LogicException::class); $this->expectExceptionMessage('To use given scheme "scheme5b7aa7d7cd213" a package has to be installed. Run "composer req thePackage theOtherPackage" to add it.'); $factory = new DriverFactory(); $factory->create($this->createConnectionFactoryMock(), $this->createDummyConfig($scheme.'://foo'), new RouteCollection([])); } public function testThrowIfSchemeIsNotKnown() { $scheme = 'scheme5b7aa862e70a5'; $this->expectException(\LogicException::class); $this->expectExceptionMessage('A given scheme "scheme5b7aa862e70a5" is not supported. Maybe it is a custom driver, make sure you registered it with "Enqueue\Client\Resources::addDriver".'); $factory = new DriverFactory(); $factory->create($this->createConnectionFactoryMock(), $this->createDummyConfig($scheme.'://foo'), new RouteCollection([])); } public function testThrowIfDsnInvalid() { $this->expectException(\LogicException::class); $this->expectExceptionMessage('The DSN is invalid. It does not have scheme separator ":".'); $factory = new DriverFactory(); $factory->create($this->createConnectionFactoryMock(), $this->createDummyConfig('invalidDsn'), new RouteCollection([])); } /** * @dataProvider provideDSN */ public function testReturnsExpectedFactories( string $dsn, string $connectionFactoryClass, string $contextClass, array $conifg, string $expectedDriverClass ) { $connectionFactoryMock = $this->createMock($connectionFactoryClass); $connectionFactoryMock ->expects($this->once()) ->method('createContext') ->willReturn($this->createMock($contextClass)) ; $driverFactory = new DriverFactory(); $driver = $driverFactory->create($connectionFactoryMock, $this->createDummyConfig($dsn), new RouteCollection([])); $this->assertInstanceOf($expectedDriverClass, $driver); } public static function provideDSN() { yield ['null:', NullConnectionFactory::class, NullContext::class, [], GenericDriver::class]; yield ['amqp:', AmqpConnectionFactory::class, AmqpContext::class, [], AmqpDriver::class]; yield ['amqp+rabbitmq:', AmqpConnectionFactory::class, AmqpContext::class, [], RabbitMqDriver::class]; yield ['mysql:', DbalConnectionFactory::class, DbalContext::class, [], DbalDriver::class]; yield ['file:', FsConnectionFactory::class, FsContext::class, [], FsDriver::class]; // https://github.com/php-enqueue/enqueue-dev/issues/511 // yield ['gearman:', GearmanConnectionFactory::class, NullContext::class, [], NullDriver::class]; yield ['gps:', GpsConnectionFactory::class, GpsContext::class, [], GpsDriver::class]; yield ['mongodb:', MongodbConnectionFactory::class, MongodbContext::class, [], MongodbDriver::class]; yield ['kafka:', RdKafkaConnectionFactory::class, RdKafkaContext::class, [], RdKafkaDriver::class]; yield ['redis:', RedisConnectionFactory::class, RedisContext::class, [], GenericDriver::class]; yield ['redis+predis:', RedisConnectionFactory::class, RedisContext::class, [], GenericDriver::class]; yield ['sqs:', SqsConnectionFactory::class, SqsContext::class, [], SqsDriver::class]; yield ['stomp:', StompConnectionFactory::class, StompContext::class, [], StompDriver::class]; yield ['stomp+rabbitmq:', StompConnectionFactory::class, StompContext::class, [], RabbitMqStompDriver::class]; yield ['stomp+foo+bar:', StompConnectionFactory::class, StompContext::class, [], StompDriver::class]; yield ['gearman:', GearmanConnectionFactory::class, GearmanContext::class, [], GenericDriver::class]; yield ['beanstalk:', PheanstalkConnectionFactory::class, PheanstalkContext::class, [], GenericDriver::class]; } private function createDummyConfig(string $dsn): Config { return Config::create( null, null, null, null, null, null, null, ['dsn' => $dsn], [] ); } private function createConnectionFactoryMock(): ConnectionFactory { return $this->createMock(ConnectionFactory::class); } private function createConfigMock(): Config { return $this->createMock(Config::class); } }