dimanche 8 mars 2020

How to test size of persistent collection in Symfony?

Symfony 5.0.5. I have a service with this method:

public function getFleas(Cat $cat): Collection
{
    return $cat->getFleas();
}

In my Cat entity:

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Flea", mappedBy="cat")
 */
private $fleas;

I am trying to write a test for the getFleas method:

public function testItReturnsAListOfFleas()
{
    $cat = $this->createCat();
    $fleaList = [];
    $numFleas = 5;
    for ($i = 0; $i < $numFleas; $i++) {
        $fleaList[] = $this->createFlea($cat);
    }
    $fleas = $this->fleaService->getFleas($cat);
    $this->assertCount($numFleas, $fleas);
}

However, this fails. The count of the $fleas persistent collection returned by getFleas is always 0. How can I get the count of $fleas? I have also tried

$this->assertSame($numFleas, $fleas->count());

but this also returns 0.

Aucun commentaire:

Enregistrer un commentaire