public static function goodInputs() { $filesystem1 = new Filesystem(new Local(FILESYSTEM1)); $gaufrette = new File('key1', $filesystem1); $gaufrette->setContent(self::CONTENT); $file = new FSiFile('key2', $filesystem1); $file->setContent(self::CONTENT); return array(array($gaufrette), array($file)); }
/** * Updating files keys. * * @param \Doctrine\Common\Persistence\ObjectManager $objectManager * @param \FSi\DoctrineExtensions\Uploadable\Mapping\ClassMetadata $uploadableMeta * @param object $object * @param \FSi\DoctrineExtensions\Mapping\Event\AdapterInterface $eventAdapter * @throws \FSi\DoctrineExtensions\Uploadable\Exception\RuntimeException */ protected function updateFiles(ObjectManager $objectManager, $uploadableMeta, $object, AdapterInterface $eventAdapter) { $propertyObserver = $this->getPropertyObserver($objectManager); $id = $eventAdapter->extractIdentifier($objectManager, $object, false); $id = implode('-', $id); foreach ($uploadableMeta->getUploadableProperties() as $property => $config) { if (!$propertyObserver->hasSavedValue($object, $config['targetField']) || $propertyObserver->hasValueChanged($object, $config['targetField'])) { $accessor = PropertyAccess::getPropertyAccessor(); $file = $accessor->getValue($object, $config['targetField']); $filesystem = $this->computeFilesystem($config); // Since file has changed, the old one should be removed. if ($oldKey = $accessor->getValue($object, $property)) { if ($oldFile = $propertyObserver->getSavedValue($object, $config['targetField'])) { $this->addToDelete($oldFile); } } if (empty($file)) { $accessor->setValue($object, $property, null); $propertyObserver->saveValue($object, $config['targetField']); continue; } if (!$this->getFileHandler()->supports($file)) { throw new RuntimeException(sprintf('Can\'t handle resource of type "%s".', is_object($file) ? get_class($file) : gettype($file))); } $keymaker = $this->computeKeymaker($config); $keyLength = $this->computeKeyLength($config); $keyPattern = $config['keyPattern'] ? $config['keyPattern'] : null; $fileName = $this->getFileHandler()->getName($file); $newKey = $this->generateNewKey($keymaker, $object, $property, $id, $fileName, $keyLength, $keyPattern, $filesystem); $newFile = new File($newKey, $filesystem); $newFile->setContent($this->getFileHandler()->getContent($file)); $accessor->setValue($object, $property, $newFile->getKey()); // Save its current value, so if another update will be called, there won't be another saving. $propertyObserver->setValue($object, $config['targetField'], $newFile); } } }
public function testFileHandlerReturnBasename() { $key = 'some/key/blabla'; $file = new File($key, $this->_filesystem1); $file->setContent(''); $this->assertEquals('blabla', $this->_uploadableListener->getFileHandler()->getName($file)); $file = new \SplFileInfo(TESTS_PATH . self::TEST_FILE1); $this->assertEquals('penguins.jpg', $this->_uploadableListener->getFileHandler()->getName($file)); }
public function testExceptionWhenKeyIsToLong() { $key = 'some/key' . str_repeat('/aaaa', 60); $user = $this->getUser(); $file = new File($key, $this->_filesystem1); $file->setContent(''); $this->setExpectedException('FSi\\DoctrineExtensions\\Uploadable\\Exception\\RuntimeException'); $user->setFile($file); $this->_em->persist($user); $this->_em->flush(); }