ProfileOptionTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Configuration;
  11. use InvalidArgumentException;
  12. use stdClass;
  13. use PredisTestCase;
  14. use Predis\Command\Processor\KeyPrefixProcessor;
  15. use Predis\Profile;
  16. /**
  17. *
  18. */
  19. class ProfileOptionTest extends PredisTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. */
  24. public function testDefaultOptionValue()
  25. {
  26. $option = new ProfileOption();
  27. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  28. $profile = $option->getDefault($options);
  29. $this->assertInstanceOf('Predis\Profile\ProfileInterface', $profile);
  30. $this->assertInstanceOf(get_class(Profile\Factory::getDefault()), $profile);
  31. $this->assertNull($profile->getProcessor());
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testAcceptsProfileInstanceAsValue()
  37. {
  38. $option = new ProfileOption();
  39. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  40. $value = Profile\Factory::get('2.0');
  41. $profile = $option->filter($options, $value);
  42. $this->assertSame($profile, $value);
  43. $this->assertNull($profile->getProcessor());
  44. }
  45. /**
  46. * @group disconnected
  47. */
  48. public function testAcceptsStringInterpretedAsProfileVersion()
  49. {
  50. $option = new ProfileOption();
  51. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  52. $profile = $option->filter($options, '2.0');
  53. $this->assertInstanceOf('Predis\Profile\ProfileInterface', $profile);
  54. $this->assertEquals('2.0', $profile->getVersion());
  55. $this->assertNull($profile->getProcessor());
  56. }
  57. /**
  58. * @group disconnected
  59. */
  60. public function testAppliesPrefixOnDefaultOptionValue()
  61. {
  62. $option = new ProfileOption();
  63. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  64. $options->expects($this->once())
  65. ->method('__isset')
  66. ->with('prefix')
  67. ->will($this->returnValue(true));
  68. $options->expects($this->once())
  69. ->method('__get')
  70. ->with('prefix')
  71. ->will($this->returnValue(new KeyPrefixProcessor('prefix:')));
  72. $profile = $option->getDefault($options);
  73. $this->assertInstanceOf('Predis\Profile\ProfileInterface', $profile);
  74. $this->assertInstanceOf(get_class(Profile\Factory::getDefault()), $profile);
  75. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  76. $this->assertSame('prefix:', $profile->getProcessor()->getPrefix());
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testAppliesPrefixOnProfileCreatedFromStringValue()
  82. {
  83. $option = new ProfileOption();
  84. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  85. $options->expects($this->once())
  86. ->method('__isset')
  87. ->with('prefix')
  88. ->will($this->returnValue(true));
  89. $options->expects($this->once())
  90. ->method('__get')
  91. ->with('prefix')
  92. ->will($this->returnValue(new KeyPrefixProcessor('prefix:')));
  93. $profile = $option->filter($options, '2.0');
  94. $this->assertInstanceOf('Predis\Profile\ProfileInterface', $profile);
  95. $this->assertInstanceOf(get_class(Profile\Factory::get('2.0')), $profile);
  96. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $profile->getProcessor());
  97. $this->assertSame('prefix:', $profile->getProcessor()->getPrefix());
  98. }
  99. /**
  100. * @group disconnected
  101. */
  102. public function testDoesNotApplyPrefixOnProfileValue()
  103. {
  104. $option = new ProfileOption();
  105. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  106. $value = Profile\Factory::getDefault();
  107. $options->expects($this->never())->method('__isset');
  108. $options->expects($this->never())->method('__get');
  109. $profile = $option->filter($options, $value);
  110. $this->assertInstanceOf('Predis\Profile\ProfileInterface', $profile);
  111. $this->assertInstanceOf(get_class(Profile\Factory::getDefault()), $profile);
  112. $this->assertNull($profile->getProcessor());
  113. }
  114. /**
  115. * @group disconnected
  116. * @expectedException InvalidArgumentException
  117. */
  118. public function testThrowsExceptionOnInvalidValue()
  119. {
  120. $option = new ProfileOption();
  121. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  122. $option->filter($options, new stdClass);
  123. }
  124. /**
  125. * @group disconnected
  126. * @expectedException \Predis\ClientException
  127. */
  128. public function testThrowsExceptionOnUnrecognizedVersionString()
  129. {
  130. $option = new ProfileOption();
  131. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  132. $option->filter($options, '0.0');
  133. }
  134. }