Skip to content

Commit 73c689b

Browse files
authored
Merge pull request #34 from Jianbinzhu/fix/add-exclude-check-for-many-many
Add exclude_from_fixture_relationships check in ManyManyThroughFields
2 parents e4d36c0 + f363d27 commit 73c689b

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/Service/FixtureService.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,28 @@ private function addDataObjectManyManyFields(DataObject $dataObject): void
419419
continue;
420420
}
421421

422+
// Relationships are sometimes defined as ClassName.FieldName. Drop the .FieldName
423+
$cleanRelationshipClassName = strtok($relationClassName, '.');
424+
425+
// Check to see if this class has requested that it not be included in relationship maps
426+
$excludeClass = Config::inst()->get($cleanRelationshipClassName, 'exclude_from_fixture_relationships');
427+
428+
// Yup, exclude this class
429+
if ($excludeClass) {
430+
continue;
431+
}
432+
433+
// Check to see if this particular relationship wants to be excluded
434+
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
435+
$dataObject->ClassName,
436+
$relationFieldName
437+
);
438+
439+
// Yup, exclude this relationship
440+
if ($excludeRelationship) {
441+
continue;
442+
}
443+
422444
// TL;DR: many_many is really tough. Developers could choose to define it only in one direction, or in
423445
// both directions, and they could choose to define it either with, or without dot notation in either
424446
// direction
@@ -497,6 +519,17 @@ private function addDataObjectManyManyThroughFields(DataObject $dataObject): voi
497519
continue;
498520
}
499521

522+
// Check to see if this particular relationship wants to be excluded
523+
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
524+
$dataObject->ClassName,
525+
$relationFieldName
526+
);
527+
528+
// Yup, exclude this relationship
529+
if ($excludeRelationship) {
530+
continue;
531+
}
532+
500533
// This should always simply be defined as the class name (no dot notation)
501534
$throughClass = $relationshipValue['through'];
502535
$represented = false;

tests/Service/FixtureServiceTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,84 @@ public function testManyManyRelationship(): void
172172
$this->assertSame($expected, $parsed);
173173
}
174174

175+
public function testManyManyTagExcludeFromFixtureRelationships(): void
176+
{
177+
MockTag::config()->set('exclude_from_fixture_relationships', true);
178+
179+
$page = MockPage::create();
180+
$page->Title = 'Page Excluding Tags';
181+
$page->write();
182+
183+
$tag = MockTag::create();
184+
$tag->Title = 'Excluded Tag';
185+
$tag->write();
186+
187+
$page->Tags()->add($tag);
188+
189+
$service = new FixtureService();
190+
$service->addDataObject($page);
191+
192+
$expected = [
193+
MockPage::class => [
194+
$page->ID => ['Title' => 'Page Excluding Tags'],
195+
],
196+
];
197+
198+
$this->assertSame($expected, Yaml::parse($service->outputFixture()));
199+
}
200+
201+
public function testManyManyPageExcludedFromFixtureRelationships(): void
202+
{
203+
MockPage::config()->set('excluded_fixture_relationships', ['Tags']);
204+
205+
$page = MockPage::create();
206+
$page->Title = 'Page Excluding Tags Relation';
207+
$page->write();
208+
209+
$tag = MockTag::create();
210+
$tag->Title = 'Excluded Via Relation';
211+
$tag->write();
212+
213+
$page->Tags()->add($tag);
214+
215+
$service = new FixtureService();
216+
$service->addDataObject($page);
217+
218+
$expected = [
219+
MockPage::class => [
220+
$page->ID => ['Title' => 'Page Excluding Tags Relation'],
221+
],
222+
];
223+
224+
$this->assertSame($expected, Yaml::parse($service->outputFixture()));
225+
}
226+
227+
public function testManyManyThroughExcludedFixtureRelationships(): void
228+
{
229+
MockPage::config()->set('excluded_fixture_relationships', ['ThroughTargets']);
230+
231+
$page = MockPage::create();
232+
$page->Title = 'Page Excluding Through';
233+
$page->write();
234+
235+
$target = MockThroughTarget::create();
236+
$target->Title = 'Excluded Through Target';
237+
$target->write();
238+
239+
$page->ThroughTargets()->add($target);
240+
241+
$service = new FixtureService();
242+
$service->addDataObject($page);
243+
244+
$expected = [
245+
MockPage::class => [
246+
$page->ID => ['Title' => 'Page Excluding Through'],
247+
],
248+
];
249+
250+
$this->assertSame($expected, Yaml::parse($service->outputFixture()));
251+
}
252+
175253
public function testManyManyThroughRelationship(): void
176254
{
177255
$page = MockPage::create();

0 commit comments

Comments
 (0)