ClientProfileTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Option;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Command\Processor\KeyPrefixProcessor;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class ClientProfileTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testValidationReturnsServerProfileWithStringValue()
  23. {
  24. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  25. $option = new ClientProfile();
  26. $profile = $option->filter($options, '2.0');
  27. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  28. $this->assertEquals('2.0', $profile->getVersion());
  29. $this->assertNull($profile->getProcessor());
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testValidationAcceptsProfileInstancesAsValue()
  35. {
  36. $value = ServerProfile::get('2.0');
  37. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  38. $option = new ClientProfile();
  39. $profile = $option->filter($options, $value);
  40. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  41. $this->assertEquals('2.0', $profile->getVersion());
  42. $this->assertNull($profile->getProcessor());
  43. }
  44. /**
  45. * @group disconnected
  46. */
  47. public function testValidationAcceptsCallableObjectAsInitializers()
  48. {
  49. $value = $this->getMock('Predis\Profile\ServerProfileInterface');
  50. $initializer = $this->getMock('stdClass', array('__invoke'));
  51. $initializer->expects($this->once())
  52. ->method('__invoke')
  53. ->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'))
  54. ->will($this->returnValue($value));
  55. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  56. $option = new ClientProfile();
  57. $profile = $option->filter($options, $initializer);
  58. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  59. $this->assertSame($value, $profile);
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testValidationThrowsExceptionOnWrongInvalidArguments()
  65. {
  66. $this->setExpectedException('InvalidArgumentException');
  67. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  68. $option = new ClientProfile();
  69. $option->filter($options, new \stdClass());
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testDefaultReturnsDefaultServerProfile()
  75. {
  76. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  77. $option = new ClientProfile();
  78. $profile = $option->getDefault($options);
  79. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  80. $this->assertInstanceOf(get_class(ServerProfile::getDefault()), $profile);
  81. $this->assertNull($profile->getProcessor());
  82. }
  83. /**
  84. * @group disconnected
  85. */
  86. public function testInvokeReturnsSpecifiedServerProfileOrDefault()
  87. {
  88. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  89. $option = new ClientProfile();
  90. $profile = $option($options, '2.0');
  91. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  92. $this->assertEquals('2.0', $profile->getVersion());
  93. $this->assertNull($profile->getProcessor());
  94. $profile = $option($options, null);
  95. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  96. $this->assertInstanceOf(get_class(ServerProfile::getDefault()), $profile);
  97. $this->assertNull($profile->getProcessor());
  98. }
  99. /**
  100. * @group disconnected
  101. * @todo Can't we when trap __isset when mocking an interface? Doesn't seem to work here.
  102. */
  103. public function testFilterSetsPrefixProcessorFromClientOptions()
  104. {
  105. $options = $this->getMock('Predis\Option\ClientOptions', array('__isset', '__get'));
  106. $options->expects($this->once())
  107. ->method('__isset')
  108. ->with('prefix')
  109. ->will($this->returnValue(true));
  110. $options->expects($this->once())
  111. ->method('__get')
  112. ->with('prefix')
  113. ->will($this->returnValue(new KeyPrefixProcessor('prefix:')));
  114. $option = new ClientProfile();
  115. $profile = $option->filter($options, '2.0');
  116. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  117. $this->assertEquals('2.0', $profile->getVersion());
  118. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  119. $this->assertEquals('prefix:', $profile->getProcessor()->getPrefix());
  120. }
  121. /**
  122. * @group disconnected
  123. * @todo Can't we when trap __isset when mocking an interface? Doesn't seem to work here.
  124. */
  125. public function testDefaultSetsPrefixProcessorFromClientOptions()
  126. {
  127. $options = $this->getMock('Predis\Option\ClientOptions', array('__isset', '__get'));
  128. $options->expects($this->once())
  129. ->method('__isset')
  130. ->with('prefix')
  131. ->will($this->returnValue(true));
  132. $options->expects($this->once())
  133. ->method('__get')
  134. ->with('prefix')
  135. ->will($this->returnValue(new KeyPrefixProcessor('prefix:')));
  136. $option = new ClientProfile();
  137. $profile = $option->getDefault($options);
  138. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  139. $this->assertInstanceOf(get_class(ServerProfile::getDefault()), $profile);
  140. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  141. $this->assertEquals('prefix:', $profile->getProcessor()->getPrefix());
  142. }
  143. /**
  144. * @group disconnected
  145. */
  146. public function testValidationDoesNotSetPrefixProcessorWhenValueIsProfileInstance()
  147. {
  148. $options = $this->getMock('Predis\Option\ClientOptions', array('__isset', '__get'));
  149. $options->expects($this->never())->method('__isset');
  150. $options->expects($this->never())->method('__get');
  151. $option = new ClientProfile();
  152. $profile = $option->filter($options, ServerProfile::getDefault());
  153. $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
  154. $this->assertNull($profile->getProcessor());
  155. }
  156. /**
  157. * @group disconnected
  158. * @expectedException InvalidArgumentException
  159. * @expectedExceptionMessage Invalid value for the profile option
  160. */
  161. public function testValidationThrowsExceptionOnInvalidObjectReturnedByCallback()
  162. {
  163. $value = function ($options) {
  164. return new \stdClass();
  165. };
  166. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  167. $option = new ClientProfile();
  168. $option->filter($options, $value);
  169. }
  170. }