ProfileOptionTest.php 5.0 KB

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