소스 검색

Make the "profile" option accept a callable object as initializer.

This can be useful to set up the profile with additional commands,
e.g. when injecting new "virtual" commands based upon the EVAL and
the Predis\Commands\ScriptedCommand class.
Daniele Alessandri 13 년 전
부모
커밋
9bc5685959
3개의 변경된 파일44개의 추가작업 그리고 0개의 파일을 삭제
  1. 3 0
      CHANGELOG.md
  2. 4 0
      lib/Predis/Options/ClientProfile.php
  3. 37 0
      tests/Predis/Options/ClientProfileTest.php

+ 3 - 0
CHANGELOG.md

@@ -3,6 +3,9 @@ v0.7.1 (201x-xx-xx)
 
 - Miscellaneous minor fixes.
 
+- The 'profile' client option now accepts a callable object used to initialize
+  a new instance of Predis\Profiles\IServerProfile.
+
 - PearHub's PEAR channel has been deprecated in favour of `pear.nrk.io`.
 
 

+ 4 - 0
lib/Predis/Options/ClientProfile.php

@@ -33,6 +33,10 @@ class ClientProfile extends Option
             }
         }
 
+        if (is_callable($value)) {
+            $value = call_user_func($value, $options);
+        }
+
         if (!$value instanceof IServerProfile) {
             throw new \InvalidArgumentException('Invalid value for the profile option');
         }

+ 37 - 0
tests/Predis/Options/ClientProfileTest.php

@@ -52,6 +52,28 @@ class ClientProfileTest extends StandardTestCase
         $this->assertNull($profile->getProcessor());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testValidationAcceptsCallableObjectAsInitializers()
+    {
+        $value = $this->getMock('Predis\Profiles\IServerProfile');
+
+        $initializer = $this->getMock('stdClass', array('__invoke'));
+        $initializer->expects($this->once())
+                    ->method('__invoke')
+                    ->with($this->isInstanceOf('Predis\Options\IClientOptions'))
+                    ->will($this->returnValue($value));
+
+        $options = $this->getMock('Predis\Options\IClientOptions');
+        $option = new ClientProfile();
+
+        $profile = $option->filter($options, $initializer);
+
+        $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
+        $this->assertSame($value, $profile);
+    }
+
     /**
      * @group disconnected
      */
@@ -169,4 +191,19 @@ class ClientProfileTest extends StandardTestCase
         $this->assertInstanceOf('Predis\Profiles\IServerProfile', $profile);
         $this->assertNull($profile->getProcessor());
     }
+
+    /**
+     * @group disconnected
+     * @expectedException InvalidArgumentException
+     * @expectedExceptionMessage Invalid value for the profile option
+     */
+    public function testValidationThrowsExceptionOnInvalidObjectReturnedByCallback()
+    {
+        $value = function($options) { return new \stdClass(); };
+
+        $options = $this->getMock('Predis\Options\IClientOptions');
+        $option = new ClientProfile();
+
+        $option->filter($options, $value);
+    }
 }