Skip to content

Instantly share code, notes, and snippets.

@Clindbergh
Last active October 26, 2020 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Clindbergh/cd962669b4088a04bac2733a552740aa to your computer and use it in GitHub Desktop.
Save Clindbergh/cd962669b4088a04bac2733a552740aa to your computer and use it in GitHub Desktop.
Load single Alice-fixture.yml in phpunit test with symfony

Goal

Load single alice fixtures yml file before a phpunit test with symfony.

Steps

  1. composer:
  composer require --dev zalas/phpunit-injector
  1. root\phpunit.xml.dist:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd">

  <!-- ... -->

  <listeners>
      <listener class="Zalas\Injector\PHPUnit\TestListener\ServiceInjectorListener" />
  </listeners>
</phpunit>
  1. root\config\packages\framework.yaml:
  framework:
   test: true

Write the test

use Fidry\AliceDataFixtures\Loader\PersisterLoader;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Form;
use Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer;
use Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase;

class YourTest extends WebTestCase implements ServiceContainerTestCase
{

  use SymfonyTestContainer;
  
  /**
   * @inject fidry_alice_data_fixtures.doctrine.persister_loader
   * @var PersisterLoader
   */
  private $loader;

  public function load() {
          $this->loader->load(['fixtures/test/your-fixture-for-this-test.yaml']);
  }
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment