Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
Clone signers
Browse files Browse the repository at this point in the history
  • Loading branch information
xdecock authored and fabpot committed Sep 20, 2014
1 parent c4e30e1 commit cd9b8b2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/classes/Swift/Message.php
Expand Up @@ -268,4 +268,20 @@ protected function restoreMessage()
$this->restoreHeaders();
$this->savedMessage = array();
}

/**
* Clone Message Signers
* @see Swift_Mime_SimpleMimeEntity::__clone()
*/
public function __clone()
{
parent::__clone();
foreach ($this->bodySigners as $key => $bodySigner) {
$this->bodySigners[$key] = clone($bodySigner);
}

foreach ($this->headerSigners as $key => $headerSigner) {
$this->headerSigners[$key] = clone($headerSigner);
}
}
}
53 changes: 53 additions & 0 deletions tests/unit/Swift/MessageTest.php
Expand Up @@ -11,6 +11,19 @@ public function testCloning()
$this->_recursiveObjectCloningCheck($message1, $message2, $message1_clone);
}

public function testCloningWithSigners()
{
$message1 = new Swift_Message('subj', 'body', 'ctype');
$signer = new Swift_Signers_DKIMSigner(dirname(dirname(__DIR__)).'/_samples/dkim/dkim.test.priv', 'test.example', 'example');
$message1->attachSigner($signer);
$message2 = new Swift_Message('subj', 'body', 'ctype');
$signer = new Swift_Signers_DKIMSigner(dirname(dirname(__DIR__)).'/_samples/dkim/dkim.test.priv', 'test.example', 'example');
$message2->attachSigner($signer);
$message1_clone = clone $message1;

$this->_recursiveObjectCloningCheck($message1, $message2, $message1_clone);
}

public function testBodySwap()
{
$message1 = new Swift_Message('Test');
Expand Down Expand Up @@ -69,6 +82,46 @@ protected function _recursiveObjectCloningCheck($obj1, $obj2, $obj1_clone)
}
// recurse
$this->_recursiveObjectCloningCheck($obj1_value, $obj2_value, $obj1_clone_value);
} elseif (is_array($value)) {
$obj1_value = $obj1_properties[$property];
$obj2_value = $obj2_properties[$property];
$obj1_clone_value = $obj1_clone_properties[$property];

return $this->_recursiveArrayCloningCheck($obj1_value, $obj2_value, $obj1_clone_value);
}
}
}

protected function _recursiveArrayCloningCheck($array1, $array2, $array1_clone)
{
foreach ($array1 as $key => $value) {
if (is_object($value)) {
$arr1_value = $array1[$key];
$arr2_value = $array2[$key];
$arr1_clone_value = $array1_clone[$key];
if ($arr1_value !== $arr2_value) {
// two separetely instanciated objects property not referencing same object
$this->assertFalse(
// but object's clone does - not everything copied
$arr1_value === $arr1_clone_value,
"Key `$key` cloning error: source and cloned objects property is referencing same object"
);
} else {
// two separetely instanciated objects have same reference
$this->assertFalse(
// but object's clone doesn't - overdone making copies
$arr1_value !== $arr1_clone_value,
"Key `$key` not properly cloned: it should reference same object as cloning source (overdone copping)"
);
}
// recurse
$this->_recursiveObjectCloningCheck($arr1_value, $arr2_value, $arr1_clone_value);
} elseif (is_array($value)) {
$arr1_value = $array1[$key];
$arr2_value = $array2[$key];
$arr1_clone_value = $array1_clone[$key];

return $this->_recursiveArrayCloningCheck($obj1_value, $obj2_value, $obj1_clone_value);
}
}
}
Expand Down

0 comments on commit cd9b8b2

Please sign in to comment.