3 class Swift_MessageTest extends \PHPUnit\Framework\TestCase
5 public function testCloning()
7 $message1 = new Swift_Message('subj', 'body', 'ctype');
8 $message2 = new Swift_Message('subj', 'body', 'ctype');
9 $message1_clone = clone $message1;
11 $this->recursiveObjectCloningCheck($message1, $message2, $message1_clone);
12 // the test above will fail if the two messages are not identical
13 $this->addToAssertionCount(1);
16 public function testCloningWithSigners()
18 $message1 = new Swift_Message('subj', 'body', 'ctype');
19 $signer = new Swift_Signers_DKIMSigner(dirname(dirname(__DIR__)).'/_samples/dkim/dkim.test.priv', 'test.example', 'example');
20 $message1->attachSigner($signer);
21 $message2 = new Swift_Message('subj', 'body', 'ctype');
22 $signer = new Swift_Signers_DKIMSigner(dirname(dirname(__DIR__)).'/_samples/dkim/dkim.test.priv', 'test.example', 'example');
23 $message2->attachSigner($signer);
24 $message1_clone = clone $message1;
26 $this->recursiveObjectCloningCheck($message1, $message2, $message1_clone);
27 // the test above will fail if the two messages are not identical
28 $this->addToAssertionCount(1);
31 public function testBodySwap()
33 $message1 = new Swift_Message('Test');
34 $html = new Swift_MimePart('<html></html>', 'text/html');
35 $html->getHeaders()->addTextHeader('X-Test-Remove', 'Test-Value');
36 $html->getHeaders()->addTextHeader('X-Test-Alter', 'Test-Value');
37 $message1->attach($html);
38 $source = $message1->toString();
39 $message2 = clone $message1;
40 $message2->setSubject('Message2');
41 foreach ($message2->getChildren() as $child) {
42 $child->setBody('Test');
43 $child->getHeaders()->removeAll('X-Test-Remove');
44 $child->getHeaders()->get('X-Test-Alter')->setValue('Altered');
46 $final = $message1->toString();
47 if ($source != $final) {
48 $this->fail("Difference although object cloned \n [".$source."]\n[".$final."]\n");
50 $final = $message2->toString();
51 if ($final == $source) {
52 $this->fail('Two body matches although they should differ'."\n [".$source."]\n[".$final."]\n");
54 $id_1 = $message1->getId();
55 $id_2 = $message2->getId();
56 $this->assertEquals($id_1, $id_2, 'Message Ids differ');
57 $id_2 = $message2->generateId();
58 $this->assertNotEquals($id_1, $id_2, 'Message Ids are the same');
61 protected function recursiveObjectCloningCheck($obj1, $obj2, $obj1_clone)
63 $obj1_properties = (array) $obj1;
64 $obj2_properties = (array) $obj2;
65 $obj1_clone_properties = (array) $obj1_clone;
67 foreach ($obj1_properties as $property => $value) {
68 if (is_object($value)) {
69 $obj1_value = $obj1_properties[$property];
70 $obj2_value = $obj2_properties[$property];
71 $obj1_clone_value = $obj1_clone_properties[$property];
73 if ($obj1_value !== $obj2_value) {
74 // two separetely instanciated objects property not referencing same object
76 // but object's clone does - not everything copied
77 $obj1_value === $obj1_clone_value,
78 "Property `$property` cloning error: source and cloned objects property is referencing same object"
81 // two separetely instanciated objects have same reference
83 // but object's clone doesn't - overdone making copies
84 $obj1_value !== $obj1_clone_value,
85 "Property `$property` not properly cloned: it should reference same object as cloning source (overdone copping)"
89 $this->recursiveObjectCloningCheck($obj1_value, $obj2_value, $obj1_clone_value);
90 } elseif (is_array($value)) {
91 $obj1_value = $obj1_properties[$property];
92 $obj2_value = $obj2_properties[$property];
93 $obj1_clone_value = $obj1_clone_properties[$property];
95 return $this->recursiveArrayCloningCheck($obj1_value, $obj2_value, $obj1_clone_value);
100 protected function recursiveArrayCloningCheck($array1, $array2, $array1_clone)
102 foreach ($array1 as $key => $value) {
103 if (is_object($value)) {
104 $arr1_value = $array1[$key];
105 $arr2_value = $array2[$key];
106 $arr1_clone_value = $array1_clone[$key];
107 if ($arr1_value !== $arr2_value) {
108 // two separetely instanciated objects property not referencing same object
110 // but object's clone does - not everything copied
111 $arr1_value === $arr1_clone_value,
112 "Key `$key` cloning error: source and cloned objects property is referencing same object"
115 // two separetely instanciated objects have same reference
117 // but object's clone doesn't - overdone making copies
118 $arr1_value !== $arr1_clone_value,
119 "Key `$key` not properly cloned: it should reference same object as cloning source (overdone copping)"
123 $this->recursiveObjectCloningCheck($arr1_value, $arr2_value, $arr1_clone_value);
124 } elseif (is_array($value)) {
125 $arr1_value = $array1[$key];
126 $arr2_value = $array2[$key];
127 $arr1_clone_value = $array1_clone[$key];
129 return $this->recursiveArrayCloningCheck($arr1_value, $arr2_value, $arr1_clone_value);