Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make cloning object do clone used objects as well #5

Merged
merged 1 commit into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/XmlRpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
82 changes: 82 additions & 0 deletions tests/CloneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the Eventum (Issue Tracking System) package.
*
* @copyright (c) Eventum Team
* @license GNU General Public License, version 2 or later (GPL-2+)
*
* For the full copyright and license information,
* please see the COPYING and AUTHORS files
* that were distributed with this source code.
*/

namespace Eventum\RPC\Test;

use Eventum\RPC\XmlRpcClient;
use ReflectionClass;
use SplObjectStorage;

class CloneTest extends TestCase
{
/** @var XmlRpcClient */
private $client;
/** @var XmlRpcClient */
private $cloned;
/** @var SplObjectStorage */
private $store;

public function setUp()
{
$this->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);
}
}