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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update readme description #9

Merged
merged 2 commits into from
May 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@

# Active Record Implementation for Cycle ORM

Active Record Pattern implementation for Cycle ORM. This package provides a simple way to work with your database records using Active Record pattern.
This library extends Cycle ORM by integrating the [Active Record pattern](https://en.wikipedia.org/wiki/Active_record_pattern), providing developers with an intuitive, object-centric way to interact with databases.

Unlike Cycle ORM's default Data Mapper pattern, which separates the in-memory object representations from database operations, Active Record combines data access and business logic in a single entity.

This allows for more straightforward and rapid development cycles, particularly for simpler CRUD operations, by enabling direct data manipulation through the object's properties and methods.

<br>

## 🚩 Prerequisites

Expand Down
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 @@ -99,13 +99,13 @@
* @throws NotFoundExceptionInterface
* @throws Throwable
*/
final public function save(bool $cascade = true): StateInterface

Check warning on line 102 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 save(bool $cascade = true) : StateInterface + public final function save(bool $cascade = false) : StateInterface { /** @var EntityManager $entityManager */ $entityManager = Facade::getEntityManager();
{
/** @var EntityManager $entityManager */
$entityManager = Facade::getEntityManager();
$entityManager->persist($this, $cascade);

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

Check warning on line 108 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->persist($this, $cascade); - return $entityManager->run(throwException: false); + return $entityManager->run(throwException: true); } /** * Persists the current entity and throws an exception if an error occurs.
}

/**
Expand All @@ -115,7 +115,7 @@
* @throws NotFoundExceptionInterface
* @throws Throwable
*/
final public function saveOrFail(bool $cascade = true): StateInterface

Check warning on line 118 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 saveOrFail(bool $cascade = true) : StateInterface + public final function saveOrFail(bool $cascade = false) : StateInterface { /** @var EntityManager $entityManager */ $entityManager = Facade::getEntityManager();
{
/** @var EntityManager $entityManager */
$entityManager = Facade::getEntityManager();
Expand All @@ -128,8 +128,36 @@
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
final public function persist(bool $cascade = true): EntityManagerInterface

Check warning on line 131 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 ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public final function persist(bool $cascade = true) : EntityManagerInterface + public final function persist(bool $cascade = false) : EntityManagerInterface { return Facade::getEntityManager()->persist($this, $cascade); }
{
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());
}
}
Loading