Skip to content

Commit

Permalink
Doctrine Object : Move Suite Parent Id to object
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Feb 8, 2024
1 parent 5a2004a commit 0b7dcc8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
51 changes: 40 additions & 11 deletions src/Entity/Suite.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Suite
#[ORM\Column(name: 'hasTests', nullable: true)]
private ?bool $hasTests = null;

#[ORM\Column(nullable: true)]
private ?int $parent_id = null;
#[ORM\ManyToOne(inversedBy: 'suites')]
private ?Suite $parent = null;

#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTime $insertion_date = null;
Expand All @@ -74,12 +74,14 @@ class Suite
#[ORM\OneToMany(mappedBy: 'suite', targetEntity: Test::class, orphanRemoval: true)]
private Collection $tests;

/** @var array<int, Suite> */
private array $suites = [];
/** @var Collection<int, Suite> */
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: Suite::class)]
private Collection $suites;

public function __construct()
{
$this->tests = new ArrayCollection();
$this->suites = new ArrayCollection();
}

public function getId(): ?int
Expand Down Expand Up @@ -286,14 +288,14 @@ public function setHasTests(?bool $hasTests): static
return $this;
}

public function getParentId(): ?int
public function getParent(): ?Suite
{
return $this->parent_id;
return $this->parent;
}

public function setParentId(?int $parent_id): static
public function setParent(?Suite $parent): static
{
$this->parent_id = $parent_id;
$this->parent = $parent;

return $this;
}
Expand Down Expand Up @@ -356,9 +358,9 @@ public function removeTest(Test $test): static
}

/**
* @return array<int, Suite>
* @return Collection<int, Suite>
*/
public function getSuites(): array
public function getSuites(): Collection
{
return $this->suites;
}
Expand All @@ -368,7 +370,34 @@ public function getSuites(): array
*/
public function setSuites(array $suites): static
{
$this->suites = $suites;
foreach ($this->suites as $suite) {
$this->removeSuite($suite);
}
foreach ($suites as $suite) {
$this->addSuite($suite);
}

return $this;
}

public function addSuite(Suite $suite): static
{
if (!$this->suites->contains($suite)) {
$this->suites->add($suite);
$suite->setParent($this);
}

return $this;
}

public function removeSuite(Suite $suite): static
{
if ($this->suites->removeElement($suite)) {
// set the owning side to null (unless already changed)
if ($suite->getParent() === $this) {
$suite->setParent(null);
}
}

return $this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Service/ReportMochaImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function import(
return $execution;
}

private function insertExecutionSuite(Execution $execution, \stdClass $suite, ?int $parentSuiteId = null): void
private function insertExecutionSuite(Execution $execution, \stdClass $suite, ?Suite $parentSuite = null): void
{
$executionSuite = new Suite();
$executionSuite
Expand All @@ -96,7 +96,7 @@ private function insertExecutionSuite(Execution $execution, \stdClass $suite, ?i
->setTotalPending(count($suite->pending))
->setTotalPasses(count($suite->passes))
->setTotalFailures(count($suite->failures))
->setParentId($parentSuiteId)
->setParent($parentSuite)
->setCampaign($this->extractDataFromFile($suite->file, 'campaign'))
->setFile($this->extractDataFromFile($suite->file, 'file'))
->setInsertionDate(new \DateTime())
Expand Down Expand Up @@ -130,9 +130,9 @@ private function insertExecutionSuite(Execution $execution, \stdClass $suite, ?i

// Insert children suites
foreach ($suite->suites as $suiteChildren) {
$this->insertExecutionSuite($execution, $suiteChildren, $executionSuite->getId());
$this->insertExecutionSuite($execution, $suiteChildren, $executionSuite);
}
if (!$parentSuiteId) {
if (!$parentSuite) {
$this->entityManager->clear();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Service/ReportPlaywrightImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function import(
return $execution;
}

protected function insertExecutionSuite(Execution $execution, \stdClass $suite, ?int $parentSuiteId = null): Suite
protected function insertExecutionSuite(Execution $execution, \stdClass $suite): Suite
{
$executionSuite = new Suite();
$executionSuite
Expand All @@ -94,7 +94,7 @@ protected function insertExecutionSuite(Execution $execution, \stdClass $suite,
->setTitle($suite->title)
->setHasSuites(false)
->setHasTests(!empty($suite->specs))
->setParentId($parentSuiteId)
->setParent(null)
->setCampaign($this->extractDataFromFile('/' . $suite->file, 'campaign'))
->setFile($this->extractDataFromFile('/' . $suite->file, 'file'))
->setInsertionDate(new \DateTime())
Expand Down
13 changes: 7 additions & 6 deletions src/Service/ReportSuiteBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\Execution;
use App\Entity\Suite;
use App\Entity\Test;
use Doctrine\Common\Collections\Collection;

class ReportSuiteBuilder
{
Expand Down Expand Up @@ -77,7 +78,7 @@ public function build(Execution $execution): self
$hasOnlyOneMainSuite = false;
$mainSuiteId = null;
foreach ($this->suites as $suite) {
if ($suite->getParentId()) {
if ($suite->getParent()) {
continue;
}

Expand Down Expand Up @@ -157,7 +158,7 @@ private function formatSuite(Suite $suite): array
'totalFailures' => $suite->getTotalFailures(),
'hasSuites' => $suite->getHasSuites() ? 1 : 0,
'hasTests' => $suite->getHasTests() ? 1 : 0,
'parent_id' => $suite->getParentId(),
'parent_id' => $suite->getParent()?->getId(),
'insertion_date' => $suite->getInsertionDate()
->setTimezone(new \DateTimeZone('-01:00'))
->format('Y-m-d H:i:s'),
Expand Down Expand Up @@ -237,7 +238,7 @@ private function buildTree(?int $parentId, bool $isRoot): array
&& $suite->getId() !== $this->filterSuiteId) {
continue;
}
if ($suite->getParentId() !== $parentId) {
if ($suite->getParent()?->getId() !== $parentId) {
continue;
}

Expand Down Expand Up @@ -289,9 +290,9 @@ private function buildTree(?int $parentId, bool $isRoot): array
}

/**
* @param array<int, Suite> $suites
* @param Collection<int, Suite> $suites
*/
private function countStatus(int $basis, array $suites, string $status): int
private function countStatus(int $basis, Collection $suites, string $status): int
{
$num = $basis;

Expand All @@ -316,7 +317,7 @@ private function countStatus(int $basis, array $suites, string $status): int
private function filterTree(array $suites): array
{
foreach ($suites as $key => &$suite) {
$suiteChildren = $suite->getSuites();
$suiteChildren = $suite->getSuites()->toArray();
$numSuiteTests = $suite->getTests()->count();
if (!empty($suiteChildren)) {
$suite->setSuites($this->filterTree($suiteChildren));
Expand Down

0 comments on commit 0b7dcc8

Please sign in to comment.