Zenstruck foundry symfony api

479 Views Asked by At

I have an issue on how to use, Zenstruck foundry to test an api.

What I want is to be able to create object using zenstruck foundry, to be able to check them using the api. Then at the end of my test, I want the object to be destroyed.

For exemple I have this test

public function listEventType(): void
{
    // Init kernel browser
    $this->init();
    // create object
    $factory = EventTypeFactory::new();
    $eventType = $factory
        ->create(['name' => 'EventType 1']);

    // ask api to send me back a list of eventType
    $arrayResponse = $this->getJsonResponse('GET', 'api/eventType');

    // Assert success and that there is only one
    self::assertResponseIsSuccessful();
    self::assertEquals(1, $arrayResponse['totalItems']);
    self::assertCount(1, $arrayResponse['items']);
}

At the end of my test I want the object created with the factory to be destroyed, so that it does not poluate my next test.

I saw that I can use zendstruck foundry withoutPersisting, but then the api call return zero results:

protected function initialize(): self
{
    return $this
        ->withoutPersisting()
        ;
}

Does anyone as an idea how to solve this problem ?

(For now I'm using without persisting in the factories, and I remove everything at the end of every test)

1

There are 1 best solutions below

0
LinChan On

Just found a solution, using this bundle https://github.com/dmaicher/doctrine-test-bundle. Which set everything in a transaction for every single test.