generated from spiral-packages/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from spiral-packages/feature/transaction-strategy
Add TransactionStrategy, add helper trait
- Loading branch information
Showing
11 changed files
with
458 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\DatabaseSeeder\Database\Strategy; | ||
|
||
use Cycle\Database\DatabaseInterface; | ||
use Spiral\Testing\TestCase; | ||
|
||
/** | ||
* This strategy utilizes MigrationStrategy without executing migration and rolling back migration for each test | ||
* but performs migration prior to running the first test. It wraps the test execution in a transaction before each | ||
* test. | ||
*/ | ||
class TransactionStrategy | ||
{ | ||
protected MigrationStrategy $migrationStrategy; | ||
|
||
public function __construct( | ||
protected TestCase $testCase, | ||
?MigrationStrategy $migrationStrategy = null | ||
) { | ||
$this->migrationStrategy = $migrationStrategy ?? new MigrationStrategy($this->testCase); | ||
} | ||
|
||
public function begin(): void | ||
{ | ||
$this->migrationStrategy->migrate(); | ||
|
||
if ($this->migrationStrategy->isCreateMigrations()) { | ||
$this->migrationStrategy->deleteMigrations(); | ||
} | ||
|
||
$this->testCase->getContainer()->get(DatabaseInterface::class)->getDriver()->beginTransaction(); | ||
} | ||
|
||
public function rollback(): void | ||
{ | ||
$this->testCase->getContainer()->get(DatabaseInterface::class)->getDriver()->rollbackTransaction(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\DatabaseSeeder\Database\Traits; | ||
|
||
use Cycle\Database\DatabaseInterface; | ||
use Cycle\Database\DatabaseProviderInterface; | ||
use Cycle\Database\Driver\DriverInterface; | ||
use Cycle\ORM\EntityManagerInterface; | ||
use Cycle\ORM\ORMInterface; | ||
use Cycle\ORM\RepositoryInterface; | ||
use Spiral\DatabaseSeeder\Database\Cleaner; | ||
|
||
trait Helper | ||
{ | ||
private ?Cleaner $cleaner = null; | ||
|
||
public function getDatabaseCleaner(): Cleaner | ||
{ | ||
if ($this->cleaner === null) { | ||
$this->cleaner = new Cleaner($this->getCurrentDatabaseProvider()); | ||
} | ||
|
||
return $this->cleaner; | ||
} | ||
|
||
public function getCurrentDatabase(): DatabaseInterface | ||
{ | ||
return $this->getContainer()->get(DatabaseInterface::class); | ||
} | ||
|
||
public function getCurrentDatabaseDriver(): DriverInterface | ||
{ | ||
return $this->getCurrentDatabase()->getDriver(); | ||
} | ||
|
||
public function getCurrentDatabaseProvider(): DatabaseProviderInterface | ||
{ | ||
return $this->getContainer()->get(DatabaseProviderInterface::class); | ||
} | ||
|
||
public function getOrm(): ORMInterface | ||
{ | ||
return $this->getContainer()->get(ORMInterface::class); | ||
} | ||
|
||
public function getEntityManager(): EntityManagerInterface | ||
{ | ||
return $this->getContainer()->get(EntityManagerInterface::class); | ||
} | ||
|
||
public function detachEntityFromIdentityMap(object $entity): void | ||
{ | ||
$this->getOrm()->getHeap()->detach($entity); | ||
} | ||
|
||
public function cleanIdentityMap(): void | ||
{ | ||
$this->getOrm()->getHeap()->clean(); | ||
} | ||
|
||
public function getRepositoryFor(object|string $entity): RepositoryInterface | ||
{ | ||
return $this->getOrm()->getRepository($entity); | ||
} | ||
|
||
public function persist(object $entity): void | ||
{ | ||
$this->getEntityManager()->persist($entity)->run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\DatabaseSeeder\Database\Traits; | ||
|
||
use Spiral\DatabaseSeeder\Database\Strategy\TransactionStrategy; | ||
|
||
trait Transactions | ||
{ | ||
private ?TransactionStrategy $transactionStrategy = null; | ||
|
||
|
||
public function beginTransaction(): void | ||
{ | ||
$this->beforeBeginTransaction(); | ||
|
||
$this->getTransactionStrategy()->begin(); | ||
|
||
$this->afterBeginTransaction(); | ||
} | ||
|
||
public function rollbackTransaction(): void | ||
{ | ||
$this->beforeRollbackTransaction(); | ||
|
||
$this->getTransactionStrategy()->rollback(); | ||
|
||
$this->afterRollbackTransaction(); | ||
} | ||
|
||
protected function setUpTransactions(): void | ||
{ | ||
$this->beginTransaction(); | ||
} | ||
|
||
protected function tearDownTransactions(): void | ||
{ | ||
$this->rollbackTransaction(); | ||
} | ||
|
||
protected function getTransactionStrategy(): TransactionStrategy | ||
{ | ||
if ($this->transactionStrategy === null) { | ||
$this->transactionStrategy = new TransactionStrategy(testCase: $this); | ||
} | ||
|
||
return $this->transactionStrategy; | ||
} | ||
|
||
/** | ||
* Perform any work before the database transaction has started | ||
*/ | ||
protected function beforeBeginTransaction(): void | ||
{ | ||
// ... | ||
} | ||
|
||
/** | ||
* Perform any work after the database transaction has started | ||
*/ | ||
protected function afterBeginTransaction(): void | ||
{ | ||
// ... | ||
} | ||
|
||
/** | ||
* Perform any work before rolling back the transaction | ||
*/ | ||
protected function beforeRollbackTransaction(): void | ||
{ | ||
// ... | ||
} | ||
|
||
/** | ||
* Perform any work after rolling back the transaction | ||
*/ | ||
protected function afterRollbackTransaction(): void | ||
{ | ||
// ... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.