OSDN Git Service

Добавлена возможность изменения количества записей, выводимых на странице предметов...
[invent/invent.git] / vendor / swiftmailer / swiftmailer / tests / unit / Swift / MessageTest.php
1 <?php
2
3 class Swift_MessageTest extends \PHPUnit\Framework\TestCase
4 {
5     public function testCloning()
6     {
7         $message1 = new Swift_Message('subj', 'body', 'ctype');
8         $message2 = new Swift_Message('subj', 'body', 'ctype');
9         $message1_clone = clone $message1;
10
11         $this->recursiveObjectCloningCheck($message1, $message2, $message1_clone);
12         // the test above will fail if the two messages are not identical
13         $this->addToAssertionCount(1);
14     }
15
16     public function testCloningWithSigners()
17     {
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;
25
26         $this->recursiveObjectCloningCheck($message1, $message2, $message1_clone);
27         // the test above will fail if the two messages are not identical
28         $this->addToAssertionCount(1);
29     }
30
31     public function testBodySwap()
32     {
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');
45         }
46         $final = $message1->toString();
47         if ($source != $final) {
48             $this->fail("Difference although object cloned \n [".$source."]\n[".$final."]\n");
49         }
50         $final = $message2->toString();
51         if ($final == $source) {
52             $this->fail('Two body matches although they should differ'."\n [".$source."]\n[".$final."]\n");
53         }
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');
59     }
60
61     protected function recursiveObjectCloningCheck($obj1, $obj2, $obj1_clone)
62     {
63         $obj1_properties = (array) $obj1;
64         $obj2_properties = (array) $obj2;
65         $obj1_clone_properties = (array) $obj1_clone;
66
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];
72
73                 if ($obj1_value !== $obj2_value) {
74                     // two separetely instanciated objects property not referencing same object
75                     $this->assertFalse(
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"
79                     );
80                 } else {
81                     // two separetely instanciated objects have same reference
82                     $this->assertFalse(
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)"
86                     );
87                 }
88                 // recurse
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];
94
95                 return $this->recursiveArrayCloningCheck($obj1_value, $obj2_value, $obj1_clone_value);
96             }
97         }
98     }
99
100     protected function recursiveArrayCloningCheck($array1, $array2, $array1_clone)
101     {
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
109                     $this->assertFalse(
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"
113                     );
114                 } else {
115                     // two separetely instanciated objects have same reference
116                     $this->assertFalse(
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)"
120                     );
121                 }
122                 // recurse
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];
128
129                 return $this->recursiveArrayCloningCheck($arr1_value, $arr2_value, $arr1_clone_value);
130             }
131         }
132     }
133 }