Skip to content

Commit fa35d86

Browse files
committed
fix empty deep relationship issue
1 parent 3c51971 commit fa35d86

File tree

2 files changed

+19
-44
lines changed

2 files changed

+19
-44
lines changed

src/Translator/Transformers/CollectionTransformer.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,13 @@ protected function parseRelationsTranslations(array $relations, Collection $tran
131131
->groupBy(fn ($item) => $this->groupByKey($item))
132132
);
133133
}
134-
} else {
134+
} elseif(!is_null($relation)) {
135135
$relationTranslations->push($translations->get($this->groupByKey($relation)));
136-
137-
if ($relation === null) {
138-
dd('Relation is null', $relation, $translations);
139-
}
140-
141-
if (array_filter($relation->getRelations())) {
142-
//dd($relation);
136+
137+
if ($deepRelations = array_filter($relation->getRelations())) {
143138

144139
$relationTranslations = $relationTranslations->merge(
145-
$this->parseRelationsTranslations($relation->getRelations(), $translations)
140+
$this->parseRelationsTranslations($deepRelations, $translations)
146141
);
147142
}
148143
}

tests/EndToEndTest.php

+15-35
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ public function testTranslationsForCollectionRelationships()
9393
public function testTranslationsForCollectionDeepRelationships()
9494
{
9595
$category = Category::with(['content', 'content.links', 'content.author', 'content.author.posts'])->get();
96-
97-
Post::all()->each(function ($post) {
98-
$post->delete();
99-
});
100-
$translated = Translator::translate($category->load())->first();
96+
$translated = Translator::translate($category)->first();
10197

10298
$this->assertCount(2, $translated->content);
10399
$this->assertCount(2, $translated->content[0]->links);
@@ -116,7 +112,7 @@ public function testTranslationsForModelDeepRelationships()
116112

117113
$translated = Translator::translate($category);
118114

119-
$this->assertCount(3, $translated->content);
115+
$this->assertCount(4, $translated->content);
120116
$this->assertCount(2, $translated->content[0]->links);
121117
$this->assertEquals('This is what we shall do', $translated->content[0]->trans('en_GB', 'title'));
122118
$this->assertEquals('This is a link', $translated->content[0]->links[0]->trans('en_GB', 'title'));
@@ -126,35 +122,17 @@ public function testTranslationsForModelDeepRelationships()
126122

127123
public function testTranslationsForModelDeepRelationshipsWithMissingRelation()
128124
{
129-
$reviewer = new Reviewer;
130-
131-
Link::find(2)->reviewer()->save($reviewer);
132-
133-
$coll = Author::with([
134-
'content',
135-
// 'content.category',
136-
'content.links',
137-
'content.links.reviewer',
138-
'content.links.content'
139-
140-
])->paginate(1);
141-
142-
dd($coll);
143-
144-
Translator::translate($coll);
145-
// $collection = Post::with(['category', 'category.content', 'category.content.links', 'category.content.author'])->paginate(1);
146-
//
147-
// dd($collection);
148-
// $translated = Translator::translate($collection);
149-
150-
// dd($translated->getRelations());
151-
152-
// $this->assertCount(3, $translated->content);
153-
// $this->assertCount(2, $translated->content[0]->links);
154-
// $this->assertEquals('This is what we shall do', $translated->content[0]->trans('en_GB', 'title'));
155-
// $this->assertEquals('This is a link', $translated->content[0]->links[0]->trans('en_GB', 'title'));
156-
// $this->assertEquals('Author 2 summary', $translated->content[0]->author->trans('en_GB', 'summary'));
157-
// $this->assertEquals('This is a title 3', $translated->content[0]->author->posts[0]->trans('en_GB', 'title'));
125+
$links = Link::with(['content', 'content.links.reviewer', 'content.author'])->get();
126+
$translated = Translator::translate($links);
127+
128+
$this->assertCount(11, $translated);
129+
$link = $translated->first();
130+
$this->assertCount(2, $link->content->links);
131+
$this->assertEquals('This is a link', $link->trans('en_GB', 'title'));
132+
$this->assertEquals('This is what we shall do', $link->content->trans('en_GB', 'title'));
133+
$this->assertEquals('This is a link',$link->content->links[0]->trans('en_GB', 'title'));
134+
$this->assertEquals('Author 1 summary', $link->content->author->trans('en_GB', 'summary'));
135+
$this->assertNull($link->content->author->posts[0]->trans('en_GB', 'title'));
158136
}
159137

160138
/**
@@ -198,6 +176,7 @@ private function createContent()
198176
$this->author2->content()->save($this->content3 = $this->category2->content()->save(new Content));
199177
$this->author2->content()->save($this->content4 = $this->category2->content()->save(new Content));
200178
$this->author2->content()->save($this->content5 = $this->category2->content()->save(new Content));
179+
$this->content6 = $this->category2->content()->save(new Content);
201180
}
202181

203182
/**
@@ -215,6 +194,7 @@ private function createLinks()
215194
$this->content4->links()->save(new Link);
216195
$this->content5->links()->save(new Link);
217196
$this->content5->links()->save(new Link);
197+
$this->content6->links()->save(new Link);
218198
}
219199

220200
/**

0 commit comments

Comments
 (0)