PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Tests/Unit/Object/ObjectSerializerTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 965 lines | 752 code | 137 blank | 76 comment | 0 complexity | fd0e471a2a4ccc489a96a017fd922d7b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Object;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. /**
  13. * @testcase for the object serializer
  14. *
  15. */
  16. class ObjectSerializerTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  17. /**
  18. * @test
  19. */
  20. public function serializeObjectAsPropertyArraySerializesTheCorrectPropertyArrayUnderTheCorrectObjectName() {
  21. $className = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  22. eval('class ' . $className . ' {
  23. private $privateProperty = \'privateProperty\';
  24. protected $protectedProperty = \'protectedProperty\';
  25. public $publicProperty = \'publicProperty\';
  26. }');
  27. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  28. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('privateProperty', 'protectedProperty', 'publicProperty')));
  29. $objectSerializer = new \TYPO3\FLOW3\Object\ObjectSerializer($this->getMock('TYPO3\FLOW3\Session\SessionInterface', array(), array(), '', FALSE));
  30. $objectSerializer->injectReflectionService($mockReflectionService);
  31. $someObject = new $className();
  32. $expectedPropertyArray = array(
  33. \spl_object_hash($someObject) => array(
  34. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  35. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  36. 'privateProperty' => array (
  37. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  38. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'privateProperty',
  39. ),
  40. 'protectedProperty' => array(
  41. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  42. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'protectedProperty',
  43. ),
  44. 'publicProperty' => array(
  45. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  46. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'publicProperty',
  47. )
  48. )
  49. )
  50. );
  51. $this->assertEquals($expectedPropertyArray, $objectSerializer->serializeObjectAsPropertyArray($someObject), 'The object was not serialized correctly as property array.');
  52. }
  53. /**
  54. * @test
  55. */
  56. public function serializeObjectAsPropertyArraySerializesArrayPropertiesCorrectly() {
  57. $className = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  58. eval('class ' . $className . ' {
  59. private $arrayProperty = array(1,2,3);
  60. }');
  61. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  62. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('arrayProperty')));
  63. $objectSerializer = $this->getMock('TYPO3\FLOW3\Object\ObjectSerializer', array('buildStorageArrayForArrayProperty'), array(), '', FALSE);
  64. $objectSerializer->injectReflectionService($mockReflectionService);
  65. $objectSerializer->expects($this->once())->method('buildStorageArrayForArrayProperty')->with(array(1,2,3))->will($this->returnValue('storable array'));
  66. $someObject = new $className();
  67. $expectedPropertyArray = array(
  68. \spl_object_hash($someObject) => array(
  69. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  70. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  71. 'arrayProperty' => array(
  72. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'array',
  73. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'storable array',
  74. )
  75. )
  76. )
  77. );
  78. $this->assertEquals($expectedPropertyArray, $objectSerializer->serializeObjectAsPropertyArray($someObject), 'The array property was not serialized correctly.');
  79. }
  80. /**
  81. * @test
  82. */
  83. public function serializeObjectAsPropertyArraySerializesArrayObjectPropertiesCorrectly() {
  84. $className = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  85. eval('class ' . $className . ' {
  86. private $arrayObjectProperty;
  87. public function __construct() {
  88. $this->arrayObjectProperty = new \ArrayObject(array(1,2,3));
  89. }
  90. }');
  91. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  92. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('arrayObjectProperty')));
  93. $objectSerializer = $this->getMock('TYPO3\FLOW3\Object\ObjectSerializer', array('buildStorageArrayForArrayProperty'), array(), '', FALSE);
  94. $objectSerializer->injectReflectionService($mockReflectionService);
  95. $objectSerializer->expects($this->once())->method('buildStorageArrayForArrayProperty')->with(array(1,2,3))->will($this->returnValue('storable array'));
  96. $someObject = new $className();
  97. $expectedPropertyArray = array(
  98. \spl_object_hash($someObject) => array(
  99. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  100. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  101. 'arrayObjectProperty' => array(
  102. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'ArrayObject',
  103. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'storable array',
  104. )
  105. )
  106. )
  107. );
  108. $this->assertEquals($expectedPropertyArray, $objectSerializer->serializeObjectAsPropertyArray($someObject), 'The ArrayObject property was not serialized correctly.');
  109. }
  110. /**
  111. * @test
  112. */
  113. public function serializeObjectAsPropertyArraySerializesObjectPropertiesCorrectly() {
  114. $className1 = 'DummyClass1' . md5(uniqid(mt_rand(), TRUE));
  115. $className2 = 'DummyClass2' . md5(uniqid(mt_rand(), TRUE));
  116. eval('class ' . $className2 . '{}');
  117. eval('class ' . $className1 . ' {
  118. private $objectProperty;
  119. public function __construct() {
  120. $this->objectProperty = new ' . $className2 . '();
  121. }
  122. public function getObjectProperty() {
  123. return $this->objectProperty;
  124. }
  125. }');
  126. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  127. $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className1)->will($this->returnValue(array('objectProperty')));
  128. $mockReflectionService->expects($this->at(2))->method('getClassPropertyNames')->with($className2)->will($this->returnValue(array()));
  129. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManager', array(), array(), '', FALSE);
  130. $mockObjectManager->expects($this->once())->method('getObjectNameByClassName')->with($className2)->will($this->returnValue('objectName2'));
  131. $mockObjectManager->expects($this->once())->method('getScope')->with('objectName2')->will($this->returnValue(\TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_PROTOTYPE));
  132. $mockPersistenceManager = $this->getMock('TYPO3\FLOW3\Persistence\PersistenceManagerInterface');
  133. $objectSerializer = $this->getMock('TYPO3\FLOW3\Object\ObjectSerializer', array('dummy'), array(), '', FALSE);
  134. $objectSerializer->injectReflectionService($mockReflectionService);
  135. $objectSerializer->injectObjectManager($mockObjectManager);
  136. $objectSerializer->injectPersistenceManager($mockPersistenceManager);
  137. $someObject = new $className1();
  138. $expectedPropertyArray = array(
  139. \spl_object_hash($someObject) => array(
  140. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className1,
  141. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  142. 'objectProperty' => array(
  143. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  144. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => spl_object_hash($someObject->getObjectProperty()),
  145. )
  146. )
  147. ),
  148. spl_object_hash($someObject->getObjectProperty()) => array(
  149. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className2,
  150. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(),
  151. )
  152. );
  153. $this->assertEquals($expectedPropertyArray, $objectSerializer->serializeObjectAsPropertyArray($someObject), 'The object property was not serialized correctly.');
  154. }
  155. /**
  156. * @test
  157. */
  158. public function serializeObjectAsPropertyArraySkipsObjectPropertiesThatAreScopeSingleton() {
  159. $propertyClassName1 = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  160. $propertyClassName2 = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  161. $propertyClassName3 = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  162. eval('class ' . $propertyClassName1 . ' {}');
  163. eval('class ' . $propertyClassName2 . ' {}');
  164. eval('class ' . $propertyClassName3 . ' {}');
  165. $className = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  166. eval('class ' . $className . ' {
  167. private $property1;
  168. private $property2;
  169. private $property3;
  170. public function __construct() {
  171. $this->property1 = new ' . $propertyClassName1 . '();
  172. $this->property2 = new ' . $propertyClassName2 . '();
  173. $this->property3 = new ' . $propertyClassName3 . '();
  174. }
  175. public function getProperty1() {
  176. return $this->property1;
  177. }
  178. public function getProperty3() {
  179. return $this->property3;
  180. }
  181. }');
  182. $object = new $className();
  183. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  184. $mockReflectionService->expects($this->any())->method('isPropertyTaggedWith')->will($this->returnValue(FALSE));
  185. $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('property1', 'property2', 'property3')));
  186. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->will($this->returnValue(array()));
  187. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManager', array(), array(), '', FALSE);
  188. $mockObjectManager->expects($this->at(0))->method('getObjectNameByClassName')->with($propertyClassName1)->will($this->returnValue('propertyObjectName1'));
  189. $mockObjectManager->expects($this->at(1))->method('getScope')->with('propertyObjectName1')->will($this->returnValue(\TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_PROTOTYPE));
  190. $mockObjectManager->expects($this->at(2))->method('getObjectNameByClassName')->with($propertyClassName2)->will($this->returnValue('propertyObjectName2'));
  191. $mockObjectManager->expects($this->at(3))->method('getScope')->with('propertyObjectName2')->will($this->returnValue(\TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_SINGLETON));
  192. $mockObjectManager->expects($this->at(4))->method('getObjectNameByClassName')->with($propertyClassName3)->will($this->returnValue('propertyObjectName3'));
  193. $mockObjectManager->expects($this->at(5))->method('getScope')->with('propertyObjectName3')->will($this->returnValue(\TYPO3\FLOW3\Object\Configuration\Configuration::SCOPE_SESSION));
  194. $mockPersistenceManager = $this->getMock('TYPO3\FLOW3\Persistence\PersistenceManagerInterface');
  195. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('dummy'), array(), '', FALSE);
  196. $objectSerializer->injectReflectionService($mockReflectionService);
  197. $objectSerializer->injectObjectManager($mockObjectManager);
  198. $objectSerializer->injectPersistenceManager($mockPersistenceManager);
  199. $objectSerializer->_set('objects', array($className => $object));
  200. $objectHash1 = spl_object_hash($object->getProperty1());
  201. $objectHash3 = spl_object_hash($object->getProperty3());
  202. $expectedArray = array(
  203. spl_object_hash($object) => array(
  204. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  205. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  206. 'property1' => array(
  207. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  208. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => $objectHash1,
  209. ),
  210. 'property3' => array(
  211. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  212. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => $objectHash3,
  213. )
  214. )
  215. ),
  216. $objectHash1 => array(
  217. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $propertyClassName1,
  218. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(),
  219. ),
  220. $objectHash3 => array(
  221. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $propertyClassName3,
  222. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(),
  223. )
  224. );
  225. $this->assertEquals($expectedArray, $objectSerializer->serializeObjectAsPropertyArray($object), 'The singleton has not been skipped.');
  226. }
  227. /**
  228. * @test
  229. */
  230. public function serializeObjectAsPropertyArraySkipsPropertiesThatAreAnnotatedToBeTransient() {
  231. $className = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  232. eval('class ' . $className . ' {
  233. private $privateProperty = \'privateProperty\';
  234. protected $protectedProperty = \'protectedProperty\';
  235. public $publicProperty = \'publicProperty\';
  236. }');
  237. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  238. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('privateProperty', 'protectedProperty', 'publicProperty')));
  239. $mockReflectionService->expects($this->at(1))->method('isPropertyTaggedWith')->with($className, 'privateProperty', 'transient')->will($this->returnValue(FALSE));
  240. $mockReflectionService->expects($this->at(2))->method('isPropertyTaggedWith')->with($className, 'protectedProperty', 'transient')->will($this->returnValue(TRUE));
  241. $mockReflectionService->expects($this->at(3))->method('isPropertyTaggedWith')->with($className, 'publicProperty', 'transient')->will($this->returnValue(FALSE));
  242. $objectSerializerClassName = $this->buildAccessibleProxy('TYPO3\FLOW3\Object\ObjectSerializer');
  243. $objectSerializer = new $objectSerializerClassName($this->getMock('TYPO3\FLOW3\Session\SessionInterface', array(), array(), '', FALSE));
  244. $objectSerializer->injectReflectionService($mockReflectionService);
  245. $someObject = new $className();
  246. $expectedPropertyArray = array(
  247. spl_object_hash($someObject) => array(
  248. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  249. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  250. 'privateProperty' => array (
  251. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  252. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'privateProperty',
  253. ),
  254. 'publicProperty' => array(
  255. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  256. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'publicProperty',
  257. )
  258. )
  259. )
  260. );
  261. $this->assertEquals($expectedPropertyArray, $objectSerializer->serializeObjectAsPropertyArray($someObject), 'The object was not stored correctly as property array.');
  262. }
  263. /**
  264. * @test
  265. */
  266. public function serializeObjectSerializesObjectInstancesOnlyOnceToPreventRecursion() {
  267. $className = 'DummyClassForRecursion' . md5(uniqid(mt_rand(), TRUE));
  268. eval('class ' . $className . ' {
  269. public $name;
  270. public $parent;
  271. public $child;
  272. }');
  273. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManager', array(), array(), '', FALSE);
  274. $mockObjectManager->expects($this->any())->method('getObjectNameByClassName')->with($className)->will($this->returnValue(\TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME));
  275. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  276. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('name', 'parent', 'child')));
  277. $mockReflectionService->expects($this->any())->method('isPropertyTaggedWith')->will($this->returnValue(FALSE));
  278. $mockPersistenceManager = $this->getMock('TYPO3\FLOW3\Persistence\PersistenceManagerInterface');
  279. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('dummy'));
  280. $objectSerializer->injectObjectManager($mockObjectManager);
  281. $objectSerializer->injectReflectionService($mockReflectionService);
  282. $objectSerializer->injectPersistenceManager($mockPersistenceManager);
  283. $objectA = new $className();
  284. $objectA->name = 'A';
  285. $objectB = new $className();
  286. $objectB->name = 'B';
  287. $objectA->child = $objectB;
  288. $objectB->parent = $objectA;
  289. $expectedPropertyArray = array(
  290. spl_object_hash($objectB) => array(
  291. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  292. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  293. 'name' => array (
  294. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  295. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'B',
  296. ),
  297. 'parent' => array (
  298. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  299. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => \spl_object_hash($objectA),
  300. ),
  301. 'child' => array(
  302. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  303. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => NULL,
  304. )
  305. )
  306. ),
  307. spl_object_hash($objectA) => array(
  308. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  309. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  310. 'name' => array (
  311. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  312. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'A',
  313. ),
  314. 'parent' => array(
  315. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  316. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => NULL,
  317. ),
  318. 'child' => array (
  319. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  320. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => \spl_object_hash($objectB),
  321. )
  322. )
  323. )
  324. );
  325. $actualPropertyArray = $objectSerializer->serializeObjectAsPropertyArray($objectA);
  326. $this->assertEquals($expectedPropertyArray, $actualPropertyArray);
  327. }
  328. /**
  329. * @test
  330. */
  331. public function serializeObjectAsPropertyArraySerializesOnlyTheUuidOfEntityObjectsIfTheyAreNotMarkedAsNew() {
  332. $sessionClassName = 'dummyClass' . md5(uniqid(mt_rand(), TRUE));
  333. eval('class ' . $sessionClassName . ' {
  334. public $entityProperty;
  335. }');
  336. $entityClassName = 'entityClass' . md5(uniqid(mt_rand(), TRUE));
  337. eval('class ' . $entityClassName . ' implements \TYPO3\FLOW3\Object\Proxy\ProxyInterface {
  338. public function FLOW3_Aop_Proxy_invokeJoinPoint(\TYPO3\FLOW3\Aop\JoinPointInterface $joinPoint) {}
  339. public function __clone() {}
  340. public function __wakeup() {}
  341. }');
  342. $entityObject = new $entityClassName();
  343. $mockPersistenceManager = $this->getMock('TYPO3\FLOW3\Persistence\PersistenceManagerInterface');
  344. $mockPersistenceManager->expects($this->once())->method('isNewObject')->with($entityObject)->will($this->returnValue(FALSE));
  345. $mockPersistenceManager->expects($this->once())->method('getIdentifierByObject')->with($entityObject)->will($this->returnValue('someUUID'));
  346. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  347. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->with($sessionClassName)->will($this->returnValue(array('entityProperty')));
  348. $mockReflectionService->expects($this->at(2))->method('isClassAnnotatedWith')->with($entityClassName, 'TYPO3\FLOW3\Annotations\Entity')->will($this->returnValue(TRUE));
  349. $objectSerializer = $this->getMock('TYPO3\FLOW3\Object\ObjectSerializer', array('dummy'), array(), '', FALSE);
  350. $objectSerializer->injectReflectionService($mockReflectionService);
  351. $objectSerializer->injectPersistenceManager($mockPersistenceManager);
  352. $expectedArray = array(
  353. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $sessionClassName,
  354. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  355. 'entityProperty' => array(
  356. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'persistenceObject',
  357. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => $entityClassName . ':' . 'someUUID',
  358. )
  359. )
  360. );
  361. $sessionObject = new $sessionClassName();
  362. $sessionObject->entityProperty = $entityObject;
  363. $objectsAsArray = $objectSerializer->serializeObjectAsPropertyArray($sessionObject);
  364. $this->assertEquals($expectedArray, $objectsAsArray[spl_object_hash($sessionObject)]);
  365. }
  366. /**
  367. * @test
  368. */
  369. public function serializeObjectAsPropertyArraySerializessOnlyTheUuidOfPersistenceValueobjectsIfTheyAreNotMarkedAsNew() {
  370. $sessionClassName = 'dummyClass' . md5(uniqid(mt_rand(), TRUE));
  371. eval('class ' . $sessionClassName . ' {
  372. public $entityProperty;
  373. }');
  374. $entityClassName = 'entityClass' . md5(uniqid(mt_rand(), TRUE));
  375. eval('class ' . $entityClassName . ' implements \TYPO3\FLOW3\Object\Proxy\ProxyInterface {
  376. public function FLOW3_Aop_Proxy_invokeJoinPoint(\TYPO3\FLOW3\Aop\JoinPointInterface $joinPoint) {}
  377. public function __clone() {}
  378. public function __wakeup() {}
  379. }');
  380. $entityObject = new $entityClassName();
  381. $mockPersistenceManager = $this->getMock('TYPO3\FLOW3\Persistence\PersistenceManagerInterface');
  382. $mockPersistenceManager->expects($this->once())->method('isNewObject')->with($entityObject)->will($this->returnValue(FALSE));
  383. $mockPersistenceManager->expects($this->once())->method('getIdentifierByObject')->with($entityObject)->will($this->returnValue('someUUID'));
  384. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  385. $mockReflectionService->expects($this->any())->method('getClassPropertyNames')->with($sessionClassName)->will($this->returnValue(array('entityProperty')));
  386. $mockReflectionService->expects($this->at(2))->method('isClassAnnotatedWith')->with($entityClassName, 'TYPO3\FLOW3\Annotations\Entity')->will($this->returnValue(FALSE));
  387. $mockReflectionService->expects($this->at(3))->method('isClassAnnotatedWith')->with($entityClassName, 'TYPO3\FLOW3\Annotations\ValueObject')->will($this->returnValue(TRUE));
  388. $objectSerializer = $this->getMock('TYPO3\FLOW3\Object\ObjectSerializer', array('dummy'), array(), '', FALSE);
  389. $objectSerializer->injectReflectionService($mockReflectionService);
  390. $objectSerializer->injectPersistenceManager($mockPersistenceManager);
  391. $expectedArray = array(
  392. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $sessionClassName,
  393. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  394. 'entityProperty' => array(
  395. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'persistenceObject',
  396. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => $entityClassName . ':someUUID'
  397. )
  398. )
  399. );
  400. $sessionObject = new $sessionClassName();
  401. $sessionObject->entityProperty = $entityObject;
  402. $objectsAsArray = $objectSerializer->serializeObjectAsPropertyArray($sessionObject);
  403. $this->assertEquals($expectedArray, $objectsAsArray[spl_object_hash($sessionObject)]);
  404. }
  405. /**
  406. * @test
  407. */
  408. public function deserializeObjectsArraySetsTheInternalObjectsAsArrayPropertyCorreclty() {
  409. $someDataArray = array(
  410. 'bla' => 'blub',
  411. 'another' => 'bla',
  412. 'and another' => 'blub'
  413. );
  414. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManager', array(), array(), '', FALSE);
  415. $mockObjectManager->expects($this->any())->method('isRegistered')->will($this->returnValue(FALSE));
  416. $objectSerializerClassName = $this->buildAccessibleProxy('TYPO3\FLOW3\Object\ObjectSerializer');
  417. $objectSerializer = new $objectSerializerClassName($this->getMock('TYPO3\FLOW3\Session\SessionInterface', array(), array(), '', FALSE));
  418. $objectSerializer->injectObjectManager($mockObjectManager);
  419. $objectSerializer->deserializeObjectsArray($someDataArray);
  420. $this->assertEquals($someDataArray, $objectSerializer->_get('objectsAsArray'), 'The data array has not been set as expected.');
  421. }
  422. /**
  423. * @test
  424. */
  425. public function deserializeObjectsArrayCallsReconstituteObjectWithTheCorrectObjectData() {
  426. $className = 'dummyClass' . md5(uniqid(mt_rand(), TRUE));
  427. eval('class ' . $className . ' {}');
  428. $className1 = 'class1' . md5(uniqid(mt_rand(), TRUE));
  429. $object1 = $this->getMock($className, array(), array(), $className1, FALSE);
  430. $className2 = 'class2' . md5(uniqid(mt_rand(), TRUE));
  431. $object2 = $this->getMock($className, array(), array(), $className2, FALSE);
  432. $className3 = 'class3' . md5(uniqid(mt_rand(), TRUE));
  433. $object3 = $this->getMock($className, array(), array(), $className3, FALSE);
  434. $objectsAsArray = array(
  435. spl_object_hash($object1) => array(
  436. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className1,
  437. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(1),
  438. ),
  439. spl_object_hash($object2) => array(
  440. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className2,
  441. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(2),
  442. ),
  443. 'someReferencedObject1' => array(),
  444. spl_object_hash($object3) => array(
  445. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className3,
  446. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(3),
  447. ),
  448. 'someReferencedObject2' => array(),
  449. 'someReferencedObject3' => array(),
  450. );
  451. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManager', array(), array(), '', FALSE);
  452. $mockObjectManager->expects($this->any())->method('isRegistered')->will($this->returnValue(TRUE));
  453. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('reconstituteObject', 'createEmptyObject'), array(), '', FALSE);
  454. $objectSerializer->expects($this->at(0))->method('reconstituteObject')->with(spl_object_hash($object1), $objectsAsArray[spl_object_hash($object1)])->will($this->returnValue($object1));
  455. $objectSerializer->expects($this->at(1))->method('reconstituteObject')->with(spl_object_hash($object2), $objectsAsArray[spl_object_hash($object2)])->will($this->returnValue($object2));
  456. $objectSerializer->expects($this->at(2))->method('reconstituteObject')->with(spl_object_hash($object3), $objectsAsArray[spl_object_hash($object3)])->will($this->returnValue($object3));
  457. $objectSerializer->injectObjectManager($mockObjectManager);
  458. $objects = $objectSerializer->deserializeObjectsArray($objectsAsArray);
  459. $this->assertEquals(
  460. array(
  461. spl_object_hash($object1) => $object1,
  462. spl_object_hash($object2) => $object2,
  463. spl_object_hash($object3) => $object3
  464. ),
  465. $objects
  466. );
  467. }
  468. /**
  469. * @test
  470. */
  471. public function buildStorageArrayCreatesTheCorrectArrayForAnArrayProperty() {
  472. $objectSerializerClassName = $this->buildAccessibleProxy('TYPO3\FLOW3\Object\ObjectSerializer');
  473. $objectSerializer = new $objectSerializerClassName($this->getMock('TYPO3\FLOW3\Session\SessionInterface', array(), array(), '', FALSE));
  474. $expectedArray = array(
  475. 'key1' => array(
  476. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  477. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 1,
  478. ),
  479. 'key2' => array(
  480. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  481. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 2,
  482. ),
  483. 'key3' => array(
  484. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'array',
  485. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => array(
  486. 'key4' => array(
  487. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  488. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 4,
  489. ),
  490. 'key5' => array(
  491. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  492. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 5,
  493. )
  494. )
  495. )
  496. );
  497. $arrayProperty = array(
  498. 'key1' => 1,
  499. 'key2' => 2,
  500. 'key3' => array(
  501. 'key4' => 4,
  502. 'key5' => 5
  503. )
  504. );
  505. $this->assertSame($expectedArray, $objectSerializer->_call('buildStorageArrayForArrayProperty', $arrayProperty));
  506. }
  507. /**
  508. * @test
  509. */
  510. public function buildStorageArrayCreatesTheCorrectArrayForAnArrayPropertyWithContainingObject() {
  511. $className = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  512. eval('class ' . $className . ' {}');
  513. $mockObject = $this->getMock($className);
  514. $objectName = spl_object_hash($mockObject);
  515. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('serializeObjectAsPropertyArray'), array(), '', FALSE);
  516. $objectSerializer->expects($this->once())->method('serializeObjectAsPropertyArray')->with($mockObject);
  517. $arrayProperty = array(
  518. 'key1' => 1,
  519. 'key2' => $mockObject,
  520. );
  521. $expectedArray = array(
  522. 'key1' => array(
  523. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  524. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 1,
  525. ),
  526. 'key2' => array(
  527. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  528. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => $objectName,
  529. )
  530. );
  531. $this->assertSame($expectedArray, $objectSerializer->_call('buildStorageArrayForArrayProperty', $arrayProperty));
  532. }
  533. /**
  534. * @test
  535. */
  536. public function serializeObjectAsPropertyArrayForSplObjectStoragePropertyBuildsTheCorrectArrayStructureAndStoresEveryObjectInsideSeparately() {
  537. $propertyClassName1 = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  538. $propertyClassName2 = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  539. eval('class ' . $propertyClassName1 . ' {}');
  540. eval('class ' . $propertyClassName2 . ' {}');
  541. $propertyClass1 = new $propertyClassName1();
  542. $propertyClass2 = new $propertyClassName2();
  543. $className = 'DummyClass' . md5(uniqid(mt_rand(), TRUE));
  544. eval('class ' . $className . ' {
  545. private $SplObjectProperty;
  546. public function __construct($object1, $object2) {
  547. $this->SplObjectProperty = new \SplObjectStorage();
  548. $this->SplObjectProperty->attach($object1);
  549. $this->SplObjectProperty->attach($object2);
  550. }
  551. public function getSplObjectProperty() {
  552. return $this->SplObjectProperty;
  553. }
  554. }');
  555. $mockReflectionService = $this->getMock('TYPO3\FLOW3\Reflection\ReflectionService', array(), array(), '', FALSE);
  556. $mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(array('SplObjectProperty')));
  557. $mockReflectionService->expects($this->at(1))->method('getClassPropertyNames')->will($this->returnValue(array()));
  558. $mockReflectionService->expects($this->at(2))->method('getClassPropertyNames')->will($this->returnValue(array()));
  559. $mockReflectionService->expects($this->at(3))->method('getClassPropertyNames')->will($this->returnValue(array()));
  560. $mockReflectionService->expects($this->any())->method('isPropertyTaggedWith')->with($className, 'SplObjectProperty', 'transient')->will($this->returnValue(FALSE));
  561. $objectSerializer = new \TYPO3\FLOW3\Object\ObjectSerializer($this->getMock('TYPO3\FLOW3\Session\SessionInterface', array(), array(), '', FALSE));
  562. $objectSerializer->injectReflectionService($mockReflectionService);
  563. $objectHash1 = spl_object_hash($propertyClass1);
  564. $objectHash2 = spl_object_hash($propertyClass2);
  565. $object = new $className($propertyClass1, $propertyClass2);
  566. $expectedArray = array(
  567. spl_object_hash($object) => array(
  568. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  569. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  570. 'SplObjectProperty' => array(
  571. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'SplObjectStorage',
  572. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => array($objectHash1, $objectHash2)
  573. )
  574. )
  575. ),
  576. $objectHash1 => array(
  577. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $propertyClassName1,
  578. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(),
  579. ),
  580. $objectHash2 => array(
  581. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $propertyClassName2,
  582. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(),
  583. ),
  584. );
  585. $this->assertEquals($expectedArray, $objectSerializer->serializeObjectAsPropertyArray($object));
  586. }
  587. /**
  588. * @test
  589. */
  590. public function reconstituteObjectCallsTheCorrectReconstitutePropertyTypeFunctionsAndSetsTheValuesInTheObject() {
  591. $emptyClassName = 'emptyClass' . md5(uniqid(mt_rand(), TRUE));
  592. eval('class ' . $emptyClassName . ' {}');
  593. $className = 'someClass' . md5(uniqid(mt_rand(), TRUE));
  594. eval('class ' . $className . ' {
  595. private $simpleProperty;
  596. private $arrayProperty;
  597. private $arrayObjectProperty;
  598. private $objectProperty;
  599. private $splObjectStorageProperty;
  600. private $persistenceObjectProperty;
  601. public function getSimpleProperty() { return $this->simpleProperty; }
  602. public function getArrayProperty() { return $this->arrayProperty; }
  603. public function getArrayObjectProperty() { return $this->arrayObjectProperty; }
  604. public function getObjectProperty() { return $this->objectProperty; }
  605. public function getSplObjectStorageProperty() { return $this->splObjectStorageProperty; }
  606. public function getPersistenceObjectProperty() { return $this->persistenceObjectProperty; }
  607. }');
  608. $objectData = array(
  609. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $className,
  610. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(
  611. 'simpleProperty' => array (
  612. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  613. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'simplePropertyValue',
  614. ),
  615. 'arrayProperty' => array (
  616. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'array',
  617. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'arrayPropertyValue',
  618. ),
  619. 'arrayObjectProperty' => array (
  620. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'ArrayObject',
  621. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'arrayObjectPropertyValue',
  622. ),
  623. 'objectProperty' => array (
  624. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  625. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'emptyClass'
  626. ),
  627. 'splObjectStorageProperty' => array (
  628. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'SplObjectStorage',
  629. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'splObjectStoragePropertyValue',
  630. ),
  631. 'persistenceObjectProperty' => array (
  632. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'persistenceObject',
  633. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'persistenceObjectClassName:persistenceObjectUUID',
  634. )
  635. )
  636. );
  637. $mockObjectManager = $this->getMock('TYPO3\FLOW3\Object\ObjectManager', array(), array(), '', FALSE);
  638. $mockObjectManager->expects($this->any())->method('getClassNameByObjectName')->will($this->returnArgument(0));
  639. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('reconstituteArray', 'reconstituteSplObjectStorage', 'reconstitutePersistenceObject'), array(), '', FALSE);
  640. $objectSerializer->injectObjectManager($mockObjectManager);
  641. $objectSerializer->expects($this->at(0))->method('reconstituteArray')->with('arrayPropertyValue')->will($this->returnValue('arrayPropertyValue'));
  642. $objectSerializer->expects($this->at(1))->method('reconstituteArray')->with('arrayObjectPropertyValue')->will($this->returnValue(array('arrayObjectPropertyValue')));
  643. $objectSerializer->expects($this->once())->method('reconstituteSplObjectStorage')->with('splObjectStoragePropertyValue')->will($this->returnValue('splObjectStoragePropertyValue'));
  644. $objectSerializer->expects($this->once())->method('reconstitutePersistenceObject')->with('persistenceObjectClassName', 'persistenceObjectUUID')->will($this->returnValue('persistenceObjectPropertyValue'));
  645. $objectsAsArray = array(
  646. 'emptyClass' => array(
  647. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => $emptyClassName,
  648. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => array(),
  649. )
  650. );
  651. $objectSerializer->_set('objectsAsArray', $objectsAsArray);
  652. $object = $objectSerializer->_call('reconstituteObject', 'dummyobjecthash', $objectData);
  653. $this->assertEquals('simplePropertyValue', $object->getSimpleProperty(), 'Simple property was not set as expected.');
  654. $this->assertEquals('arrayPropertyValue', $object->getArrayProperty(), 'Array property was not set as expected.');
  655. $this->assertEquals(new \ArrayObject(array('arrayObjectPropertyValue')), $object->getArrayObjectProperty(), 'ArrayObject property was not set as expected.');
  656. $this->assertEquals(new $emptyClassName(), $object->getObjectProperty(), 'Object property was not set as expected.');
  657. $this->assertEquals('splObjectStoragePropertyValue', $object->getSplObjectStorageProperty(), 'SplObjectStorage property was not set as expected.');
  658. $this->assertEquals('persistenceObjectPropertyValue', $object->getPersistenceObjectProperty(), 'Persistence object property was not set as expected.');
  659. }
  660. /**
  661. * @test
  662. */
  663. public function reconstituteArrayWorks() {
  664. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('dummy'), array(), '', FALSE);
  665. $dataArray = array(
  666. 'key1' => array(
  667. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  668. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 1,
  669. ),
  670. 'key2' => array(
  671. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  672. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 2,
  673. ),
  674. 'key3' => array(
  675. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'array',
  676. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => array(
  677. 'key4' => array(
  678. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  679. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 4,
  680. ),
  681. 'key5' => array(
  682. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  683. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 5,
  684. )
  685. )
  686. )
  687. );
  688. $expectedArrayProperty = array(
  689. 'key1' => 1,
  690. 'key2' => 2,
  691. 'key3' => array(
  692. 'key4' => 4,
  693. 'key5' => 5
  694. )
  695. );
  696. $this->assertEquals($expectedArrayProperty, $objectSerializer->_call('reconstituteArray', $dataArray), 'The array was not reconstituted correctly.');
  697. }
  698. /**
  699. * @test
  700. */
  701. public function reconstituteArrayWorksWithObjectsInTheArray() {
  702. $objectsAsArray = array(
  703. 'some object' => array(
  704. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => 'some object',
  705. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES,
  706. )
  707. );
  708. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('reconstituteObject'), array(), '', FALSE);
  709. $objectSerializer->expects($this->once())->method('reconstituteObject')->with('some object', array(\TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => 'some object',\TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES,))->will($this->returnValue('reconstituted object'));
  710. $objectSerializer->_set('objectsAsArray', $objectsAsArray);
  711. $dataArray = array(
  712. 'key1' => array(
  713. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  714. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 1,
  715. ),
  716. 'key2' => array(
  717. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'object',
  718. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'some object'
  719. )
  720. );
  721. $expectedArrayProperty = array(
  722. 'key1' => 1,
  723. 'key2' => 'reconstituted object',
  724. );
  725. $this->assertEquals($expectedArrayProperty, $objectSerializer->_call('reconstituteArray', $dataArray), 'The array was not reconstituted correctly.');
  726. }
  727. /**
  728. * @test
  729. */
  730. public function reconstituteArrayWorksWithSplObjectStorageInTheArray() {
  731. $objectsAsArray = array(
  732. 'some object' => array(
  733. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => 'some object',
  734. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES,
  735. )
  736. );
  737. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('reconstituteSplObjectStorage'), array(), '', FALSE);
  738. $objectSerializer->expects($this->once())->method('reconstituteSplObjectStorage')->with('some object', array(\TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => 'some object',\TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES,))->will($this->returnValue('reconstituted object'));
  739. $objectSerializer->_set('objectsAsArray', $objectsAsArray);
  740. $dataArray = array(
  741. 'key1' => array(
  742. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  743. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 1,
  744. ),
  745. 'key2' => array(
  746. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'SplObjectStorage',
  747. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 'some object'
  748. )
  749. );
  750. $expectedArrayProperty = array(
  751. 'key1' => 1,
  752. 'key2' => 'reconstituted object',
  753. );
  754. $this->assertEquals($expectedArrayProperty, $objectSerializer->_call('reconstituteArray', $dataArray), 'The array was not reconstituted correctly.');
  755. }
  756. /**
  757. * @test
  758. */
  759. public function reconstituteArrayWorksWithPersistenceObjectsInTheArray() {
  760. $objectsAsArray = array(
  761. 'some object' => array(
  762. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => 'some object',
  763. \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES => \TYPO3\FLOW3\Object\ObjectSerializer::PROPERTIES,
  764. )
  765. );
  766. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('reconstitutePersistenceObject'), array(), '', FALSE);
  767. $objectSerializer->expects($this->once())->method('reconstitutePersistenceObject')->with('persistenceObjectClassName', 'someUUID')->will($this->returnValue('reconstituted object'));
  768. $objectSerializer->_set('objectsAsArray', $objectsAsArray);
  769. $dataArray = array(
  770. 'key1' => array(
  771. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'simple',
  772. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => 1,
  773. ),
  774. 'key2' => array(
  775. \TYPO3\FLOW3\Object\ObjectSerializer::TYPE => 'persistenceObject',
  776. \TYPO3\FLOW3\Object\ObjectSerializer::VALUE => array(
  777. \TYPO3\FLOW3\Object\ObjectSerializer::CLASSNAME => 'persistenceObjectClassName',
  778. 'UUID' => 'someUUID',
  779. )
  780. )
  781. );
  782. $expectedArrayProperty = array(
  783. 'key1' => 1,
  784. 'key2' => 'reconstituted object',
  785. );
  786. $this->assertEquals($expectedArrayProperty, $objectSerializer->_call('reconstituteArray', $dataArray), 'The array was not reconstituted correctly.');
  787. }
  788. /**
  789. * @test
  790. */
  791. public function reconstituteSplObjectStorageWorks() {
  792. $mockObject1 = $this->getMock('dummyClass1' . md5(uniqid(mt_rand(), TRUE)), array(), array(), '', FALSE);
  793. $mockObject2 = $this->getMock('dummyClass2' . md5(uniqid(mt_rand(), TRUE)), array(), array(), '', FALSE);
  794. $objectsAsArray = array(
  795. 'some object' => array('object1 data'),
  796. 'some other object' => array('object2 data')
  797. );
  798. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('reconstituteObject'), array(), '', FALSE);
  799. $objectSerializer->expects($this->at(0))->method('reconstituteObject')->with('some object', array('object1 data'))->will($this->returnValue($mockObject1));
  800. $objectSerializer->expects($this->at(1))->method('reconstituteObject')->with('some other object', array('object2 data'))->will($this->returnValue($mockObject2));
  801. $objectSerializer->_set('objectsAsArray', $objectsAsArray);
  802. $dataArray = array('some object', 'some other object');
  803. $expectedResult = new \SplObjectStorage();
  804. $expectedResult->attach($mockObject1);
  805. $expectedResult->attach($mockObject2);
  806. $this->assertEquals($expectedResult, $objectSerializer->_call('reconstituteSplObjectStorage', $dataArray), 'The SplObjectStorage was not reconstituted correctly.');
  807. }
  808. /**
  809. * @test
  810. */
  811. public function reconstitutePersistenceObjectRetrievesTheObjectCorrectlyFromThePersistenceFramework() {
  812. $mockPersistenceManager = $this->getMock('TYPO3\FLOW3\Persistence\PersistenceManagerInterface');
  813. $mockPersistenceManager->expects($this->once())->method('getObjectByIdentifier')->with('someUUID')->will($this->returnValue('theObject'));
  814. $objectSerializer = $this->getAccessibleMock('TYPO3\FLOW3\Object\ObjectSerializer', array('dummy'), array(), '', FALSE);
  815. $objectSerializer->injectPersistenceManager($mockPersistenceManager);
  816. $this->assertEquals('theObject', $objectSerializer->_call('reconstitutePersistenceObject', 'someClassName', 'someUUID'));
  817. }
  818. }
  819. ?>