public function testUploadDoesNotSetMetadataWhenUsingNonMetadataSupporterAdapter()
 {
     $obj = new DummyEntity();
     $mapping = $this->getMock('Vich\\UploaderBundle\\Mapping\\PropertyMapping');
     $adapter = $this->getMockBuilder('\\Gaufrette\\Adapter\\Apc')->disableOriginalConstructor()->getMock();
     $prop = $this->getMockBuilder('\\ReflectionProperty')->disableOriginalConstructor()->getMock();
     $prop->expects($this->any())->method('getValue')->with($obj)->will($this->returnValue('file.txt'));
     $prop->expects($this->any())->method('getName')->will($this->returnValue('nameProperty'));
     $file = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\File\\UploadedFile')->disableOriginalConstructor()->getMock();
     $file->expects($this->once())->method('getClientOriginalName')->will($this->returnValue('filename'));
     $file->expects($this->once())->method('getPathname')->will($this->returnValue(__DIR__ . '/../Fixtures/file.txt'));
     $mapping->expects($this->once())->method('getPropertyValue')->will($this->returnValue($file));
     $mapping->expects($this->once())->method('getUploadDir')->will($this->returnValue('filesystemKey'));
     $mapping->expects($this->once())->method('getFileNameProperty')->will($this->returnValue($prop));
     $mapping->expects($this->once())->method('getProperty')->will($this->returnValue($prop));
     $filesystem = $this->getMockBuilder('\\Gaufrette\\Filesystem')->disableOriginalConstructor()->setMethods(array('getAdapter', 'createStream', 'write'))->getMock();
     $imb = $this->getMockBuilder('\\Gaufrette\\Stream\\InMemoryBuffer')->disableOriginalConstructor()->setMethods(array('open', 'write', 'close'))->getMock();
     $imb->expects($this->once())->method('open')->will($this->returnValue(true));
     $imb->expects($this->once())->method('write')->will($this->returnValue(true));
     $imb->expects($this->once())->method('close')->will($this->returnValue(true));
     $filesystem->expects($this->once())->method('createStream')->will($this->returnValue($imb));
     $filesystemMap = $this->getMockBuilder('Knp\\Bundle\\GaufretteBundle\\FilesystemMap')->disableOriginalConstructor()->setMethods(array('get'))->getMock();
     $filesystemMap->expects($this->once())->method('get')->with('filesystemKey')->will($this->returnValue($filesystem));
     $this->factory->expects($this->once())->method('fromObject')->with($obj)->will($this->returnValue(array($mapping)));
     $adapter->expects($this->never())->method('setMetadata')->will($this->returnValue(null));
     $filesystem->expects($this->any())->method('getAdapter')->will($this->returnValue($adapter));
     $storage = new GaufretteStorage($this->factory, $filesystemMap);
     $storage->upload($obj);
 }
 /**
  * Test the resolve path method throws exception
  * when an invaid field name is specified.
  *
  * @expectedException \InvalidArgumentException
  */
 public function testResolvePathThrowsExceptionOnInvalidFieldName()
 {
     $obj = new DummyEntity();
     $this->factory->expects($this->once())->method('fromField')->with($obj, 'oops')->will($this->returnValue(null));
     $storage = new FileSystemStorage($this->factory);
     $storage->resolvePath($obj, 'oops');
 }
 /**
  *  Test file upload when filename contains directories
  * @dataProvider filenameWithDirectoriesDataProvider
  */
 public function testFilenameWithDirectoriesIsUploadedToCorrectDirectory($dir, $filename, $expectedDir, $expectedFileName)
 {
     $obj = new DummyEntity();
     $file = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\File\\UploadedFile')->disableOriginalConstructor()->getMock();
     $prop = $this->getMockBuilder('\\ReflectionProperty')->disableOriginalConstructor()->getMock();
     $name = new \stdClass();
     $namer = $this->getMockBuilder('Vich\\UploaderBundle\\Naming\\NamerInterface')->disableOriginalConstructor()->getMock();
     $namer->expects($this->once())->method('name')->with($obj, $name)->will($this->returnValue($filename));
     $prop->expects($this->any())->method('getName')->will($this->returnValue($name));
     $mapping = $this->getMock('Vich\\UploaderBundle\\Mapping\\PropertyMapping');
     $mapping->expects($this->once())->method('getNamer')->will($this->returnValue($namer));
     $mapping->expects($this->any())->method('getProperty')->will($this->returnValue($prop));
     $mapping->expects($this->once())->method('getFileNameProperty')->will($this->returnValue($prop));
     $mapping->expects($this->once())->method('getPropertyValue')->with($obj)->will($this->returnValue($file));
     $mapping->expects($this->once())->method('getDeleteOnUpdate')->will($this->returnValue(false));
     $mapping->expects($this->once())->method('hasNamer')->will($this->returnValue(true));
     $mapping->expects($this->once())->method('getUploadDir')->with($obj, $name)->will($this->returnValue($dir));
     $this->factory->expects($this->once())->method('fromObject')->with($obj)->will($this->returnValue(array($mapping)));
     $file->expects($this->once())->method('move')->with($expectedDir, $expectedFileName);
     $storage = new FileSystemStorage($this->factory);
     $storage->upload($obj);
 }
Example #4
0
 /**
  * @dataProvider emptyFilenameProvider
  */
 public function testResolveUriWithEmptyFile($filename)
 {
     $this->mapping->expects($this->once())->method('getFileName')->will($this->returnValue($filename));
     $this->factory->expects($this->once())->method('fromName')->with($this->object, 'file_mapping')->will($this->returnValue($this->mapping));
     $this->assertNull($this->storage->resolvePath($this->object, 'file_mapping'));
 }