DeepCopyTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. namespace DeepCopyTest;
  3. use DateInterval;
  4. use DateTime;
  5. use DateTimeImmutable;
  6. use DateTimeZone;
  7. use DeepCopy\DeepCopy;
  8. use DeepCopy\Exception\CloneException;
  9. use DeepCopy\f001;
  10. use DeepCopy\f002;
  11. use DeepCopy\f003;
  12. use DeepCopy\f004;
  13. use DeepCopy\f005;
  14. use DeepCopy\f006;
  15. use DeepCopy\f007;
  16. use DeepCopy\f008;
  17. use DeepCopy\Filter\KeepFilter;
  18. use DeepCopy\Filter\SetNullFilter;
  19. use DeepCopy\Matcher\PropertyNameMatcher;
  20. use DeepCopy\Matcher\PropertyTypeMatcher;
  21. use DeepCopy\TypeFilter\ShallowCopyFilter;
  22. use DeepCopy\TypeMatcher\TypeMatcher;
  23. use PHPUnit\Framework\TestCase;
  24. use SplDoublyLinkedList;
  25. use stdClass;
  26. use function DeepCopy\deep_copy;
  27. /**
  28. * @covers \DeepCopy\DeepCopy
  29. */
  30. class DeepCopyTest extends TestCase
  31. {
  32. /**
  33. * @dataProvider provideScalarValues
  34. */
  35. public function test_it_can_copy_scalar_values($value)
  36. {
  37. $copy = deep_copy($value);
  38. $this->assertSame($value, $copy);
  39. }
  40. public function provideScalarValues()
  41. {
  42. return [
  43. [true],
  44. ['string'],
  45. [null],
  46. [10],
  47. [-1],
  48. [.5],
  49. ];
  50. }
  51. public function test_it_can_copy_an_array_of_scalar_values()
  52. {
  53. $copy = deep_copy([10, 20]);
  54. $this->assertSame([10, 20], $copy);
  55. }
  56. public function test_it_can_copy_an_object()
  57. {
  58. $object = new stdClass();
  59. $copy = deep_copy($object);
  60. $this->assertEqualButNotSame($object, $copy);
  61. }
  62. public function test_it_can_copy_an_array_of_objects()
  63. {
  64. $object = [new stdClass()];
  65. $copy = deep_copy($object);
  66. $this->assertEqualButNotSame($object, $copy);
  67. $this->assertEqualButNotSame($object[0], $copy[0]);
  68. }
  69. /**
  70. * @dataProvider provideObjectWithScalarValues
  71. */
  72. public function test_it_can_copy_an_object_with_scalar_properties($object, $expectedVal)
  73. {
  74. $copy = deep_copy($object);
  75. $this->assertEqualButNotSame($object, $copy);
  76. $this->assertSame($expectedVal, $copy->prop);
  77. }
  78. public function provideObjectWithScalarValues()
  79. {
  80. $createObject = function ($val) {
  81. $object = new stdClass();
  82. $object->prop = $val;
  83. return $object;
  84. };
  85. return array_map(
  86. function (array $vals) use ($createObject) {
  87. return [$createObject($vals[0]), $vals[0]];
  88. },
  89. $this->provideScalarValues()
  90. );
  91. }
  92. public function test_it_can_copy_an_object_with_an_object_property()
  93. {
  94. $foo = new stdClass();
  95. $bar = new stdClass();
  96. $foo->bar = $bar;
  97. $copy = deep_copy($foo);
  98. $this->assertEqualButNotSame($foo, $copy);
  99. $this->assertEqualButNotSame($foo->bar, $copy->bar);
  100. }
  101. public function test_it_copies_dynamic_properties()
  102. {
  103. $foo = new stdClass();
  104. $bar = new stdClass();
  105. $foo->bar = $bar;
  106. $copy = deep_copy($foo);
  107. $this->assertEqualButNotSame($foo, $copy);
  108. $this->assertEqualButNotSame($foo->bar, $copy->bar);
  109. }
  110. /**
  111. * @ticket https://github.com/myclabs/DeepCopy/issues/38
  112. * @ticket https://github.com/myclabs/DeepCopy/pull/70
  113. * @ticket https://github.com/myclabs/DeepCopy/pull/76
  114. */
  115. public function test_it_can_copy_an_object_with_a_date_object_property()
  116. {
  117. $object = new stdClass();
  118. $object->d1 = new DateTime();
  119. $object->d2 = new DateTimeImmutable();
  120. $object->dtz = new DateTimeZone('UTC');
  121. $object->di = new DateInterval('P2D');
  122. $copy = deep_copy($object);
  123. $this->assertEqualButNotSame($object->d1, $copy->d1);
  124. $this->assertEqualButNotSame($object->d2, $copy->d2);
  125. $this->assertEqualButNotSame($object->dtz, $copy->dtz);
  126. $this->assertEqualButNotSame($object->di, $copy->di);
  127. }
  128. /**
  129. * @ticket https://github.com/myclabs/DeepCopy/pull/70
  130. */
  131. public function test_it_skips_the_copy_for_userland_datetimezone()
  132. {
  133. $deepCopy = new DeepCopy();
  134. $deepCopy->addFilter(
  135. new SetNullFilter(),
  136. new PropertyNameMatcher('cloned')
  137. );
  138. $object = new stdClass();
  139. $object->dtz = new f007\FooDateTimeZone('UTC');
  140. $copy = $deepCopy->copy($object);
  141. $this->assertTrue($copy->dtz->cloned);
  142. }
  143. /**
  144. * @ticket https://github.com/myclabs/DeepCopy/pull/76
  145. */
  146. public function test_it_skips_the_copy_for_userland_dateinterval()
  147. {
  148. $deepCopy = new DeepCopy();
  149. $deepCopy->addFilter(
  150. new SetNullFilter(),
  151. new PropertyNameMatcher('cloned')
  152. );
  153. $object = new stdClass();
  154. $object->di = new f007\FooDateInterval('P2D');
  155. $copy = $deepCopy->copy($object);
  156. $this->assertFalse($copy->di->cloned);
  157. }
  158. public function test_it_copies_the_private_properties_of_the_parent_class()
  159. {
  160. $object = new f001\B();
  161. $object->setAProp($aStdClass = new stdClass());
  162. $object->setBProp($bStdClass = new stdClass());
  163. /** @var f001\B $copy */
  164. $copy = deep_copy($object);
  165. $this->assertEqualButNotSame($aStdClass, $copy->getAProp());
  166. $this->assertEqualButNotSame($bStdClass, $copy->getBProp());
  167. }
  168. public function test_it_keeps_reference_of_the_copied_objects_when_copying_the_graph()
  169. {
  170. $a = new f002\A();
  171. $b = new stdClass();
  172. $c = new stdClass();
  173. $a->setProp1($b);
  174. $a->setProp2($c);
  175. $b->c = $c;
  176. /** @var f002\A $copy */
  177. $copy = deep_copy($a);
  178. $this->assertEqualButNotSame($a, $copy);
  179. $this->assertEqualButNotSame($b, $copy->getProp1());
  180. $this->assertEqualButNotSame($c, $copy->getProp2());
  181. $this->assertSame($copy->getProp1()->c, $copy->getProp2());
  182. }
  183. public function test_it_can_copy_graphs_with_circular_references()
  184. {
  185. $a = new stdClass();
  186. $b = new stdClass();
  187. $a->prop = $b;
  188. $b->prop = $a;
  189. $copy = deep_copy($a);
  190. $this->assertEqualButNotSame($a, $copy);
  191. $this->assertEqualButNotSame($b, $copy->prop);
  192. $this->assertSame($copy, $copy->prop->prop);
  193. }
  194. public function test_it_can_copy_graphs_with_circular_references_with_userland_class()
  195. {
  196. $a = new f003\Foo('a');
  197. $b = new f003\Foo('b');
  198. $a->setProp($b);
  199. $b->setProp($a);
  200. /** @var f003\Foo $copy */
  201. $copy = deep_copy($a);
  202. $this->assertEqualButNotSame($a, $copy);
  203. $this->assertEqualButNotSame($b, $copy->getProp());
  204. $this->assertSame($copy, $copy->getProp()->getProp());
  205. }
  206. public function test_it_cannot_copy_unclonable_items()
  207. {
  208. $object = new f004\UnclonableItem();
  209. try {
  210. deep_copy($object);
  211. $this->fail('Expected exception to be thrown.');
  212. } catch (CloneException $exception) {
  213. $this->assertSame(
  214. sprintf(
  215. 'The class "%s" is not cloneable.',
  216. f004\UnclonableItem::class
  217. ),
  218. $exception->getMessage()
  219. );
  220. $this->assertSame(0, $exception->getCode());
  221. $this->assertNull($exception->getPrevious());
  222. }
  223. }
  224. public function test_it_can_skip_uncloneable_objects()
  225. {
  226. $object = new f004\UnclonableItem();
  227. $deepCopy = new DeepCopy();
  228. $deepCopy->skipUncloneable(true);
  229. $copy = $deepCopy->copy($object);
  230. $this->assertSame($object, $copy);
  231. }
  232. public function test_it_uses_the_userland_defined_cloned_method()
  233. {
  234. $object = new f005\Foo();
  235. $copy = deep_copy($object);
  236. $this->assertTrue($copy->cloned);
  237. }
  238. public function test_it_only_uses_the_userland_defined_cloned_method_when_configured_to_do_so()
  239. {
  240. $object = new f005\Foo();
  241. $object->foo = new stdClass();
  242. $copy = deep_copy($object, true);
  243. $this->assertTrue($copy->cloned);
  244. $this->assertSame($object->foo, $copy->foo);
  245. }
  246. public function test_it_uses_type_filter_to_copy_objects_if_matcher_matches()
  247. {
  248. $deepCopy = new DeepCopy();
  249. $deepCopy->addTypeFilter(
  250. new ShallowCopyFilter(),
  251. new TypeMatcher(f006\A::class)
  252. );
  253. $a = new f006\A;
  254. $b = new f006\B;
  255. $a->setAProp($b);
  256. /** @var f006\A $copy */
  257. $copy = $deepCopy->copy($a);
  258. $this->assertTrue($copy->cloned);
  259. $this->assertFalse($copy->getAProp()->cloned);
  260. $this->assertSame($b, $copy->getAProp());
  261. }
  262. public function test_it_uses_filters_to_copy_object_properties_if_matcher_matches()
  263. {
  264. $deepCopy = new DeepCopy();
  265. $deepCopy->addFilter(
  266. new SetNullFilter(),
  267. new PropertyNameMatcher('cloned')
  268. );
  269. $a = new f006\A;
  270. $b = new f006\B;
  271. $a->setAProp($b);
  272. /** @var f006\A $copy */
  273. $copy = $deepCopy->copy($a);
  274. $this->assertNull($copy->cloned);
  275. $this->assertNull($copy->getAProp()->cloned);
  276. }
  277. public function test_it_uses_the_first_filter_matching_for_copying_object_properties()
  278. {
  279. $deepCopy = new DeepCopy();
  280. $deepCopy->addFilter(
  281. new SetNullFilter(),
  282. new PropertyNameMatcher('cloned')
  283. );
  284. $deepCopy->addFilter(
  285. new KeepFilter(),
  286. new PropertyNameMatcher('cloned')
  287. );
  288. $a = new f006\A;
  289. $b = new f006\B;
  290. $a->setAProp($b);
  291. /** @var f006\A $copy */
  292. $copy = $deepCopy->copy($a);
  293. $this->assertNull($copy->cloned);
  294. $this->assertNull($copy->getAProp()->cloned);
  295. }
  296. /**
  297. * @ticket https://github.com/myclabs/DeepCopy/pull/49
  298. */
  299. public function test_it_can_copy_a_SplDoublyLinkedList()
  300. {
  301. $object = new SplDoublyLinkedList();
  302. $a = new stdClass();
  303. $b = new stdClass();
  304. $a->b = $b;
  305. $object->push($a);
  306. /** @var SplDoublyLinkedList $copy */
  307. $copy = deep_copy($object);
  308. $this->assertEqualButNotSame($object, $copy);
  309. $aCopy = $copy->pop();
  310. $this->assertEqualButNotSame($b, $aCopy->b);
  311. }
  312. /**
  313. * @ticket https://github.com/myclabs/DeepCopy/issues/62
  314. */
  315. public function test_matchers_can_access_to_parent_private_properties()
  316. {
  317. $deepCopy = new DeepCopy();
  318. $deepCopy->addFilter(new SetNullFilter(), new PropertyTypeMatcher(stdClass::class));
  319. $object = new f008\B(new stdClass());
  320. /** @var f008\B $copy */
  321. $copy = $deepCopy->copy($object);
  322. $this->assertNull($copy->getFoo());
  323. }
  324. private function assertEqualButNotSame($expected, $val)
  325. {
  326. $this->assertEquals($expected, $val);
  327. $this->assertNotSame($expected, $val);
  328. }
  329. }