Skip to content

Commit

Permalink
update MarshallerTest
Browse files Browse the repository at this point in the history
- separate method (_hasTranslations)
- delete all “pass by reference”
- simplify tests and remove all mocks
  • Loading branch information
Albert Cansado Solà authored and markstory committed Aug 7, 2016
1 parent 35cff0d commit 1ade959
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 319 deletions.
46 changes: 33 additions & 13 deletions src/ORM/Marshaller.php
Expand Up @@ -148,6 +148,9 @@ public function one(array $data, array $options = [])
}

$hasTranslations = $this->_hasTranslations($options);
if ($hasTranslations) {
$options = $this->_addTranslationsToFieldList($options);
}

$errors = $this->_validate($data, $options, true);
$properties = [];
Expand All @@ -169,7 +172,10 @@ public function one(array $data, array $options = [])
$converter = Type::build($columnType);
$value = $converter->marshal($value);
} elseif ($key === '_translations' && $hasTranslations) {
$value = $this->_mergeTranslations(null, $value, $errors, $options);
list($value, $translationsErrors) = $this->_mergeTranslations(null, $value, $options);
if (!empty($translationsErrors)) {
$errors += $translationsErrors;
}
}
$properties[$key] = $value;
}
Expand Down Expand Up @@ -454,48 +460,56 @@ protected function _loadBelongsToMany($assoc, $ids)
}

/**
* Call translation merge. Validations errors during merge will be added to `$errors` param
* Call translation merge
*
* @param \Cake\Datasource\EntityInterface $original The original entity
* @param array $data key value list of languages with fields to be merged into the translate entity
* @param array $errors array with entity errors
* @param array $options list of options
* @return array|null
* @return array
*/
protected function _mergeTranslations($original, array $data, array &$errors, array $options = [])
protected function _mergeTranslations($original, array $data, array $options = [])
{
$result = $this->_table->mergeTranslations($original, $data, $this, $options);

$errors = [];
if (is_array($result)) {
if ((bool)$result[1]) {
$errors['_translations'] = $result[1];
}
$result = $result[0];
}

return $result;
return [$result, $errors];
}

/**
* Return if table contains translate behavior or we specificate to use via `translations` options.
*
* In case that $options has `fieldList` option and `_translations` field is not present inside it, it will include
*
* ### Options:
*
* - translations: Set to false to disable translations
*
* @param array $options List of options
* @return bool
*/
protected function _hasTranslations(array &$options = [])
protected function _hasTranslations(array $options = [])
{
return ($this->_table->behaviors()->hasMethod('mergeTranslations') && (bool)$options['translations']);
}

/**
* Add `_translations` field to `fieldList` $options if it's not present inside
*
* @param array $options List of options
* @return array
*/
protected function _addTranslationsToFieldList(array $options = [])
{
$hasTranslations = ($this->_table->behaviors()->hasMethod('mergeTranslations') && (bool)$options['translations']);
if ($hasTranslations && !empty($options['fieldList']) && !in_array('_translations', $options['fieldList'])) {
if (!empty($options['fieldList']) && !in_array('_translations', $options['fieldList'])) {
array_push($options['fieldList'], '_translations');
}

return $hasTranslations;
return $options;
}

/**
Expand Down Expand Up @@ -553,6 +567,9 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
}

$hasTranslations = $this->_hasTranslations($options);
if ($hasTranslations) {
$options = $this->_addTranslationsToFieldList($options);
}

$errors = $this->_validate($data + $keys, $options, $isNew);
$schema = $this->_table->schema();
Expand Down Expand Up @@ -582,7 +599,10 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
continue;
}
} elseif ($key === '_translations' && $hasTranslations) {
$value = $this->_mergeTranslations($original, $value, $errors, $options);
list($value, $translationsErrors) = $this->_mergeTranslations($original, $value, $options);
if (!empty($translationsErrors)) {
$errors += $translationsErrors;
}
}

$properties[$key] = $value;
Expand Down

0 comments on commit 1ade959

Please sign in to comment.