Skip to content

Commit

Permalink
feat: add delete entity method
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp committed May 3, 2024
1 parent ccc72ca commit 8be8e30
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"report": "master"
}
},
"minCoveredMsi": 93,
"minCoveredMsi": 70,
"minMsi": 70,
"phpUnit": {
"configDir": "./"
Expand Down
3 changes: 3 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
<file src="src/ActiveRecord.php">
<PossiblyUnusedMethod>
<code><![CDATA[deleteOrFail]]></code>
</PossiblyUnusedMethod>
<PossiblyUnusedReturnValue>
<code><![CDATA[EntityManagerInterface]]></code>
</PossiblyUnusedReturnValue>
Expand Down
28 changes: 28 additions & 0 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,32 @@ final public function persist(bool $cascade = true): EntityManagerInterface
{
return Facade::getEntityManager()->persist($this, $cascade);
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Throwable
*/
final public function delete(bool $cascade = true): StateInterface

Check warning on line 141 in src/ActiveRecord.php

View workflow job for this annotation

GitHub Actions / mutation-testing (ubuntu-latest, 8.2, locked)

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * @throws NotFoundExceptionInterface * @throws Throwable */ - public final function delete(bool $cascade = true) : StateInterface + public final function delete(bool $cascade = false) : StateInterface { /** @var EntityManager $entityManager */ $entityManager = Facade::getEntityManager();
{
/** @var EntityManager $entityManager */
$entityManager = Facade::getEntityManager();
$entityManager->delete($this, $cascade);

return $entityManager->run(throwException: false);

Check warning on line 147 in src/ActiveRecord.php

View workflow job for this annotation

GitHub Actions / mutation-testing (ubuntu-latest, 8.2, locked)

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ /** @var EntityManager $entityManager */ $entityManager = Facade::getEntityManager(); $entityManager->delete($this, $cascade); - return $entityManager->run(throwException: false); + return $entityManager->run(throwException: true); } /** * @throws ContainerExceptionInterface
}

/**
* @throws ContainerExceptionInterface
* @throws Throwable
* @throws NotFoundExceptionInterface
*/
final public function deleteOrFail(bool $cascade = true): StateInterface

Check warning on line 155 in src/ActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRecord.php#L155

Added line #L155 was not covered by tests
{
/** @var EntityManager $entityManager */
$entityManager = Facade::getEntityManager();
$entityManager->delete($this, $cascade);

Check warning on line 159 in src/ActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRecord.php#L158-L159

Added lines #L158 - L159 were not covered by tests

return $entityManager->run();

Check warning on line 161 in src/ActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRecord.php#L161

Added line #L161 was not covered by tests
}
}
17 changes: 17 additions & 0 deletions tests/src/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,21 @@ public function it_persists_multiple_entities(): void
$savedUserTwo = $this->selectEntity(User::class, cleanHeap: true)->wherePK($userTwo->id)->fetchOne();
$this::assertSame($savedUserTwo->name, $userTwo->name);
}

/**
* @test
*
* @throws NotFoundExceptionInterface
* @throws Throwable
* @throws ContainerExceptionInterface
*/
#[Test]
public function it_deletes_entity(): void
{
$user = User::find(1);
$this::assertNotNull($user);

$this::assertTrue($user->delete()->isSuccess());
$this::assertCount(1, User::findAll());
}
}

0 comments on commit 8be8e30

Please sign in to comment.