/** * Sets up the test */ public function setUp() { $this->adapter = $this->getAdapterMock(); $this->handler = $this->getHandlerMock(); $this->object = new DummyEntity(); $this->event = $this->getEventMock(); // the adapter is always used to return the object $this->adapter->expects($this->any())->method('getObjectFromArgs')->with($this->event)->will($this->returnValue($this->object)); // in these tests, the adapter is always used with the same object $this->adapter->expects($this->any())->method('getClassName')->will($this->returnValue(get_class($this->object))); }
/** * Sets up the test */ public function setUp() { $this->adapter = $this->getAdapterMock(); $this->metadata = $this->getMetadataReaderMock(); $this->handler = $this->getHandlerMock(); $this->object = new DummyEntity(); $this->event = $this->getEventMock(); $that = $this; // the adapter is always used to return the object $this->adapter->expects($this->any())->method('getObjectFromArgs')->with($this->event)->will($this->returnCallback(function () use($that) { return $that->object; })); }
/** * Test that postRemove skips non uploadable entity. */ public function testPostRemoveSkipsNonUploadable() { $obj = new DummyEntity(); $class = new \ReflectionClass($obj); $args = $this->getMockBuilder('Doctrine\\Common\\EventArgs')->disableOriginalConstructor()->getMock(); $this->adapter->expects($this->once())->method('getObjectFromArgs')->will($this->returnValue($obj)); $this->adapter->expects($this->once())->method('getReflectionClass')->will($this->returnValue($class)); $this->driver->expects($this->once())->method('readUploadable')->with($class)->will($this->returnValue(null)); $this->storage->expects($this->never())->method('remove'); $listener = new UploaderListener($this->adapter, $this->driver, $this->storage, $this->injector); $listener->postRemove($args); }
/** * 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; }
public function testConfiguredNamerRetrievedFromContainer() { $obj = new DummyEntity(); $class = new \ReflectionClass($obj); $mappings = array('dummy_file' => array('upload_dir' => '/tmp', 'delete_on_remove' => true, 'namer' => 'my.custom.namer', 'inject_on_load' => true)); $uploadable = $this->getMockBuilder('Vich\\UploaderBundle\\Mapping\\Annotation\\Uploadable')->disableOriginalConstructor()->getMock(); $fileField = $this->getMockBuilder('Vich\\UploaderBundle\\Mapping\\Annotation\\UploadableField')->disableOriginalConstructor()->getMock(); $fileField->expects($this->any())->method('getMapping')->will($this->returnValue('dummy_file')); $fileField->expects($this->once())->method('getPropertyName')->will($this->returnValue('file')); $fileField->expects($this->once())->method('getFileNameProperty')->will($this->returnValue('fileName')); $namer = $this->getMock('Vich\\UploaderBundle\\Naming\\NamerInterface'); $this->container->expects($this->once())->method('get')->with('my.custom.namer')->will($this->returnValue($namer)); $this->adapter->expects($this->any())->method('getReflectionClass')->will($this->returnValue($class)); $this->driver->expects($this->once())->method('readUploadable')->with($class)->will($this->returnValue($uploadable)); $this->driver->expects($this->once())->method('readUploadableFields')->with($class)->will($this->returnValue(array($fileField))); $factory = new PropertyMappingFactory($this->container, $this->driver, $this->adapter, $mappings); $mappings = $factory->fromObject($obj); $this->assertEquals(1, count($mappings)); if (count($mappings) > 0) { $mapping = $mappings[0]; $this->assertEquals($namer, $mapping->getNamer()); $this->assertTrue($mapping->hasNamer()); } }
/** * Tests if the object is Uploadable. * * @param object $obj The object. * @return boolean True if uploadable, false otherwise. */ protected function isUploadable($obj) { $class = $this->adapter->getReflectionClass($obj); return null !== $this->driver->readUploadable($class); }