diff --git a/CHANGELOG.md b/CHANGELOG.md index c7321a2..1480c7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,13 @@ ## UNRELEASED +- Make cloning object do clone used objects as well, [#5] + +[#5]: https://github.com/eventum/rpc/pull/5 + ## [4.2.0] - 2018-05-01 -- Move classes to `Eventum\RPC` namespace, deprecate root namespace [#4] +- Move classes to `Eventum\RPC` namespace, deprecate root namespace, [#4] - Update xmlrpc methods doc, include `@method` annotation, [#4] [4.2.0]: https://github.com/eventum/rpc/compare/v4.1.1...v4.2.0 diff --git a/src/XmlRpcClient.php b/src/XmlRpcClient.php index 566965e..cf20e28 100644 --- a/src/XmlRpcClient.php +++ b/src/XmlRpcClient.php @@ -33,6 +33,12 @@ public function __construct($url) $this->addUserAgent('EventumRPC/' . self::VERSION); } + public function __clone() + { + $this->client = clone $this->client; + $this->encoder = clone $this->encoder; + } + /** * Change the current debug mode * diff --git a/tests/CloneTest.php b/tests/CloneTest.php new file mode 100644 index 0000000..dd81aa1 --- /dev/null +++ b/tests/CloneTest.php @@ -0,0 +1,82 @@ +client = $this->createClient(); + $this->cloned = clone $this->client; + $this->store = new SplObjectStorage(); + } + + public function testClone() + { + // add both clients, they should not be there + $this->assertAttachFalse($this->client); + $this->assertAttachFalse($this->cloned); + + /** @var \PhpXmlRpc\Client $client */ + $client = $this->getClient($this->client); + /** @var \PhpXmlRpc\Client $cloned */ + $cloned = $this->getClient($this->cloned); + // the real clients should be also different + $this->assertAttachFalse($client); + $this->assertAttachFalse($cloned); + + // setting credentials should not affect each other + $client->setCredentials('client', 'tneilc'); + $cloned->setCredentials('cloned', 'denolc'); + + $this->assertNotSame($client->username, $cloned->username); + $this->assertNotSame($client->password, $cloned->password); + } + + private function assertAttachFalse($object) + { + $this->assertFalse($this->store->contains($object)); + $this->store->attach($object); + } + + private function getClient(XmlRpcClient $client) + { + $reflectionClass = new ReflectionClass($client); + $reflectionProperty = $reflectionClass->getProperty('client'); + $reflectionProperty->setAccessible(true); + + return $reflectionProperty->getValue($client); + } + + /** + * @return XmlRpcClient + */ + private function createClient() + { + $url = 'http://localhost'; + + return new XmlRpcClient($url); + } +}