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'));
 }
예제 #2
0
 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()));
 }
예제 #3
0
 /**
  * @Remote
  */
 public function batchUpdateAction(array $params)
 {
     $config = $this->getPreparedConfig();
     try {
         $persistenceHandler = $config['batch_update_entities_handler'];
         $dataMapper = $config['map_data_on_update'];
         $validator = $config['updated_entity_validator'];
         $this->interceptAction('batchUpdate', $params);
         if (isset($params['queries']) && is_array($params['queries']) && isset($params['record']) && is_array($params['record'])) {
             if (!isset($params['record'])) {
                 $e = new BadRequestException("'/record' hasn't been provided");
                 $e->setParams($params);
                 $e->setPath('/record');
                 throw $e;
             }
             $entities = array();
             foreach ($params['queries'] as $query) {
                 $entities = array_merge($entities, $this->getPersistenceHandler()->query($config['entity'], $query));
             }
             $errors = array();
             $operationResult = null;
             foreach ($entities as $entity) {
                 $dataMapper($params['record'], $entity, $this->getDataMapper(), $this->container);
                 if ($validator) {
                     /* @var ValidationResult $validationResult */
                     $validationResult = $validator($params, $entity, $this->getEntityValidator(), $config, $this->container);
                     if ($validationResult->hasErrors()) {
                         $pkFields = $this->getPersistenceHandler()->resolveEntityPrimaryKeyFields($config['entity']);
                         $ids = array();
                         foreach ($pkFields as $fieldName) {
                             $ids[$fieldName] = Toolkit::getPropertyValue($entity, $fieldName);
                         }
                         $errors[] = array('id' => $ids, 'errors' => $validationResult->toArray());
                     }
                 }
             }
             if (count($errors) == 0) {
                 $operationResult = $persistenceHandler($entities, $params, $this->getPersistenceHandler(), $this->container);
                 return array_merge($operationResult->toArray($this->getModelManager()), array('success' => true));
             } else {
                 return array('success' => false, 'errors' => $errors);
             }
         } elseif (isset($params['records']) && is_array($params['records'])) {
             $entities = array();
             $errors = array();
             foreach ($params['records'] as $recordParams) {
                 $missingPkFields = array();
                 $query = array();
                 foreach ($this->getPersistenceHandler()->resolveEntityPrimaryKeyFields($config['entity']) as $fieldName) {
                     if (isset($recordParams[$fieldName])) {
                         $query[] = array('property' => $fieldName, 'value' => 'eq:' . $recordParams[$fieldName]);
                     } else {
                         $missingPkFields[] = $fieldName;
                     }
                 }
                 if (count($missingPkFields) == 0) {
                     $entity = $this->getPersistenceHandler()->query($config['entity'], array('filter' => $query));
                     $this->validateResultHasExactlyOneEntity($entity, $params);
                     $entity = $entity[0];
                     $entities[] = $entity;
                     $dataMapper($recordParams, $entity, $this->getDataMapper(), $this->container);
                     if ($validator) {
                         /* @var ValidationResult $validationResult */
                         $validationResult = $validator($params, $entity, $this->getEntityValidator(), $config, $this->container);
                         if ($validationResult->hasErrors()) {
                             $pkFields = $this->getPersistenceHandler()->resolveEntityPrimaryKeyFields($config['entity']);
                             $ids = array();
                             foreach ($pkFields as $fieldName) {
                                 $ids[$fieldName] = Toolkit::getPropertyValue($entity, $fieldName);
                             }
                             $errors[] = array('id' => $ids, 'errors' => $validationResult->toArray());
                         }
                     }
                 }
             }
             if (count($errors) == 0) {
                 $operationResult = $persistenceHandler($entities, $params, $this->getPersistenceHandler(), $this->container);
                 return array_merge($operationResult->toArray($this->getModelManager()), array('success' => true));
             } else {
                 return array('success' => false, 'errors' => $errors);
             }
         } else {
             $e = new BadRequestException("Invalid request structure. Valid request would either contain 'queries' and 'record' or 'records' keys.");
             $e->setPath($params);
             $e->setPath('/');
             throw $e;
         }
     } catch (\Exception $e) {
         return $this->createExceptionResponse($e, ExceptionHandlerInterface::OPERATION_UPDATE);
     }
 }