Skip to content

Commit 1ade959

Browse files
Albert Cansado Solàmarkstory
authored andcommitted
update MarshallerTest
- separate method (_hasTranslations) - delete all “pass by reference” - simplify tests and remove all mocks
1 parent 35cff0d commit 1ade959

File tree

2 files changed

+262
-319
lines changed

2 files changed

+262
-319
lines changed

src/ORM/Marshaller.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public function one(array $data, array $options = [])
148148
}
149149

150150
$hasTranslations = $this->_hasTranslations($options);
151+
if ($hasTranslations) {
152+
$options = $this->_addTranslationsToFieldList($options);
153+
}
151154

152155
$errors = $this->_validate($data, $options, true);
153156
$properties = [];
@@ -169,7 +172,10 @@ public function one(array $data, array $options = [])
169172
$converter = Type::build($columnType);
170173
$value = $converter->marshal($value);
171174
} elseif ($key === '_translations' && $hasTranslations) {
172-
$value = $this->_mergeTranslations(null, $value, $errors, $options);
175+
list($value, $translationsErrors) = $this->_mergeTranslations(null, $value, $options);
176+
if (!empty($translationsErrors)) {
177+
$errors += $translationsErrors;
178+
}
173179
}
174180
$properties[$key] = $value;
175181
}
@@ -454,48 +460,56 @@ protected function _loadBelongsToMany($assoc, $ids)
454460
}
455461

456462
/**
457-
* Call translation merge. Validations errors during merge will be added to `$errors` param
463+
* Call translation merge
458464
*
459465
* @param \Cake\Datasource\EntityInterface $original The original entity
460466
* @param array $data key value list of languages with fields to be merged into the translate entity
461-
* @param array $errors array with entity errors
462467
* @param array $options list of options
463-
* @return array|null
468+
* @return array
464469
*/
465-
protected function _mergeTranslations($original, array $data, array &$errors, array $options = [])
470+
protected function _mergeTranslations($original, array $data, array $options = [])
466471
{
467472
$result = $this->_table->mergeTranslations($original, $data, $this, $options);
468473

474+
$errors = [];
469475
if (is_array($result)) {
470476
if ((bool)$result[1]) {
471477
$errors['_translations'] = $result[1];
472478
}
473479
$result = $result[0];
474480
}
475481

476-
return $result;
482+
return [$result, $errors];
477483
}
478484

479485
/**
480486
* Return if table contains translate behavior or we specificate to use via `translations` options.
481487
*
482-
* In case that $options has `fieldList` option and `_translations` field is not present inside it, it will include
483-
*
484488
* ### Options:
485489
*
486490
* - translations: Set to false to disable translations
487491
*
488492
* @param array $options List of options
489493
* @return bool
490494
*/
491-
protected function _hasTranslations(array &$options = [])
495+
protected function _hasTranslations(array $options = [])
496+
{
497+
return ($this->_table->behaviors()->hasMethod('mergeTranslations') && (bool)$options['translations']);
498+
}
499+
500+
/**
501+
* Add `_translations` field to `fieldList` $options if it's not present inside
502+
*
503+
* @param array $options List of options
504+
* @return array
505+
*/
506+
protected function _addTranslationsToFieldList(array $options = [])
492507
{
493-
$hasTranslations = ($this->_table->behaviors()->hasMethod('mergeTranslations') && (bool)$options['translations']);
494-
if ($hasTranslations && !empty($options['fieldList']) && !in_array('_translations', $options['fieldList'])) {
508+
if (!empty($options['fieldList']) && !in_array('_translations', $options['fieldList'])) {
495509
array_push($options['fieldList'], '_translations');
496510
}
497511

498-
return $hasTranslations;
512+
return $options;
499513
}
500514

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

555569
$hasTranslations = $this->_hasTranslations($options);
570+
if ($hasTranslations) {
571+
$options = $this->_addTranslationsToFieldList($options);
572+
}
556573

557574
$errors = $this->_validate($data + $keys, $options, $isNew);
558575
$schema = $this->_table->schema();
@@ -582,7 +599,10 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
582599
continue;
583600
}
584601
} elseif ($key === '_translations' && $hasTranslations) {
585-
$value = $this->_mergeTranslations($original, $value, $errors, $options);
602+
list($value, $translationsErrors) = $this->_mergeTranslations($original, $value, $options);
603+
if (!empty($translationsErrors)) {
604+
$errors += $translationsErrors;
605+
}
586606
}
587607

588608
$properties[$key] = $value;

0 commit comments

Comments
 (0)