public function testHowWellContainerIsInjected() { $repository = new Repository('test repo', array('filesystem' => '', 'storage_key_generator' => '')); self::$em->persist($repository); self::$em->flush(); self::$em->clear(); /* @var Repository $repository */ $repository = self::$em->getRepository(Repository::clazz())->find($repository->getId()); $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\ContainerInterface', Toolkit::getPropertyValue($repository, 'container')); }
public function testInterceptors() { $itc = new DummyInterceptor(); $interceptorsProvider = \Phake::mock('Modera\\FileRepositoryBundle\\Intercepting\\InterceptorsProviderInterface'); \Phake::when($interceptorsProvider)->getInterceptors($this->isInstanceOf(Repository::clazz()))->thenReturn([$itc]); $container = \Phake::mock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); \Phake::when($container)->get('modera_file_repository.intercepting.interceptors_provider')->thenReturn($interceptorsProvider); $repository = new Repository('foo', array('filesystem' => 'foo')); $repository->init($container); $splFile = new \SplFileInfo(__FILE__); $storedFile = \Phake::mock(StoredFile::clazz()); // --- $this->assertEquals(0, count($itc->beforePutInvocations)); $repository->beforePut($splFile); $this->assertEquals(1, count($itc->beforePutInvocations)); $this->assertSame($splFile, $itc->beforePutInvocations[0][0]); $this->assertSame($repository, $itc->beforePutInvocations[0][1]); // --- $receivedInterceptor = null; $repository->beforePut($splFile, function ($interceptor) use(&$receivedInterceptor) { $receivedInterceptor = $interceptor; return false; }); $this->assertSame($itc, $receivedInterceptor); $this->assertEquals(1, count($itc->beforePutInvocations)); // --- $this->assertEquals(0, count($itc->onPutInvocations)); $repository->onPut($storedFile, $splFile); $this->assertEquals(1, count($itc->onPutInvocations)); $this->assertSame($storedFile, $itc->onPutInvocations[0][0]); $this->assertSame($splFile, $itc->onPutInvocations[0][1]); $this->assertSame($repository, $itc->onPutInvocations[0][2]); // --- $receivedInterceptor = null; $repository->onPut($storedFile, $splFile, function ($interceptor) use(&$receivedInterceptor) { $receivedInterceptor = $interceptor; return false; }); $this->assertSame($itc, $receivedInterceptor); $this->assertEquals(1, count($itc->onPutInvocations)); // --- $this->assertEquals(0, count($itc->afterPutInvocations)); $repository->afterPut($storedFile, $splFile); $this->assertEquals(1, count($itc->afterPutInvocations)); $this->assertSame($storedFile, $itc->afterPutInvocations[0][0]); $this->assertSame($splFile, $itc->afterPutInvocations[0][1]); $this->assertSame($repository, $itc->afterPutInvocations[0][2]); $receivedInterceptor = null; $repository->afterPut($storedFile, $splFile, function ($interceptor) use(&$receivedInterceptor) { $receivedInterceptor = $interceptor; return false; }); $this->assertSame($itc, $receivedInterceptor); $this->assertEquals(1, count($itc->afterPutInvocations)); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { /* @var EntityManager $em */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $rows = array(); foreach ($em->getRepository(Repository::clazz())->findAll() as $repository) { /* @var Repository $repository */ $config = $repository->getConfig(); $rows[] = array($repository->getId(), $repository->getName(), $repository->getLabel() ? $repository->getLabel() : '-', $config['filesystem'], isset($config['overwrite_files']) ? $config['overwrite_files'] : false, $config['storage_key_generator']); } /* @var TableHelper $table */ $table = $this->getHelperSet()->get('table'); $table->setHeaders(array('#', 'Name', 'Label', 'Filesystem', 'Overwrite files', 'Storage key generator'))->setRows($rows); $table->render($output); }
public function testGetInterceptors() { $wannabeInterceptor = new \stdClass(); $anotherWannabeInterceptor = new \stdClass(); $container = \Phake::mock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); \Phake::when($container)->get('modera_file_repository.validation.file_properties_validation_interceptor')->thenReturn($wannabeInterceptor); \Phake::when($container)->get('foo_interceptor')->thenReturn($anotherWannabeInterceptor); $repository = \Phake::mock(Repository::clazz()); $provider = new DefaultInterceptorsProvider($container); $result = $provider->getInterceptors($repository); $this->assertEquals(1, count($result)); $this->assertSame($wannabeInterceptor, $result[0]); // and now with a "interceptors" config: \Phake::when($repository)->getConfig()->thenReturn(array('interceptors' => array('foo_interceptor'))); $result = $provider->getInterceptors($repository); $this->assertEquals(2, count($result)); $this->assertSame($wannabeInterceptor, $result[0]); $this->assertSame($anotherWannabeInterceptor, $result[1]); }
/** * {@inheritdoc} */ public static function doTearDownAfterClass() { self::$st->dropSchema(array(self::$em->getClassMetadata(Repository::clazz()), self::$em->getClassMetadata(StoredFile::clazz()))); }
public function testHowWellItWorks() { /* @var FileRepository $fr */ $fr = self::$container->get('modera_file_repository.repository.file_repository'); $this->assertNull($fr->getRepository('dummy_repository')); $repositoryConfig = array('storage_key_generator' => 'modera_file_repository.repository.uniqid_key_generator', 'filesystem' => 'dummy_tmp_fs'); $this->assertFalse($fr->repositoryExists('dummy_repository')); $repository = $fr->createRepository('dummy_repository', $repositoryConfig, 'My dummy repository'); $this->assertTrue($fr->repositoryExists('dummy_repository')); $this->assertInstanceOf(Repository::clazz(), $repository); $this->assertNotNull($repository->getId()); $this->assertEquals('dummy_repository', $repository->getName()); $this->assertEquals('My dummy repository', $repository->getLabel()); $this->assertSame($repositoryConfig, $repository->getConfig()); $this->assertInstanceOf('Symfony\\Component\\DependencyInjection\\ContainerInterface', Toolkit::getPropertyValue($repository, 'container')); // --- $fileContents = 'foo contents'; $filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'our-dummy-file.txt'; file_put_contents($filePath, $fileContents); $file = new File($filePath); $storedFile = $fr->put($repository->getName(), $file, array()); self::$em->clear(); // this way we will make sure that data is actually persisted in database /* @var StoredFile $storedFile */ $storedFile = self::$em->find(StoredFile::clazz(), $storedFile->getId()); $this->assertInstanceOf(StoredFile::clazz(), $storedFile); $this->assertNotNull($storedFile->getId()); $this->assertNotNull($storedFile->getStorageKey()); $this->assertEquals('our-dummy-file.txt', $storedFile->getFilename()); $this->assertNotNull($storedFile->getCreatedAt()); $this->assertEquals('txt', $storedFile->getExtension()); $this->assertEquals('text/plain', $storedFile->getMimeType()); $this->assertSame($repository->getId(), $storedFile->getRepository()->getId()); $this->assertEquals($fileContents, $storedFile->getContents()); $this->assertTrue('' != $storedFile->getChecksum()); $this->assertEquals($file->getSize(), $storedFile->getSize()); // --- $storedFileData = array('id' => $storedFile->getId(), 'storageKey' => $storedFile->getStorageKey(), 'filename' => $storedFile->getFilename()); $fileContents = 'bar contents'; $filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bar-dummy-file.txt'; file_put_contents($filePath, $fileContents); $file = new File($filePath); $storedFile = $fr->put($repository->getName(), $file, array()); self::$em->clear(); // this way we will make sure that data is actually persisted in database /* @var StoredFile $storedFile */ $storedFile = self::$em->find(StoredFile::clazz(), $storedFile->getId()); $this->assertNotEquals($storedFileData['id'], $storedFile->getId()); $this->assertNotEquals($storedFileData['storageKey'], $storedFile->getStorageKey()); $this->assertNotEquals($storedFileData['filename'], $storedFile->getFilename()); // --- $repositoryConfig['overwrite_files'] = true; $repository = $storedFile->getRepository(); $repository->setConfig($repositoryConfig); self::$em->persist($repository); self::$em->flush(); $storedFileData = array('id' => $storedFile->getId(), 'storageKey' => $storedFile->getStorageKey(), 'filename' => $storedFile->getFilename()); $storedFile = $fr->put($repository->getName(), $file, array()); self::$em->clear(); // this way we will make sure that data is actually persisted in database /* @var StoredFile $storedFile */ $storedFile = self::$em->find(StoredFile::clazz(), $storedFile->getId()); $this->assertEquals($storedFileData['id'], $storedFile->getId()); $this->assertEquals($storedFileData['storageKey'], $storedFile->getStorageKey()); $this->assertEquals($storedFileData['filename'], $storedFile->getFilename()); // --- $fs = $storedFile->getRepository()->getFilesystem(); $this->assertTrue($fs->has($storedFile->getStorageKey())); self::$em->remove($storedFile); self::$em->flush(); $this->assertFalse($fs->has($storedFile->getStorageKey())); }
/** * @param string $name * * @return bool */ public function repositoryExists($name) { $q = $this->em->createQuery(sprintf('SELECT COUNT(e.id) FROM %s e WHERE e.name = ?0', Repository::clazz())); $q->setParameter(0, $name); return $q->getSingleScalarResult() != 0; }