/**
  * Test that the configured mappings are accessed
  * correctly.
  */
 public function testConfiguredMappingAccess()
 {
     $prop = new PropertyMapping();
     $prop->setMapping(array('delete_on_remove' => true, 'delete_on_update' => true, 'upload_destination' => '/tmp', 'inject_on_load' => true));
     $this->assertEquals($prop->getUploadDir(), '/tmp');
     $this->assertTrue($prop->getDeleteOnRemove());
     $this->assertTrue($prop->getInjectOnLoad());
 }
示例#2
0
 public function testDirectoryNamerIsCalled()
 {
     $object = new DummyEntity();
     $prop = new PropertyMapping('file', 'fileName');
     $prop->setMapping(array('upload_destination' => '/tmp'));
     $namer = $this->getMock('Vich\\UploaderBundle\\Naming\\DirectoryNamerInterface');
     $namer->expects($this->once())->method('directoryName')->with($object, $prop)->will($this->returnValue('/other-dir'));
     $prop->setDirectoryNamer($namer);
     $this->assertEquals('/other-dir/', $prop->getUploadDir($object));
     $this->assertEquals('/tmp', $prop->getUploadDestination());
 }
 /**
  * Creates the property mapping from the read annotation and configured mapping.
  *
  * @param object $obj         The object.
  * @param string $fieldName   The field name.
  * @param array  $mappingData The mapping data.
  *
  * @return PropertyMapping           The property mapping.
  * @throws \InvalidArgumentException
  */
 protected function createMapping($obj, $fieldName, array $mappingData)
 {
     if (!array_key_exists($mappingData['mapping'], $this->mappings)) {
         throw new \InvalidArgumentException(sprintf('No mapping named "%s" configured.', $mappingData['mapping']));
     }
     $config = $this->mappings[$mappingData['mapping']];
     $fileProperty = isset($mappingData['propertyName']) ? $mappingData['propertyName'] : $fieldName;
     $fileNameProperty = empty($mappingData['fileNameProperty']) ? $fileProperty . $this->defaultFilenameAttributeSuffix : $mappingData['fileNameProperty'];
     $mapping = new PropertyMapping($fileProperty, $fileNameProperty);
     $mapping->setMappingName($mappingData['mapping']);
     $mapping->setMapping($config);
     if ($config['namer']) {
         $mapping->setNamer($this->container->get($config['namer']));
     }
     if ($config['directory_namer']) {
         $mapping->setDirectoryNamer($this->container->get($config['directory_namer']));
     }
     return $mapping;
 }
 /**
  * Creates the property mapping from the read annotation and configured mapping.
  *
  * @param object $obj The object.
  * @param \Vich\UploaderBundle\Annotation\UploadableField $field The read annotation.
  * @return PropertyMapping The property mapping.
  * @throws \InvalidArgumentException
  */
 protected function createMapping($obj, UploadableField $field)
 {
     $class = $this->adapter->getReflectionClass($obj);
     if (!array_key_exists($field->getMapping(), $this->mappings)) {
         throw new \InvalidArgumentException(sprintf('No mapping named "%s" configured.', $field->getMapping()));
     }
     $config = $this->mappings[$field->getMapping()];
     $mapping = new PropertyMapping();
     $mapping->setProperty($class->getProperty($field->getPropertyName()));
     $mapping->setFileNameProperty($class->getProperty($field->getFileNameProperty()));
     $mapping->setMappingName($field->getMapping());
     $mapping->setMapping($config);
     if ($config['namer']) {
         $mapping->setNamer($this->container->get($config['namer']));
     }
     return $mapping;
 }
 /**
  * Creates the property mapping from the read annotation and configured mapping.
  *
  * @param object $obj         The object.
  * @param string $fieldName   The field name.
  * @param array  $mappingData The mapping data.
  *
  * @return PropertyMapping           The property mapping.
  * @throws MappingNotFoundException
  */
 protected function createMapping($obj, $fieldName, array $mappingData)
 {
     if (!array_key_exists($mappingData['mapping'], $this->mappings)) {
         throw MappingNotFoundException::createNotFoundForClassAndField($mappingData['mapping'], $this->getClassName($obj), $fieldName);
     }
     $config = $this->mappings[$mappingData['mapping']];
     $fileProperty = isset($mappingData['propertyName']) ? $mappingData['propertyName'] : $fieldName;
     $fileNameProperty = empty($mappingData['fileNameProperty']) ? $fileProperty . $this->defaultFilenameAttributeSuffix : $mappingData['fileNameProperty'];
     $mapping = new PropertyMapping($fileProperty, $fileNameProperty);
     $mapping->setMappingName($mappingData['mapping']);
     $mapping->setMapping($config);
     if ($config['namer']['service']) {
         $namerConfig = $config['namer'];
         $namer = $this->container->get($namerConfig['service']);
         if (!empty($namerConfig['options']) && $namer instanceof ConfigurableInterface) {
             $namer->configure($namerConfig['options']);
         } else {
             if (!empty($namerConfig['options']) && !$namer instanceof ConfigurableInterface) {
                 throw new \LogicException(sprintf('Namer %s can not receive options as it does not implement ConfigurableInterface.', $namerConfig['service']));
             }
         }
         $mapping->setNamer($namer);
     }
     if ($config['directory_namer']) {
         $mapping->setDirectoryNamer($this->container->get($config['directory_namer']));
     }
     return $mapping;
 }