2
votes

I am trying to configure symfony to use sqlite for tests.

composer.json (require-dev)

"doctrine/doctrine-fixtures-bundle": "^3.1",
"liip/functional-test-bundle": "~2.0@alpha",

config/packages/test/doctrine.yaml:

doctrine:
  dbal:
    driver: 'pdo_sqlite'
    url: 'sqlite:///%kernel.project_dir%/var/test.db3'

then I made a test like this

class SimplestTest extends WebTestCase
{

private $fixtures;

public function setUp()
{
    $this->fixtures = $this->loadFixtures([
        MyFixtures::class
        ])->getReferenceRepository();
}

public function testToSeeIfItWorks()
{
    $this->assertTrue(true);
}
}

MyFixtures class extends AbstractFixture and load some simple objects:

class MyFixtures extends AbstractFixture
{
public function load(ObjectManager $manager)
{

    $user1 = new User();
    $user1->setRoles(['ROLE_USER']);

    $manager->persist($user1);
    $manager->flush();

    $myFeed = new Feed();
    $myFeed->setName('Feed 1');
    $myFeed->setUrl('http://someurl');
    $myFeed->setUser($user1);

    $manager->persist($myFeed);
    $manager->flush();
}
}

When I run the test I get:

InvalidArgumentException: "App\Tests\DataFixtures\ORM\MyFixtures" is not a registered fixture

no idea why. any help?

thank you

1
Can you paste the code for MyFixtures? - Michał Tomczuk
@Emma tried already same thing - user3174311
@MichałTomczuk I updated the question with the fixture code. - user3174311
Check AbstractTest class here that registers fixtures. - BentCoder
@user3174311 your whole code looks like it should work. There is one thing I would change - you are flushing twice in MyFixture, remove the first $manager->flush(). Maybe, for whatever dark doctrine reason, it produces the error. - Michał Tomczuk

1 Answers

3
votes

I think I know what's the problem. There seems to be a bug in library's documentation if it comes to the class the fixture should extend from. In the documentation in states to use Doctrine\Common\DataFixtures\AbstractFixture, but it makes the fixture not load automatically.

As you can read in DoctrineFixturesBundle documentation, the bundle will add a doctrine.fixture.orm tag by autowiring to any class that implements ORMFixtureInterface, like Doctrine\Bundle\FixturesBundle\Fixture. Loading fixtures from classes that implement the interface is done by FixturesCompilerPass.

If you change the base class of your fixture to Doctrine\Bundle\FixturesBundle\Fixture it should work.

Update:

On top of all above the fixture class has to be located in App\DataFixtures namespace.

That is because that namespace is configured to be autowired and autoconfigured. If you want to keep your fixture files under App\Tests\DataFixtures\, add proper configuration to config/services.yaml file:

services:
    App\Tests\DataFixtures\:
        resource: '../tests/DataFixtures'