ClientProfileTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Options;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Profiles\ServerProfile;
  13. use Predis\Commands\Processors\KeyPrefixProcessor;
  14. /**
  15. *
  16. */
  17. class ClientProfileTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testValidationReturnsServerProfileWithStringValue()
  23. {
  24. $options = $this->getMock('Predis\Options\IClientOptions');
  25. $option = new ClientProfile();
  26. $profile = $option->filter($options, '2.0');
  27. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $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\Options\IClientOptions');
  38. $option = new ClientProfile();
  39. $profile = $option->filter($options, $value);
  40. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
  41. $this->assertEquals('2.0', $profile->getVersion());
  42. $this->assertNull($profile->getProcessor());
  43. }
  44. /**
  45. * @group disconnected
  46. */
  47. public function testValidationThrowsExceptionOnWrongInvalidArguments()
  48. {
  49. $this->setExpectedException('InvalidArgumentException');
  50. $options = $this->getMock('Predis\Options\IClientOptions');
  51. $option = new ClientProfile();
  52. $option->filter($options, new \stdClass());
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testDefaultReturnsDefaultServerProfile()
  58. {
  59. $options = $this->getMock('Predis\Options\IClientOptions');
  60. $option = new ClientProfile();
  61. $profile = $option->getDefault($options);
  62. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
  63. $this->assertInstanceOf(get_class(ServerProfile::getDefault()), $profile);
  64. $this->assertNull($profile->getProcessor());
  65. }
  66. /**
  67. * @group disconnected
  68. */
  69. public function testInvokeReturnsSpecifiedServerProfileOrDefault()
  70. {
  71. $options = $this->getMock('Predis\Options\IClientOptions');
  72. $option = new ClientProfile();
  73. $profile = $option($options, '2.0');
  74. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
  75. $this->assertEquals('2.0', $profile->getVersion());
  76. $this->assertNull($profile->getProcessor());
  77. $profile = $option($options, null);
  78. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
  79. $this->assertInstanceOf(get_class(ServerProfile::getDefault()), $profile);
  80. $this->assertNull($profile->getProcessor());
  81. }
  82. /**
  83. * @group disconnected
  84. * @todo Can't we when trap __isset when mocking an interface? Doesn't seem to work here.
  85. */
  86. public function testFilterSetsPrefixProcessorFromClientOptions()
  87. {
  88. $options = $this->getMock('Predis\Options\ClientOptions', array('__isset', '__get'));
  89. $options->expects($this->once())
  90. ->method('__isset')
  91. ->with('prefix')
  92. ->will($this->returnValue(true));
  93. $options->expects($this->once())
  94. ->method('__get')
  95. ->with('prefix')
  96. ->will($this->returnValue(new KeyPrefixProcessor('prefix:')));
  97. $option = new ClientProfile();
  98. $profile = $option->filter($options, '2.0');
  99. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
  100. $this->assertEquals('2.0', $profile->getVersion());
  101. $this->assertInstanceOf('Predis\Commands\Processors\KeyPrefixProcessor', $profile->getProcessor());
  102. $this->assertEquals('prefix:', $profile->getProcessor()->getPrefix());
  103. }
  104. /**
  105. * @group disconnected
  106. * @todo Can't we when trap __isset when mocking an interface? Doesn't seem to work here.
  107. */
  108. public function testDefaultSetsPrefixProcessorFromClientOptions()
  109. {
  110. $options = $this->getMock('Predis\Options\ClientOptions', array('__isset', '__get'));
  111. $options->expects($this->once())
  112. ->method('__isset')
  113. ->with('prefix')
  114. ->will($this->returnValue(true));
  115. $options->expects($this->once())
  116. ->method('__get')
  117. ->with('prefix')
  118. ->will($this->returnValue(new KeyPrefixProcessor('prefix:')));
  119. $option = new ClientProfile();
  120. $profile = $option->getDefault($options);
  121. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
  122. $this->assertInstanceOf(get_class(ServerProfile::getDefault()), $profile);
  123. $this->assertInstanceOf('Predis\Commands\Processors\KeyPrefixProcessor', $profile->getProcessor());
  124. $this->assertEquals('prefix:', $profile->getProcessor()->getPrefix());
  125. }
  126. /**
  127. * @group disconnected
  128. */
  129. public function testValidationDoesNotSetPrefixProcessorWhenValueIsProfileInstance()
  130. {
  131. $options = $this->getMock('Predis\Options\ClientOptions', array('__isset', '__get'));
  132. $options->expects($this->never())->method('__isset');
  133. $options->expects($this->never())->method('__get');
  134. $option = new ClientProfile();
  135. $profile = $option->filter($options, ServerProfile::getDefault());
  136. $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
  137. $this->assertNull($profile->getProcessor());
  138. }
  139. }