Skip to content

Commit

Permalink
Merge pull request #215 from vierge-noire/main
Browse files Browse the repository at this point in the history
Port main to cake5 branch
  • Loading branch information
pabloelcolombiano authored Aug 22, 2023
2 parents 5f7473f + f3d7fec commit 63ba5eb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
9 changes: 7 additions & 2 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ overwrite the value of `key2` only and keep the default value of `key1` as follo
```php
use App\Test\Factory\ArticleFactory;
...
$article = ArticleFactory::make(['array_field.key2' => 'newValue'])->persist();
$article = ArticleFactory::make(['array_field.key2' => 'newValue'])->getEntity();
// or
$article = ArticleFactory::make()->setField('array_field.key2', 'newValue')->persist();
$article = ArticleFactory::make([
'array_field.key1' => 'foo',
'array_field.key2' => 'bar',
])->getEntity();
// or
$article = ArticleFactory::make()->setField('array_field.key2', 'newValue')->getEntity();
```
4 changes: 2 additions & 2 deletions src/Factory/DataCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ private function castArrayNotation(EntityInterface $entity, array $data): array
}
$subData = Hash::expand([$key => $value]);
$rootKey = array_key_first($subData);
$entityValue = $entity->get($rootKey) ?? [];
$entityValue = $entityValue ?? ($entity->get($rootKey) ?? []);
if (!is_array($entityValue)) {
throw new FixtureFactoryException(
"Value $entityValue cannot be merged with array notation $key => $value"
);
}
$data[$rootKey] = array_replace_recursive($entityValue, $subData[$rootKey]);
$data[$rootKey] = $entityValue = array_replace_recursive($entityValue, $subData[$rootKey]);
unset($data[$key]);
}

Expand Down
67 changes: 67 additions & 0 deletions tests/TestCase/Factory/BaseFactoryArrayNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,53 @@ public function testBaseFactoryArrayNotation_overwrite_one_field()
$this->assertNull($author->get('json_field.subField1'));
}

public function testBaseFactoryArrayNotation_OverwriteMultipleSelectedNestedFields()
{
$author = AuthorFactory::make([
'json_field.subField1' => 'newVal1',
'json_field.subField2' => 'newVal2',
])->getEntity();

$expectedValue = AuthorFactory::JSON_FIELD_DEFAULT_VALUE;
$expectedValue['subField1'] = 'newVal1';
$expectedValue['subField2'] = 'newVal2';

$this->assertSame($expectedValue, $author->json_field);
$this->assertNull($author->get('json_field.subField1'));
}

public function testBaseFactoryArrayNotation_OverwriteMultipleSelected_MultiplesNestedFields()
{
$author = AuthorFactory::make([
'json_field.subField1' => 'newVal11',
'json_field2.subField1' => 'newVal21',
'json_field2.subField2' => 'newVal22',
])->getEntity();

$this->assertSame('newVal11', $author['json_field']['subField1']);
$this->assertSame(AuthorFactory::JSON_FIELD_DEFAULT_VALUE['subField2'], $author['json_field']['subField2']);
$this->assertSame('newVal21', $author['json_field2']['subField1']);
$this->assertSame('newVal22', $author['json_field2']['subField2']);
$this->assertNull($author->get('json_field.subField1'));
$this->assertNull($author->get('json_field.subField2'));
$this->assertNull($author->get('json_field2.subField1'));
$this->assertNull($author->get('json_field2.subField2'));
}

public function testBaseFactoryArrayNotation_OverwriteMultipleSelectedNestedFields_On_Mae_And_SetField()
{
$author = AuthorFactory::make(['json_field.subField1' => 'newVal1'])
->setField('json_field.subField2', 'newVal2')
->getEntity();

$expectedValue = AuthorFactory::JSON_FIELD_DEFAULT_VALUE;
$expectedValue['subField1'] = 'newVal1';
$expectedValue['subField2'] = 'newVal2';

$this->assertSame($expectedValue, $author->json_field);
$this->assertNull($author->get('json_field.subField1'));
}

public function testBaseFactoryArrayNotation_overwrite_one_field_with_set_field()
{
$author = AuthorFactory::make()
Expand Down Expand Up @@ -96,6 +143,26 @@ public function testBaseFactoryArrayNotation_overwrite_one_field_with_deep_assoc
$this->assertSame($expectedValue, $author->json_field);
}

public function testBaseFactoryArrayNotation_overwrite_one_field_with_deep_association_inline()
{
$author = AuthorFactory::make([
'json_field.subField1.subSubField1' => 'subSubValue1',
'json_field.subField1.subSubField2' => 'subSubValue2',
])
->setField('json_field.subField1.subSubField2', 'blah')
->getEntity();

$expectedValue = [
'subField1' => [
'subSubField1' => 'subSubValue1',
'subSubField2' => 'blah',
],
'subField2' => 'subFieldValue2',
];

$this->assertSame($expectedValue, $author->json_field);
}

public function testBaseFactoryArrayNotation_overwrite_one_field_with_set_field_on_association()
{
$article = ArticleFactory::make()->withAuthors(
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase/Factory/BaseFactoryAssociationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,9 @@ public function testReproduceIssue84WithArticlesAuthors()
foreach ($articles as $article) {
$this->assertSame(5, count($article->articles_authors));
foreach ($article->articles_authors as $aa) {
$this->assertSame('Foo', $aa->author->biography);
/** @var \TestApp\Model\Entity\Author $author */
$author = $aa->get('author');
$this->assertSame('Foo', $author->biography);
}
$this->assertSame(1, count($article->bills));
}
Expand Down

0 comments on commit 63ba5eb

Please sign in to comment.