public function testCheckDatabase()
 {
     $schema = $this->getMockBuilder('Doctrine\\Tests\\Mocks\\SchemaManagerMock')->disableOriginalConstructor()->getMock();
     $schema->expects($this->any())->method('listTableNames')->will($this->returnValue(array('oro_entity_config', 'oro_entity_config_field', 'oro_entity_config_value')));
     $this->em->getConnection()->getDriver()->setSchemaManager($schema);
     $this->assertTrue($this->configModelManager->checkDatabase());
 }
예제 #2
0
 /**
  * @dataProvider checkDatabaseProvider
  */
 public function testCheckDatabase(array $tables, $expectedResult)
 {
     $connection = $this->getMockBuilder('Doctrine\\DBAL\\Connection')->disableOriginalConstructor()->getMock();
     $connection->expects($this->once())->method('getConfiguration')->will($this->returnValue(new Configuration()));
     $connection->expects($this->once())->method('getDatabasePlatform')->will($this->returnValue(new MySqlPlatform()));
     $connection->expects($this->once())->method('getSchemaManager')->will($this->returnValue(new SchemaManagerMock($connection)));
     $connection->expects($this->once())->method('fetchAll')->will($this->returnValue($tables));
     $this->em->expects($this->once())->method('getConnection')->will($this->returnValue($connection));
     $this->assertEquals($expectedResult, $this->configModelManager->checkDatabase());
 }
예제 #3
0
 /**
  * @param      $scope
  * @param      $className
  * @param null $fieldName
  * @return ConfigIdInterface
  * @throws LogicException if a database is not synced
  * @throws RuntimeException if a model was not found
  */
 public function getId($scope, $className, $fieldName = null)
 {
     if ($fieldName) {
         // at first try to find a model in the local cache
         $configId = new FieldConfigId($scope, $className, $fieldName);
         $configKey = $this->buildConfigKey($configId);
         if (isset($this->localCache[$configKey])) {
             // if found, use field config id from the local cache
             $configId = $this->localCache[$configKey]->getId();
         } else {
             // next try to find a model in the cache
             $config = null !== $this->cache ? $this->cache->loadConfigFromCache($configId) : null;
             if ($config) {
                 // if found, use field config id from the cache
                 $configId = $config->getId();
                 $this->localCache[$configKey] = $config;
             } else {
                 // if a cached model was not found use the model manager to get field config id
                 if (!$this->modelManager->checkDatabase()) {
                     throw $this->createDatabaseNotSyncedException();
                 }
                 $fieldModel = $this->modelManager->getFieldModel($className, $fieldName);
                 $configId = new FieldConfigId($scope, $className, $fieldModel->getFieldName(), $fieldModel->getType());
             }
         }
         return $configId;
     } else {
         return new EntityConfigId($scope, $className);
     }
 }
예제 #4
0
 /**
  * @param callable $callback
  * @param string   $scope
  * @param string   $className
  * @param bool     $withHidden
  *
  * @return array
  */
 protected function mapFields($callback, $scope, $className, $withHidden)
 {
     $result = [];
     $fields = $this->cache->getFields($className);
     if (null !== $fields) {
         foreach ($fields as $field => $data) {
             if ($withHidden || !$data['h']) {
                 $result[] = $callback($scope, $className, $field, $data['t']);
             }
         }
     } elseif ($this->modelManager->checkDatabase()) {
         $models = $this->modelManager->getModels($className);
         $fields = [];
         /** @var FieldConfigModel $model */
         foreach ($models as $model) {
             $isHidden = $model->isHidden();
             $field = $model->getFieldName();
             $type = $model->getType();
             $fields[$field] = ['t' => $type, 'h' => $isHidden];
             if ($withHidden || !$isHidden) {
                 $result[] = $callback($scope, $className, $field, $type);
             }
         }
         $this->cache->saveFields($className, $fields);
     }
     return $result;
 }
 /**
  * @param ConfigIdInterface $configId
  * @throws RuntimeException
  * @throws LogicException
  * @return ConfigInterface
  */
 public function getConfig(ConfigIdInterface $configId)
 {
     if ($this->localCache->containsKey($configId->toString())) {
         return $this->localCache->get($configId->toString());
     }
     if (!$this->modelManager->checkDatabase()) {
         throw new LogicException('Database is not synced, if you use ConfigManager, when a db schema may be hasn\'t synced.' . ' check it by ConfigManager::modelManager::checkDatabase');
     }
     if (!$this->hasConfig($configId->getClassName())) {
         throw new RuntimeException(sprintf('Entity "%s" is not configurable', $configId->getClassName()));
     }
     $resultConfig = null !== $this->cache ? $this->cache->loadConfigFromCache($configId) : null;
     if (!$resultConfig) {
         $model = $this->modelManager->getModelByConfigId($configId);
         $config = new Config($this->getConfigIdByModel($model, $configId->getScope()));
         $config->setValues($model->toArray($configId->getScope()));
         if (null !== $this->cache) {
             $this->cache->putConfigInCache($config);
         }
         $resultConfig = $config;
     }
     //local cache
     $this->localCache->set($resultConfig->getId()->toString(), $resultConfig);
     //for calculate change set
     $this->originalConfigs->set($resultConfig->getId()->toString(), clone $resultConfig);
     return $resultConfig;
 }
