function it_compute_file_asset_path(File $file, AssetsExtension $assets, FSiFilePathResolver $filePathResolver) { $file->getKey()->willReturn('file-name.txt'); $filePathResolver->filePath($file, 'uploaded')->willReturn('/uploaded/file-name.txt'); $assets->getAssetUrl('/uploaded/file-name.txt')->willReturn('/uploaded/file-name.txt'); $this->fileAsset($file, 'uploaded')->shouldReturn('/uploaded/file-name.txt'); }
function it_sets_not_translated_attribute_when_property_no_translation(FSiFilePathResolver $filePathResolver, File $uploadableFile, FormInterface $form, FormInterface $parentForm, TranslatableFormHelper $translatableFormHelper) { $view = new FormView(); $fileView = new FormView($view); $removeView = new FormView($view); $view->children['translatable_property'] = $fileView; $view->children['remove'] = $removeView; $translatableFormHelper->getFirstTranslatableParent($form)->willReturn($parentForm); $translatableFormHelper->isFormPropertyPathTranslatable($form)->willReturn(true); $translatableFormHelper->isFormDataInCurrentLocale($parentForm)->willReturn(false); $translatableFormHelper->getFormNormDataLocale($parentForm)->willReturn('en'); $form->getName()->willReturn('translatable_property'); $data = new \stdClass(); $view->vars['value'] = $data; $fileView->vars['value'] = $uploadableFile->getWrappedObject(); $fileView->vars['data'] = $uploadableFile->getWrappedObject(); $filePathResolver->fileBasename($uploadableFile->getWrappedObject())->willReturn('default-locale-filename'); $filePathResolver->fileUrl($uploadableFile->getWrappedObject())->willReturn('default-locale-url'); $this->finishView($view, $form, array('remove_name' => 'remove')); expect($view->vars['translatable'])->toBe(true); expect($view->vars['not_translated'])->toBe(true); expect($view->vars['label_attr']['data-default-locale'])->toBe('en'); expect($view->vars['label_attr']['data-default-locale-value'])->toBe('default-locale-filename'); expect($view->vars['label_attr']['data-default-locale-url'])->toBe('default-locale-url'); expect($fileView->vars['value'])->toBe(null); expect($fileView->vars['data'])->toBe(null); expect($removeView->vars['checked'])->toBe(true); }
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)); }
/** * @param \FSi\DoctrineExtensions\Uploadable\File $file * @param null $prefix * @return string */ protected function generatePath(File $file, $prefix = null) { if (!isset($prefix) && isset($this->filePrefix)) { $prefix = $this->filePrefix; } if (isset($prefix)) { $prefix = trim($prefix, '/'); return $prefix . '/' . ltrim($file->getKey(), '/'); } return $file->getKey(); }
public function testFileIsAbleToReturnItsFilesystem() { $filesystem = $this->getFilesystemMock(); $file = new File('key', $filesystem); $this->assertSame($filesystem, $file->getFilesystem()); }
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)); }
/** * 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); } } }
/** * @param \FSi\DoctrineExtensions\Uploadable\File $file */ function it_generate_fsi_file_basename($file) { $file->getName()->shouldBeCalled()->willReturn('TestFolder/File/file.jpg'); $this->fileBasename($file)->shouldReturn('file.jpg'); }
function it_saves_file_and_calls_symfony_validator(ImageValidator $imageValidator, File $constraint, FSiFile $file) { $file->getContent()->willReturn('file content'); $imageValidator->validate(Argument::containingString('/tmp/'), $constraint)->shouldBeCalled(); $this->validate($file, $constraint); }
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(); }