public function serializeObjectAsPropertyArraySkipsObjectPropertiesThatAreScopeSingleton()
{
$propertyClassName1 = 'DummyClass' . md5(uniqid(mt_rand(), true));
$propertyClassName2 = 'DummyClass' . md5(uniqid(mt_rand(), true));
$propertyClassName3 = 'DummyClass' . md5(uniqid(mt_rand(), true));
eval('class ' . $propertyClassName1 . ' {}');
eval('class ' . $propertyClassName2 . ' {}');
eval('class ' . $propertyClassName3 . ' {}');
$className = 'DummyClass' . md5(uniqid(mt_rand(), true));
eval('class ' . $className . ' {
private $property1;
private $property2;
private $property3;
public function __construct() {
$this->property1 = new ' . $propertyClassName1 . '();
$this->property2 = new ' . $propertyClassName2 . '();
$this->property3 = new ' . $propertyClassName3 . '();
}
public function getProperty1() {
return $this->property1;
}
public function getProperty3() {
return $this->property3;
}
}');
$object = new $className();
$mockReflectionService = $this->getMockBuilder(ReflectionService::class)->disableOriginalConstructor()->getMock();
$mockReflectionService->expects($this->any())->method('isPropertyTaggedWith')->will($this->returnValue(false));
$mockReflectionService->expects($this->at(0))->method('getClassPropertyNames')->with($className)->will($this->returnValue(['property1', 'property2', 'property3']));
$mockReflectionService->expects($this->any())->method('getClassPropertyNames')->will($this->returnValue([]));
$mockObjectManager = $this->getMockBuilder(ObjectManager::class)->disableOriginalConstructor()->getMock();
$mockObjectManager->expects($this->at(0))->method('getObjectNameByClassName')->with($propertyClassName1)->will($this->returnValue('propertyObjectName1'));
$mockObjectManager->expects($this->at(1))->method('getScope')->with('propertyObjectName1')->will($this->returnValue(Configuration::SCOPE_PROTOTYPE));
$mockObjectManager->expects($this->at(2))->method('getObjectNameByClassName')->with($propertyClassName2)->will($this->returnValue('propertyObjectName2'));
$mockObjectManager->expects($this->at(3))->method('getScope')->with('propertyObjectName2')->will($this->returnValue(Configuration::SCOPE_SINGLETON));
$mockObjectManager->expects($this->at(4))->method('getObjectNameByClassName')->with($propertyClassName3)->will($this->returnValue('propertyObjectName3'));
$mockObjectManager->expects($this->at(5))->method('getScope')->with('propertyObjectName3')->will($this->returnValue(Configuration::SCOPE_SESSION));
$mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
$objectSerializer = $this->getAccessibleMock(ObjectSerializer::class, ['dummy'], [], '', true);
$objectSerializer->injectReflectionService($mockReflectionService);
$objectSerializer->injectObjectManager($mockObjectManager);
$objectSerializer->injectPersistenceManager($mockPersistenceManager);
$objectSerializer->_set('objects', [$className => $object]);
$objectHash1 = spl_object_hash($object->getProperty1());
$objectHash3 = spl_object_hash($object->getProperty3());
$expectedArray = [spl_object_hash($object) => [ObjectSerializer::CLASSNAME => $className, ObjectSerializer::PROPERTIES => ['property1' => [ObjectSerializer::TYPE => 'object', ObjectSerializer::VALUE => $objectHash1], 'property3' => [ObjectSerializer::TYPE => 'object', ObjectSerializer::VALUE => $objectHash3]]], $objectHash1 => [ObjectSerializer::CLASSNAME => $propertyClassName1, ObjectSerializer::PROPERTIES => []], $objectHash3 => [ObjectSerializer::CLASSNAME => $propertyClassName3, ObjectSerializer::PROPERTIES => []]];
$this->assertEquals($expectedArray, $objectSerializer->serializeObjectAsPropertyArray($object), 'The singleton has not been skipped.');
}