Skip to content

Commit

Permalink
trappar#10 - add ability to skip not initialized collections
Browse files Browse the repository at this point in the history
  • Loading branch information
zhil committed May 30, 2017
1 parent 1abef57 commit 1c19eba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Trappar/AliceGenerator/FixtureGenerationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

class FixtureGenerationContext
{
/**
* @var int
*/
private $skipNotInitializedCollections = false;
/**
* @var int
*/
Expand Down Expand Up @@ -143,4 +147,22 @@ public function setSortResults($sortResults)

return $this;
}

/**
* @return int
*/
public function getSkipNotInitializedCollections(): int
{
return $this->skipNotInitializedCollections;
}

/**
* @param int $skipNotInitializedCollections
* @return self
*/
public function setSkipNotInitializedCollections(int $skipNotInitializedCollections)
{
$this->skipNotInitializedCollections = $skipNotInitializedCollections;
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public function handle(ValueContext $valueContext)
return false;
}

$valueContext->setValue($collection->toArray());
$fixturesGenerationContext = $valueContext->getValueVisitor()->getFixtureGenerationContext();
if($fixturesGenerationContext->getSkipNotInitializedCollections() && !$collection->isInitialized()) {
$valueContext->setValue([]);
} else {
$valueContext->setValue($collection->toArray());
}
$valueContext->getValueVisitor()->visitArray($valueContext);

return true;
Expand Down
18 changes: 18 additions & 0 deletions src/Trappar/AliceGenerator/ValueVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,22 @@ private function handlePersistedObject($object, $reference)
return true;
}
}

/**
* @return FixtureGenerationContext
*/
public function getFixtureGenerationContext(): FixtureGenerationContext
{
return $this->fixtureGenerationContext;
}

/**
* @param FixtureGenerationContext $fixtureGenerationContext
* @return self
*/
public function setFixtureGenerationContext(FixtureGenerationContext $fixtureGenerationContext)
{
$this->fixtureGenerationContext = $fixtureGenerationContext;
return $this;
}
}

0 comments on commit 1c19eba

Please sign in to comment.