CacheMemcachedConnectorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\MemcachedConnector;
  4. use Memcached;
  5. use Mockery as m;
  6. use PHPUnit\Framework\TestCase;
  7. use stdClass;
  8. class CacheMemcachedConnectorTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. m::close();
  13. parent::tearDown();
  14. }
  15. public function testServersAreAddedCorrectly()
  16. {
  17. $memcached = $this->memcachedMockWithAddServer();
  18. $connector = $this->connectorMock();
  19. $connector->expects($this->once())
  20. ->method('createMemcachedInstance')
  21. ->willReturn($memcached);
  22. $result = $this->connect($connector);
  23. $this->assertSame($result, $memcached);
  24. }
  25. public function testServersAreAddedCorrectlyWithPersistentConnection()
  26. {
  27. $persistentConnectionId = 'persistent_connection_id';
  28. $memcached = $this->memcachedMockWithAddServer();
  29. $connector = $this->connectorMock();
  30. $connector->expects($this->once())
  31. ->method('createMemcachedInstance')
  32. ->with($persistentConnectionId)
  33. ->willReturn($memcached);
  34. $result = $this->connect($connector, $persistentConnectionId);
  35. $this->assertSame($result, $memcached);
  36. }
  37. /**
  38. * @requires extension memcached
  39. */
  40. public function testServersAreAddedCorrectlyWithValidOptions()
  41. {
  42. $validOptions = [
  43. Memcached::OPT_NO_BLOCK => true,
  44. Memcached::OPT_CONNECT_TIMEOUT => 2000,
  45. ];
  46. $memcached = $this->memcachedMockWithAddServer();
  47. $memcached->shouldReceive('setOptions')->once()->andReturn(true);
  48. $connector = $this->connectorMock();
  49. $connector->expects($this->once())
  50. ->method('createMemcachedInstance')
  51. ->willReturn($memcached);
  52. $result = $this->connect($connector, false, $validOptions);
  53. $this->assertSame($result, $memcached);
  54. }
  55. /**
  56. * @requires extension memcached
  57. */
  58. public function testServersAreAddedCorrectlyWithSaslCredentials()
  59. {
  60. $saslCredentials = ['foo', 'bar'];
  61. $memcached = $this->memcachedMockWithAddServer();
  62. $memcached->shouldReceive('setOption')->once()->with(Memcached::OPT_BINARY_PROTOCOL, true)->andReturn(true);
  63. $memcached->shouldReceive('setSaslAuthData')
  64. ->once()->with($saslCredentials[0], $saslCredentials[1])
  65. ->andReturn(true);
  66. $connector = $this->connectorMock();
  67. $connector->expects($this->once())->method('createMemcachedInstance')->willReturn($memcached);
  68. $result = $this->connect($connector, false, [], $saslCredentials);
  69. $this->assertSame($result, $memcached);
  70. }
  71. protected function memcachedMockWithAddServer($returnedVersion = [])
  72. {
  73. $memcached = m::mock(stdClass::class);
  74. $memcached->shouldReceive('addServer')->once()->with($this->getHost(), $this->getPort(), $this->getWeight());
  75. $memcached->shouldReceive('getServerList')->once()->andReturn([]);
  76. return $memcached;
  77. }
  78. protected function connectorMock()
  79. {
  80. return $this->getMockBuilder(MemcachedConnector::class)->onlyMethods(['createMemcachedInstance'])->getMock();
  81. }
  82. protected function connect(
  83. $connector,
  84. $persistentConnectionId = false,
  85. array $customOptions = [],
  86. array $saslCredentials = []
  87. ) {
  88. return $connector->connect(
  89. $this->getServers(),
  90. $persistentConnectionId,
  91. $customOptions,
  92. $saslCredentials
  93. );
  94. }
  95. protected function getServers()
  96. {
  97. return [['host' => $this->getHost(), 'port' => $this->getPort(), 'weight' => $this->getWeight()]];
  98. }
  99. protected function getHost()
  100. {
  101. return 'localhost';
  102. }
  103. protected function getPort()
  104. {
  105. return 11211;
  106. }
  107. protected function getWeight()
  108. {
  109. return 100;
  110. }
  111. }