ynloultratech /
graphql-bundle
| 1 | <?php |
||
| 2 | /******************************************************************************* |
||
| 3 | * This file is part of the GraphQL Bundle package. |
||
| 4 | * |
||
| 5 | * (c) YnloUltratech <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | ******************************************************************************/ |
||
| 10 | |||
| 11 | namespace Ynlo\GraphQLBundle\Test\FixtureLoader; |
||
| 12 | |||
| 13 | use Doctrine\Bundle\DoctrineBundle\Registry; |
||
| 14 | use Doctrine\Common\DataFixtures\DependentFixtureInterface; |
||
| 15 | use Doctrine\Common\DataFixtures\Loader; |
||
| 16 | use Doctrine\Common\DataFixtures\ProxyReferenceRepository; |
||
| 17 | use Doctrine\Common\DataFixtures\ReferenceRepository; |
||
| 18 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
| 19 | use Symfony\Component\HttpKernel\Kernel; |
||
| 20 | use Ynlo\GraphQLBundle\Test\FixtureLoader\DataPopulator\DataLoaderInterface; |
||
| 21 | use Ynlo\GraphQLBundle\Test\FixtureLoader\DataPopulator\ORMDataLoader; |
||
| 22 | use Ynlo\GraphQLBundle\Test\FixtureLoader\SchemaUpdater\ORMSQLite; |
||
| 23 | use Ynlo\GraphQLBundle\Test\FixtureLoader\SchemaUpdater\SchemaUpdaterInterface; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Class FixtureLoader |
||
| 27 | */ |
||
| 28 | class FixtureLoader |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var ContainerInterface |
||
| 32 | */ |
||
| 33 | protected $container; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Registry |
||
| 37 | */ |
||
| 38 | protected $registry; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array[] |
||
| 42 | */ |
||
| 43 | protected $plugins = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected static $schemaUpdated = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * FixtureLoader constructor. |
||
| 52 | * |
||
| 53 | * @param ContainerInterface $container |
||
| 54 | * @param Registry $registry |
||
| 55 | * @param array $plugins |
||
| 56 | */ |
||
| 57 | public function __construct(ContainerInterface $container, Registry $registry, $plugins = []) |
||
| 58 | { |
||
| 59 | $this->registry = $registry; |
||
| 60 | $this->container = $container; |
||
| 61 | |||
| 62 | $this->plugins = $plugins; |
||
| 63 | |||
| 64 | $cacheDir = $container->getParameter('kernel.cache_dir').DIRECTORY_SEPARATOR.'tests'; |
||
| 65 | $container->get('filesystem')->mkdir($cacheDir); |
||
| 66 | |||
| 67 | $this->plugins[] = new ORMSQLite($cacheDir); |
||
| 68 | $this->plugins[] = new ORMDataLoader($cacheDir); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param array $classNames |
||
| 73 | * @param bool $append |
||
| 74 | * |
||
| 75 | * @return ReferenceRepository |
||
| 76 | */ |
||
| 77 | public function loadFixtures($classNames = [], $append = false): ReferenceRepository |
||
| 78 | { |
||
| 79 | if (!self::$schemaUpdated) { |
||
| 80 | foreach ($this->plugins as $plugin) { |
||
| 81 | if ($plugin instanceof SchemaUpdaterInterface) { |
||
| 82 | if ($plugin->supports($this->registry)) { |
||
| 83 | $plugin->updateSchema($this->registry); |
||
| 84 | self::$schemaUpdated = true; |
||
| 85 | break; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | $loader = $this->getFixtureLoader($this->container, $classNames); |
||
| 92 | $fixtures = $loader->getFixtures(); |
||
| 93 | $referenceRepository = new ProxyReferenceRepository($this->registry->getManager()); |
||
| 94 | foreach ($this->plugins as $plugin) { |
||
| 95 | if ($plugin instanceof DataLoaderInterface) { |
||
| 96 | if ($plugin->supports($this->registry)) { |
||
| 97 | $executor = $plugin->createExecutor($this->registry); |
||
| 98 | $executor->purge(); |
||
| 99 | $executor->setReferenceRepository($referenceRepository); |
||
| 100 | $executor->execute($fixtures, $append); |
||
| 101 | break; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return $referenceRepository; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Retrieve Doctrine DataFixtures loader. |
||
| 111 | * |
||
| 112 | * @param ContainerInterface $container |
||
| 113 | * @param array $classNames |
||
| 114 | * |
||
| 115 | * @return Loader |
||
| 116 | */ |
||
| 117 | protected function getFixtureLoader(ContainerInterface $container, array $classNames) |
||
| 118 | { |
||
| 119 | $loaderClass = class_exists('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader') |
||
| 120 | ? 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader' |
||
| 121 | : (class_exists('Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader') |
||
| 122 | // This class is not available during tests. |
||
| 123 | // @codeCoverageIgnoreStart |
||
| 124 | ? 'Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader' |
||
| 125 | // @codeCoverageIgnoreEnd |
||
| 126 | : 'Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader'); |
||
| 127 | |||
| 128 | /** @var Loader $loader */ |
||
| 129 | $loader = new $loaderClass($container); |
||
| 130 | |||
| 131 | if ($classNames) { |
||
|
0 ignored issues
–
show
|
|||
| 132 | foreach ($classNames as $className) { |
||
| 133 | $this->loadFixtureClass($loader, $className); |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | $kernel = $container->get('kernel'); |
||
| 137 | $bundles = $kernel->getBundles(); |
||
| 138 | foreach ($bundles as $bundle) { |
||
| 139 | $dir = $bundle->getPath().'/DataFixtures'; |
||
| 140 | if (file_exists($dir)) { |
||
| 141 | $loader->loadFromDirectory($dir); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | //load symfony4 data fixtures |
||
| 146 | if (Kernel::VERSION_ID >= 40000) { |
||
| 147 | $dir = $kernel->getRootDir().'/DataFixtures'; |
||
| 148 | if (file_exists($dir)) { |
||
| 149 | $loader->loadFromDirectory($dir); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return $loader; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Load a data fixture class. |
||
| 159 | * |
||
| 160 | * @param Loader $loader |
||
| 161 | * @param string $className |
||
| 162 | */ |
||
| 163 | protected function loadFixtureClass($loader, $className) |
||
| 164 | { |
||
| 165 | $fixture = new $className(); |
||
| 166 | |||
| 167 | if ($loader->hasFixture($fixture)) { |
||
| 168 | unset($fixture); |
||
| 169 | |||
| 170 | return; |
||
| 171 | } |
||
| 172 | |||
| 173 | $loader->addFixture($fixture); |
||
| 174 | |||
| 175 | if ($fixture instanceof DependentFixtureInterface) { |
||
| 176 | foreach ($fixture->getDependencies() as $dependency) { |
||
| 177 | $this->loadFixtureClass($loader, $dependency); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.