예제 #6
0
 /**
  * @param      $scope
  * @param      $className
  * @param null $fieldName
  * @return ConfigIdInterface
  * @throws LogicException if a database is not synced
  * @throws RuntimeException if a model was not found
  */
 public function getId($scope, $className, $fieldName = null)
 {
     if ($fieldName) {
         $configId = new FieldConfigId($scope, $className, $fieldName);
         // try to find a model in the cache
         $config = $this->cache->getConfig($configId);
         if (!$config) {
             // if a cached model was not found use the model manager to get field config id
             if (!$this->modelManager->checkDatabase()) {
                 throw $this->createDatabaseNotSyncedException();
             }
             $fieldModel = $this->modelManager->getFieldModel($className, $fieldName);
             $configId = new FieldConfigId($scope, $className, $fieldModel->getFieldName(), $fieldModel->getType());
         }
         return $configId;
     } else {
         return new EntityConfigId($scope, $className);
     }
 }
예제 #7
0
 /**
  * Gets a list of ids for all configurable entities (if $className is not specified)
  * or all configurable fields of the given $className.
  *
  * @param string $scope
  * @param string|null $className
  * @param bool $withHidden Set true if you need ids of all configurable entities,
  *                                including entities marked as mode="hidden"
  * @return array
  */
 public function getIds($scope, $className = null, $withHidden = false)
 {
     if (!$this->modelManager->checkDatabase()) {
         return [];
     }
     $models = $this->modelManager->getModels($className, $withHidden);
     $ids = [];
     if ($className) {
         /** @var FieldConfigModel $model */
         foreach ($models as $model) {
             $ids[] = new FieldConfigId($scope, $className, $model->getFieldName(), $model->getType());
         }
     } else {
         /** @var EntityConfigModel $model */
         foreach ($models as $model) {
             $ids[] = new EntityConfigId($scope, $model->getClassName());
         }
     }
     return $ids;
 }
예제 #8
0
 /**
  * @param string $className
  * @param string $fieldName
  *
  * @return array|null ['i' => field_model_id, 'h' => is_hidden_model, 't' => field_type]
  */
 protected function findField($className, $fieldName)
 {
     $result = null;
     $fields = $this->cache->getFields($className);
     if (null === $fields && $this->modelManager->checkDatabase()) {
         $models = $this->modelManager->getModels($className);
         $fields = [];
         /** @var FieldConfigModel $model */
         foreach ($models as $model) {
             $isHidden = $model->isHidden();
             $field = $model->getFieldName();
             $type = $model->getType();
             $fields[$field] = ['i' => $model->getId(), 'h' => $isHidden, 't' => $type];
         }
         $this->cache->saveFields($className, $fields);
     }
     if (null !== $fields && isset($fields[$fieldName])) {
         $result = $fields[$fieldName];
     }
     return $result;
 }