PageRenderTime 47ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Factory/PhpredisClientFactoryTest.php

http://github.com/snc/SncRedisBundle
PHP | 184 lines | 152 code | 28 blank | 4 comment | 2 complexity | cb365575d4547ef7ab2663b3c8f5a3aa MD5 | raw file
  1. <?php
  2. namespace Snc\RedisBundle\Tests\Factory;
  3. use PHPUnit\Framework\TestCase;
  4. use Psr\Log\LoggerInterface;
  5. use Snc\RedisBundle\Factory\PhpredisClientFactory;
  6. use Snc\RedisBundle\Logger\RedisLogger;
  7. use Snc\RedisBundle\Client\Phpredis\Client;
  8. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  9. class PhpredisClientFactoryTest extends TestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. if (!class_exists(\Redis::class)) {
  14. $this->markTestSkipped(sprintf('The %s requires phpredis extension.', __CLASS__));
  15. } elseif (!@fsockopen('127.0.0.1', 6379)) {
  16. $this->markTestSkipped(sprintf('The %s requires a redis instance listening on 127.0.0.1:6379.', __CLASS__));
  17. }
  18. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  19. $this->redisLogger = new RedisLogger($this->logger);
  20. }
  21. public function testCreateMinimalConfig()
  22. {
  23. $factory = new PhpredisClientFactory();
  24. $client = $factory->create(\Redis::class, ['redis://localhost:6379'], array(), 'default');
  25. $this->assertInstanceOf(\Redis::class, $client);
  26. $this->assertNull($client->getOption(\Redis::OPT_PREFIX));
  27. $this->assertSame(0, $client->getOption(\Redis::OPT_SERIALIZER));
  28. $this->assertSame(0, $client->getDBNum());
  29. $this->assertNull($client->getAuth());
  30. $this->assertNull($client->getPersistentID());
  31. }
  32. public function testCreateMinimalClusterConfig()
  33. {
  34. $factory = new PhpredisClientFactory();
  35. $client = $factory->create(\RedisCluster::class, ['redis://localhost:7000'], [], 'phprediscluster');
  36. $this->assertInstanceOf(\RedisCluster::class, $client);
  37. $this->assertNull($client->getOption(\RedisCluster::OPT_PREFIX));
  38. $this->assertSame(0, $client->getOption(\RedisCluster::OPT_SERIALIZER));
  39. $this->assertSame(0., $client->getOption(\RedisCluster::OPT_READ_TIMEOUT));
  40. $this->assertSame(0, $client->getOption(\RedisCluster::OPT_SCAN));
  41. $this->assertSame(0, $client->getOption(\RedisCluster::OPT_SLAVE_FAILOVER));
  42. }
  43. public function testCreateFullConfig()
  44. {
  45. $logger = $this->getMockBuilder(RedisLogger::class)->getMock();
  46. $factory = new PhpredisClientFactory($logger);
  47. $client = $factory->create(
  48. Client::class,
  49. ['redis://localhost:6379'],
  50. array(
  51. 'connection_timeout' => 10,
  52. 'connection_persistent' => 'x',
  53. 'prefix' => 'toto',
  54. 'serialization' => 'php',
  55. 'read_write_timeout' => 4,
  56. 'parameters' => [
  57. 'database' => 3,
  58. 'password' => 'sncredis',
  59. ],
  60. ),
  61. 'alias_test'
  62. );
  63. $this->assertInstanceOf(Client::class, $client);
  64. $this->assertSame('toto', $client->getOption(\Redis::OPT_PREFIX));
  65. $this->assertSame(1, $client->getOption(\Redis::OPT_SERIALIZER));
  66. $this->assertSame(4., $client->getOption(\Redis::OPT_READ_TIMEOUT));
  67. $this->assertSame(3, $client->getDBNum());
  68. $this->assertSame('sncredis', $client->getAuth());
  69. $this->assertNotNull($client->getPersistentID());
  70. $this->assertNotFalse($client->getPersistentID());
  71. $refObject = new \ReflectionObject($client);
  72. $refProperty = $refObject->getProperty('logger');
  73. $refProperty->setAccessible(true);
  74. $this->assertSame($logger, $refProperty->getValue($client));
  75. }
  76. public function testDsnConfig()
  77. {
  78. $factory = new PhpredisClientFactory();
  79. $client = $factory->create(
  80. \Redis::class,
  81. ['redis://redis:sncredis@localhost:6379/2'],
  82. array(
  83. 'parameters' => [
  84. 'database' => 3,
  85. 'password' => 'secret',
  86. ],
  87. ),
  88. 'alias_test'
  89. );
  90. $this->assertInstanceOf(\Redis::class, $client);
  91. $this->assertSame(2, $client->getDBNum());
  92. $this->assertSame('sncredis', $client->getAuth());
  93. $this->assertNull($client->getPersistentID());
  94. }
  95. public function testNestedDsnConfig()
  96. {
  97. $factory = new PhpredisClientFactory();
  98. $client = $factory->create(
  99. \Redis::class,
  100. [['redis://redis:sncredis@localhost:6379/2']],
  101. array(
  102. 'parameters' => [
  103. 'database' => 3,
  104. 'password' => 'secret',
  105. ],
  106. ),
  107. 'alias_test'
  108. );
  109. $this->assertInstanceOf(\Redis::class, $client);
  110. $this->assertSame(2, $client->getDBNum());
  111. $this->assertSame('sncredis', $client->getAuth());
  112. $this->assertNull($client->getPersistentID());
  113. }
  114. /**
  115. * @dataProvider serializationTypes
  116. */
  117. public function testLoadSerializationType(string $serializationType, int $serializer): void
  118. {
  119. $factory = new PhpredisClientFactory();
  120. $client = $factory->create(
  121. \Redis::class,
  122. ['redis://localhost:6379'],
  123. [
  124. 'serialization' => $serializationType
  125. ],
  126. 'default'
  127. );
  128. self::assertSame($serializer, $client->getOption(\Redis::OPT_SERIALIZER));
  129. }
  130. public function testLoadSerializationTypeFail(): void
  131. {
  132. $factory = new PhpredisClientFactory();
  133. $this->expectException(InvalidConfigurationException::class);
  134. $factory->create(
  135. \Redis::class,
  136. ['redis://localhost:6379'],
  137. [
  138. 'serialization' => 'unknown'
  139. ],
  140. 'default'
  141. );
  142. }
  143. public function serializationTypes(): array
  144. {
  145. $serializationTypes = [
  146. ['default', \Redis::SERIALIZER_NONE],
  147. ['none', \Redis::SERIALIZER_NONE],
  148. ['php', \Redis::SERIALIZER_PHP],
  149. ];
  150. // \Redis::SERIALIZER_JSON is only available since phpredis 5
  151. if (defined('Redis::SERIALIZER_JSON')) {
  152. $serializationTypes[] = ['json', \Redis::SERIALIZER_JSON];
  153. }
  154. return $serializationTypes;
  155. }
  156. }