Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doctrine Object : Move Suite Parent Id to object #131

Merged
merged 1 commit into from
Feb 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 40 additions & 11 deletions src/Entity/Suite.php
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
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
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
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