/** * {@inheritdoc} */ public function remove(File $file) : bool { $key = $file->getKey(); if ($this->filesystem->has($key)) { return $this->filesystem->delete($key); } return true; }
/** * {@inheritdoc} */ public function setFile(File $file) { // "updating" or "replacing" an already existing file is not allowed if (!$file->isTmpFile()) { throw new \RuntimeException(sprintf('Updating or replacing an existing physical file is not allowed. You should rather create a new Attachment entity.')); } $this->file = $file; return $this; }
/** * Test that a base64 encoded string is returned for the specified pathname. */ public function testCreateUploadThumbnailReturnBase64EncodedString() { $mockEntityManager = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $configuration = new TestFileStorageConfiguration(); $twigExtension = new ReskumeFileBundleExtension($configuration, $mockEntityManager); $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test_image.jpeg'); $this->assertEquals(ThumbnailHelper::getBase64Thumb(new HttpFile(TestPathProvider::getPath() . '/Fixtures/files/thumbs/thumb_test_image.jpeg')), $twigExtension->createThumbnailFunction($file)); $this->assertEquals(ThumbnailHelper::getBase64Thumb(new HttpFile(TestPathProvider::getPath() . '/Fixtures/files/thumbs/thumb_test_image.jpeg')), $twigExtension->createThumbnailFunction($file, 'base64')); }
/** * Test that MIME types that are defined as "not allowed" in the upload configuration class trigger a validation * error. */ public function testInvalidUploadConfigMimeTypeNotAllowed() { $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test_image.bmp')->setOrigFilename('test_image.jpeg')->setPath('/test/storage/path')->setFileMetadata(); $attachment = new Attachment(); $attachment->setFile($file)->setDescription('This is a test description.'); $constraintViolationBuilder = $this->getMockBuilder('Symfony\\Component\\Validator\\Violation\\ConstraintViolationBuilder')->disableOriginalConstructor()->getMock(); $mockContext = $this->getMockBuilder('Symfony\\Component\\Validator\\Context\\ExecutionContextInterface')->disableOriginalConstructor()->getMock(); $mockContext->expects($this->once())->method('buildViolation')->willReturn($constraintViolationBuilder); $constraintViolationBuilder->expects($this->any())->method('setParameter')->willReturnSelf(); $constraintViolationBuilder->expects($this->once())->method('setCode')->willReturnSelf(); $constraintViolationBuilder->expects($this->once())->method('addViolation'); $validator = static::$kernel->getContainer()->get('reskume_file.validator.file_attachment'); $validator->initialize($mockContext); $constraint = new FileConstraint(); $constraint->mimeTypes = array('image/jpeg'); $validator->validate($attachment, $constraint); }
/** * Test removing file from storage backend. */ public function testRemoveFile() { $container = static::$kernel->getContainer(); $fileStorageConfiguration = $container->get('reskume_file.configuration.file_storage_configuration'); $fileStorage = $fileStorageConfiguration->createFileStorage($this->em); // clean and copy files for testing purposes $filesystem = new Filesystem(); $this->cleanDir(TestPathProvider::getPath() . '/App/tmp'); // clean 'tmp' dir $filesystem->copy(TestPathProvider::getPath() . '/Fixtures/files/test_image.jpeg', TestPathProvider::getPath() . '/App/tmp/uploadFiles/test_image_1.jpeg'); $file = new File(); $file->setOrigFilename('test_image_1.jpeg')->setPath('test/images')->setTmpPathname(TestPathProvider::getPath() . '/App/tmp/uploadFiles/test_image_1.jpeg')->createKey()->setFileMetadata(); $this->em->persist($file); $this->em->flush(); // file storage persist is automatically called $this->em->clear(); // get file via storage backend $file = $fileStorage->getFile($file->getKey()); $this->em->remove($file); $this->em->flush(); $this->expectException('League\\Flysystem\\FileNotFoundException'); $fileStorage->getContent($file); }
/** * @return File */ public function create() { $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test.txt')->setOrigFilename('test.txt')->setPath('/test/storage/path')->setFileMetadata(); return $file; }
/** * Test that key string is created. */ public function testCreateKey() { $file = new File(); $file->setTmpPathname(TestPathProvider::getPath() . '/Fixtures/files/test.txt')->setFileMetadata()->setPath('/test/path')->createKey(); $this->assertEquals(1, preg_match('/^(\\/\\w+)+\\.[a-zA-Z]+$/', $file->getKey())); }