Skip to content

Commit

Permalink
feat: add remove method
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp committed May 3, 2024
1 parent 9d3228d commit 4d2843c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"indent-style": "space"
},
"config-plugin": {
"bootstrap": "config/yii-bootstrap.php"
"bootstrap": "src/Bridge/Yii3/yii-bootstrap.php"
},
"laravel": {
"providers": [
Expand Down
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PossiblyUnusedMethod>
<PossiblyUnusedReturnValue>
<code><![CDATA[EntityManagerInterface]]></code>
<code><![CDATA[EntityManagerInterface]]></code>
</PossiblyUnusedReturnValue>
</file>
<file src="src/Bridge/Laravel/Providers/ActiveRecordProvider.php">
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
</plugins>

<projectFiles>
<directory name="config/"/>
<directory name="src/"/>
<directory name="tests/"/>
<file name=".php-cs-fixer.dist.php"/>
Expand Down
14 changes: 14 additions & 0 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,18 @@ final public function deleteOrFail(bool $cascade = true): StateInterface

return $entityManager->run();
}

/**
* Prepares the current entity for deletion.
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
final public function remove(bool $cascade = true): EntityManagerInterface
{
/** @var EntityManager $entityManager */
$entityManager = Facade::getEntityManager();

return $entityManager->delete($this, $cascade);
}
}
File renamed without changes.
24 changes: 24 additions & 0 deletions tests/src/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,28 @@ public function it_deletes_entity(): void
$this::assertTrue($user->delete()->isSuccess());
$this::assertCount(1, User::findAll());
}

/**
* @test
*
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws Throwable
*/
#[Test]
public function it_deletes_multiple_entities_using_remove_method(): void
{
$userOne = User::find(1);
$userTwo = User::find(2);

$userOne->remove();
$userTwo->remove();

$this::assertCount(2, User::findAll());

$entityManager = Facade::getEntityManager();
$this::assertTrue($entityManager->run()->isSuccess());

$this::assertCount(0, User::findAll());
}
}

0 comments on commit 4d2843c

Please sign in to comment.