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

feat: "in memory" behavior #590

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

nikophil
Copy link
Member

@nikophil nikophil commented Apr 9, 2024

This PR introduces "in-memory" behavior

fixes #533

It makes integration testing with "in memory" repositories very easy!

Here is the proposal:
Given some kind of DDD or hexagonal architecture, repositories in domain are usually interfaces, which main implementation is a Doctrine one. "in memory" version of these repositories will exist and would be injected in the container in test environment.

This PR exposes a new interface Zenstruck\Foundry\InMemory\InMemoryRepository, e.g.:

/**
 * @implements InMemoryRepository<Address>
 */
final class InMemoryAddressRepository implements InMemoryRepository, DomainAddressRepository
{
    /** @use InMemoryRepositoryTrait<Address> */
    use InMemoryRepositoryTrait;

    public static function _class(): string
    {
        return Address::class;
    }

    // + all methods implementing "DomainAddressRepository"
}

It also exposes a new attribute #[AsInMemoryTest], which will disable persistence of the factories, and register an "after instantiate" hook, which will store the objects in their respective "in memory" repositories:

#[AsInMemoryTest]
class SomeTest extends KernelTestCase
{
    private InMemoryAddressRepository $addressRepository;
 
    protected function setUp(): void
    {
        $this->addressRepository = self::getContainer()->get(InMemoryAddressRepository::class);
    }

    #[Test]
    public function object_should_be_accessible_from_in_memory_repository(): void
    {
        $address = AddressFactory::createOne();

        self::assertSame([$address], $this->addressRepository->_all());
    }
}

A GenericInMemoryRepository is also added for convenience, when the given in memory repository is missing.

@nikophil nikophil changed the base branch from 1.x to 2.x April 9, 2024 07:07
@nikophil nikophil force-pushed the feat/in-memory-behavior branch from 3e88d34 to f7be7ac Compare April 9, 2024 07:14
@nikophil nikophil force-pushed the feat/in-memory-behavior branch 5 times, most recently from f6684d2 to 7914ee1 Compare May 7, 2024 19:59
@nikophil nikophil force-pushed the feat/in-memory-behavior branch 2 times, most recently from 23b2122 to 51dac5b Compare June 25, 2024 14:39
@nikophil nikophil force-pushed the feat/in-memory-behavior branch 13 times, most recently from 56a501b to 558fc5f Compare August 19, 2024 18:04
@nikophil nikophil changed the title feat/in memory behavior feat: "in memory" behavior Aug 19, 2024
src/Configuration.php Outdated Show resolved Hide resolved
@nikophil nikophil force-pushed the feat/in-memory-behavior branch 4 times, most recently from 90502f7 to 7608b78 Compare October 26, 2024 08:51
@nikophil nikophil force-pushed the feat/in-memory-behavior branch from 7608b78 to 191bdf4 Compare November 11, 2024 10:49
@nikophil nikophil force-pushed the feat/in-memory-behavior branch 4 times, most recently from 6e979f6 to 6135e03 Compare February 16, 2025 16:25
@nikophil nikophil force-pushed the feat/in-memory-behavior branch from 6135e03 to 5cb5d9a Compare February 16, 2025 16:32
@nikophil nikophil requested review from kbond and smnandre February 16, 2025 16:39
@nikophil nikophil marked this pull request as ready for review February 16, 2025 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

support "in-memory" repositories
2 participants