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

Add TransactionStrategy, add helper trait #31

Merged
merged 6 commits into from
Nov 9, 2023

Conversation

msmakouz
Copy link
Member

@msmakouz msmakouz commented Nov 7, 2023

Features

TransactionStrategy

A new TransactionStrategy has been implemented, utilizing the MigrationStrategy without executing migration and rollback for each test. Instead, it initiates migration before the first test execution and encapsulates each test's execution within a transaction before running it.

To use this strategy, add the Spiral\DatabaseSeeder\Database\Traits\Transactions trait to your base TestCase for tests that use a database.

namespace Tests;

use Spiral\DatabaseSeeder\Database\Traits\Transactions;
use PHPUnit\Framework\TestCase;

abstract class DatabaseTestCase extends TestCase
{
    use Transactions;
}

Helper

A Spiral\DatabaseSeeder\Database\Traits\Helper trait has been added, encompassing a set of useful testing methods:

  • getDatabaseCleaner
  • getCurrentDatabase
  • getCurrentDatabaseDriver
  • getCurrentDatabaseProvider
  • getOrm
  • getEntityManager
  • detachEntityFromIdentityMap
  • cleanIdentityMap
  • getRepositoryFor
  • persist
use Spiral\DatabaseSeeder\Database\Traits\Helper;
use PHPUnit\Framework\TestCase;

class DatabaseTestCase extends TestCase
{
    use Helper;
    
    public function testAddUser(): void
    {
        $user = $this->userFactory->create(
            uuid: Uuid::uuid7(),
            email: 'user@site.com',
            name: 'John'
        );

        $this->persist($user);

        $user = $this->getRepositoryFor(User::class)->findByPK($user->uuid);

        $this->assertSame('user@site.com', $user->email);
        $this->assertSame('John', $user->name);
    }
}

The issue with Psalm is related to cycle/database.

@msmakouz msmakouz self-assigned this Nov 7, 2023
@msmakouz msmakouz added the enhancement New feature or request label Nov 7, 2023
@msmakouz msmakouz added this to the 3.x milestone Nov 7, 2023
@butschster butschster merged commit b537345 into 3.x Nov 9, 2023
4 of 5 checks passed
@msmakouz msmakouz deleted the feature/transaction-strategy branch January 11, 2024 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants