/** * {@inheritdoc} */ protected function createChoiceLoader($group = false) { $objects = $this->objects; $this->objectLoader->expects($this->any())->method('getEntities')->will($this->returnCallback(function () use($objects) { $values = array(); foreach ($objects as $object) { $values[$object->getLabel()] = $object; } return $values; })); $this->objectLoader->expects($this->any())->method('getEntitiesByIds')->will($this->returnCallback(function ($idField, $values) use($objects) { $entities = array(); foreach ($values as $id) { if (isset($objects[$id]) && is_string($idField)) { $entities[$id] = $objects[$id]; } } return $entities; })); $this->idReader->expects($this->any())->method('getIdField')->will($this->returnValue('id')); $this->idReader->expects($this->any())->method('isSingleId')->will($this->returnValue(true)); $this->idReader->expects($this->any())->method('getIdValue')->will($this->returnCallback(function ($value) use($objects) { foreach ($objects as $i => $object) { if ($object === $value) { return $i; } } throw new RuntimeException('MOCK_EXCEPTION'); })); return new DynamicDoctrineChoiceLoader($this->objectLoader, $this->idReader, 'label'); }
public function testLoadChoicesForValuesLoadsOnlyChoicesIfValueIsIdReader() { $loader = new DoctrineChoiceLoader($this->om, $this->class, $this->idReader, $this->objectLoader); $choices = array($this->obj2, $this->obj3); $value = array($this->idReader, 'getIdValue'); $this->idReader->expects($this->any())->method('isSingleId')->willReturn(true); $this->idReader->expects($this->any())->method('getIdField')->willReturn('idField'); $this->repository->expects($this->never())->method('findAll'); $this->objectLoader->expects($this->once())->method('getEntitiesByIds')->with('idField', array('2'))->willReturn($choices); $this->idReader->expects($this->any())->method('getIdValue')->willReturnMap(array(array($this->obj2, '2'), array($this->obj3, '3'))); $this->assertSame(array($this->obj2), $loader->loadChoicesForValues(array('2'), $value)); }