Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Service/FixtureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,28 @@ private function addDataObjectManyManyFields(DataObject $dataObject): void
continue;
}

// Relationships are sometimes defined as ClassName.FieldName. Drop the .FieldName
$cleanRelationshipClassName = strtok($relationClassName, '.');

// Check to see if this class has requested that it not be included in relationship maps
$excludeClass = Config::inst()->get($cleanRelationshipClassName, 'exclude_from_fixture_relationships');

// Yup, exclude this class
if ($excludeClass) {
continue;
}

// Check to see if this particular relationship wants to be excluded
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
$dataObject->ClassName,
$relationFieldName
);

// Yup, exclude this relationship
if ($excludeRelationship) {
continue;
}

// TL;DR: many_many is really tough. Developers could choose to define it only in one direction, or in
// both directions, and they could choose to define it either with, or without dot notation in either
// direction
Expand Down Expand Up @@ -497,6 +519,17 @@ private function addDataObjectManyManyThroughFields(DataObject $dataObject): voi
continue;
}

// Check to see if this particular relationship wants to be excluded
$excludeRelationship = $this->relationshipManifest->shouldExcludeRelationship(
$dataObject->ClassName,
$relationFieldName
);

// Yup, exclude this relationship
if ($excludeRelationship) {
continue;
}

// This should always simply be defined as the class name (no dot notation)
$throughClass = $relationshipValue['through'];
$represented = false;
Expand Down
78 changes: 78 additions & 0 deletions tests/Service/FixtureServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,84 @@ public function testManyManyRelationship(): void
$this->assertSame($expected, $parsed);
}

public function testManyManyTagExcludeFromFixtureRelationships(): void
{
MockTag::config()->set('exclude_from_fixture_relationships', true);

$page = MockPage::create();
$page->Title = 'Page Excluding Tags';
$page->write();

$tag = MockTag::create();
$tag->Title = 'Excluded Tag';
$tag->write();

$page->Tags()->add($tag);

$service = new FixtureService();
$service->addDataObject($page);

$expected = [
MockPage::class => [
$page->ID => ['Title' => 'Page Excluding Tags'],
],
];

$this->assertSame($expected, Yaml::parse($service->outputFixture()));
}

public function testManyManyPageExcludedFromFixtureRelationships(): void
{
MockPage::config()->set('excluded_fixture_relationships', ['Tags']);

$page = MockPage::create();
$page->Title = 'Page Excluding Tags Relation';
$page->write();

$tag = MockTag::create();
$tag->Title = 'Excluded Via Relation';
$tag->write();

$page->Tags()->add($tag);

$service = new FixtureService();
$service->addDataObject($page);

$expected = [
MockPage::class => [
$page->ID => ['Title' => 'Page Excluding Tags Relation'],
],
];

$this->assertSame($expected, Yaml::parse($service->outputFixture()));
}

public function testManyManyThroughExcludedFixtureRelationships(): void
{
MockPage::config()->set('excluded_fixture_relationships', ['ThroughTargets']);

$page = MockPage::create();
$page->Title = 'Page Excluding Through';
$page->write();

$target = MockThroughTarget::create();
$target->Title = 'Excluded Through Target';
$target->write();

$page->ThroughTargets()->add($target);

$service = new FixtureService();
$service->addDataObject($page);

$expected = [
MockPage::class => [
$page->ID => ['Title' => 'Page Excluding Through'],
],
];

$this->assertSame($expected, Yaml::parse($service->outputFixture()));
}

public function testManyManyThroughRelationship(): void
{
$page = MockPage::create();
Expand Down
Loading