|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache\Tests\Traits; |
| 13 | + |
| 14 | +use PHPUnit\Framework\SkippedTestSuiteError; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Cache\Traits\RedisTrait; |
| 17 | + |
| 18 | +class RedisTraitTest extends TestCase |
| 19 | +{ |
| 20 | + public static function setUpBeforeClass(): void |
| 21 | + { |
| 22 | + if (!getenv('REDIS_CLUSTER_HOSTS')) { |
| 23 | + throw new SkippedTestSuiteError('REDIS_CLUSTER_HOSTS env var is not defined.'); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @dataProvider provideCreateConnection |
| 29 | + */ |
| 30 | + public function testCreateConnection(string $dsn, string $expectedClass) |
| 31 | + { |
| 32 | + if (!class_exists($expectedClass)) { |
| 33 | + throw new SkippedTestSuiteError(sprintf('The "%s" class is required.', $expectedClass)); |
| 34 | + } |
| 35 | + if (!getenv('REDIS_CLUSTER_HOSTS')) { |
| 36 | + throw new SkippedTestSuiteError('REDIS_CLUSTER_HOSTS env var is not defined.'); |
| 37 | + } |
| 38 | + |
| 39 | + $mock = self::getObjectForTrait(RedisTrait::class); |
| 40 | + $connection = $mock::createConnection($dsn); |
| 41 | + |
| 42 | + self::assertInstanceOf($expectedClass, $connection); |
| 43 | + } |
| 44 | + |
| 45 | + public static function provideCreateConnection(): array |
| 46 | + { |
| 47 | + $hosts = array_map(function ($host) { return sprintf('host[%s]', $host); }, explode(' ', getenv('REDIS_CLUSTER_HOSTS'))); |
| 48 | + |
| 49 | + return [ |
| 50 | + [ |
| 51 | + sprintf('redis:?%s&redis_cluster=1', $hosts[0]), |
| 52 | + 'RedisCluster', |
| 53 | + ], |
| 54 | + [ |
| 55 | + sprintf('redis:?%s&redis_cluster=true', $hosts[0]), |
| 56 | + 'RedisCluster', |
| 57 | + ], |
| 58 | + [ |
| 59 | + sprintf('redis:?%s', $hosts[0]), |
| 60 | + 'Redis', |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'dsn' => sprintf('redis:?%s', implode('&', \array_slice($hosts, 0, 2))), |
| 64 | + 'RedisArray', |
| 65 | + ], |
| 66 | + ]; |
| 67 | + } |
| 68 | +} |
0 commit comments