public function setUp() { static::$kernel = static::createKernel(); static::$kernel->boot(); $this->application = new Application(static::$kernel); //drop the database $command = new DropDatabaseDoctrineCommand(); $this->application->add($command); $input = new ArrayInput(array('command' => 'doctrine:database:drop', '--force' => true)); $command->run($input, new NullOutput()); //"no database selected" error $connection = $this->application->getKernel()->getContainer()->get('doctrine')->getConnection(); if ($connection->isConnected()) { $connection->close(); } //create database $command = new CreateDatabaseDoctrineCommand(); $this->application->add($command); $input = new ArrayInput(array('command' => 'doctrine:database:create')); $command->run($input, new NullOutput()); //create schema $command = new CreateSchemaDoctrineCommand(); $this->application->add($command); $input = new ArrayInput(array('command' => 'doctrine:schema:create')); $command->run($input, new NullOutput()); //get entity manager $this->em = static::$kernel->getContainer()->get('doctrine')->getManager(); //load fixtures; $client = static::createClient(); $loader = new ContainerAwareLoader($client->getContainer()); $loader->loadFromDirectory(static::$kernel->locateResource('@IbwJobeetBundle/DataFixtures/ORM')); $purger = new ORMPurger($this->em); $executor = new ORMExecutor($this->em, $purger); $executor->execute($loader->getFixtures()); }
private function loadFixtures(OutputInterface $output, EntityManager $em, array $loaded) { $logger = function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }; $executor = new ORMExecutor($em); $paths = []; foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/Fixture'; } $loader = new DataFixturesLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $env = $this->getContainer()->getParameter('kernel.environment'); $output->writeln("Loading <comment>fixtures</comment>..."); $has = array_map(function (Fixture $fixture) { return $fixture->getName(); }, $loaded); $fixtures = array_filter($loader->getFixtures(), function (FixtureInterface $fixture) use($has) { return !in_array(get_class($fixture), $has); }); if (!$fixtures) { $output->writeln(" Could not find any new <comment>fixtures</comment> to load.."); return []; } $executor->setLogger($logger); $executor->execute($fixtures, true); return $fixtures; }
/** * Loads data fixtures (ported from LoadDataFixturesDoctrineCommand class) * * @throws \Codeception\Exception\Module */ public function reloadFixtures() { $container = $this->getModule('Symfony2')->container; $kernel = $this->getModule('Symfony2')->kernel; $doctrine = $container->get('doctrine'); $em = $doctrine->getManager(); $paths = array(); foreach ($kernel->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/ORM'; } $loader = new DataFixturesLoader($container); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new ORMPurger($em); $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($em, $purger); $executor->execute($fixtures, false); }
protected function execute(InputInterface $input, OutputInterface $output) { $dm = $this->getContainer()->get('doctrine_mongodb')->getManager($input->getOption('dm')); $dirOrFile = $input->getOption('fixtures'); if ($dirOrFile) { $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); } else { $paths = $this->getContainer()->getParameter('doctrine_mongodb.odm.fixtures_dirs'); $paths = is_array($paths) ? $paths : array($paths); foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/MongoDB'; } } $loader = new ContainerAwareLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new \InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new MongoDBPurger($dm); $executor = new MongoDBExecutor($dm, $purger); $executor->setLogger(function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }); $executor->execute($fixtures, $input->getOption('append')); }
/** * @inheritdoc */ public function getFixtures() { $fixtures = parent::getFixtures(); // remove already loaded fixtures foreach ($fixtures as $key => $fixture) { if ($this->isFixtureAlreadyLoaded($fixture)) { unset($fixtures[$key]); } } // add a special fixture to mark new fixtures as "loaded" if (!empty($fixtures)) { $toBeLoadFixtureClassNames = []; foreach ($fixtures as $fixture) { $version = null; if ($fixture instanceof VersionedFixtureInterface) { $version = $fixture->getVersion(); } $toBeLoadFixtureClassNames[get_class($fixture)] = $version; } $updateFixture = new UpdateDataFixturesFixture(); $updateFixture->setDataFixtures($toBeLoadFixtureClassNames); $fixtures[get_class($updateFixture)] = $updateFixture; } return $fixtures; }
/** * */ protected function fixtures() { $container = $this->getContainer(); $fixtures = func_get_args(); $loader = new ContainerAwareLoader($container); foreach ($fixtures as $fixture) { $loader->addFixture($fixture); } if ($this->isFixtureAwareAlreadyPurged) { $executor = new ORMExecutor($container->get('doctrine')->getManager()); $executor->execute($loader->getFixtures(), true); } else { $purger = new ORMPurger(); $executor = new ORMExecutor($container->get('doctrine')->getManager(), $purger); $executor->execute($loader->getFixtures()); $this->isFixtureAwareAlreadyPurged = true; } }
protected function execute(InputInterface $input, OutputInterface $output) { $appDir = $this->getContainer()->getParameter('kernel.root_dir'); if (file_exists($path = $appDir . '/UpdateFixtures')) { $loader = new DataFixturesLoader($this->getContainer()); $loader->loadFromDirectory($path); $fixtures = $loader->getFixtures(); if ($fixtures) { $input = new ArrayInput(array('command' => 'doctrine:fixtures:load', '--append' => true, '--fixtures' => 'app/UpdateFixtures/')); $this->runCommand('doctrine:fixtures:load', $input, $output); } } }
/** * Get the fixtures to load * * @param string[] $paths * * @throws \InvalidArgumentException * * @return array */ protected function getFixtures(array $paths) { $loader = new DataFixturesLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } return $fixtures; }
public function loadFixtures() { $path = sprintf('%s/Fixtures', __DIR__); $loader = new DataFixturesLoader($this->container); $loader->loadFromDirectory($path); $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', $path)); } $purger = new ORMPurger($this->em); $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($this->em, $purger); $executor->execute($fixtures); }
/** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->container = $this->getContainer(); $output->writeln("<comment>Updating...</comment>\n"); $em = $this->container->get('doctrine.orm.entity_manager'); $loader = new ContainerAwareLoader($this->container); $loader->loadFromDirectory($input->getOption('fixtures')); $purger = new ORMPurger($em); $executor = new ORMExecutor($em, $purger); $executor->setLogger(function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }); $executor->execute($loader->getFixtures(), true); }
protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Loading demo data ...'); $container = $this->getContainer(); $loader = new ContainerAwareLoader($container); foreach ($container->get('kernel')->getBundles() as $bundle) { if (is_dir($path = $bundle->getPath() . '/DataFixtures/Demo')) { $loader->loadFromDirectory($path); } } $executor = new ORMExecutor($container->get('doctrine.orm.entity_manager')); $executor->setLogger(function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }); $executor->execute($loader->getFixtures(), true); }
public function setUp() { $loader = new ContainerAwareLoader(self::$container); $loader->loadFromDirectory('./src/Pizza/CustomerBundle/DataFixtures/ORM'); $purger = new ORMPurger(self::$em); $executor = new ORMExecutor(self::$em, $purger); $executor->execute($loader->getFixtures()); $categoryrepo = self::$em->getRepository('PizzaCustomerBundle:Category'); $pizzaProducts = $categoryrepo->findOneBy(array('name' => 'pizza')); $toppingProduct = $categoryrepo->findOneBy(array('name' => 'topping')); foreach ($pizzaProducts->getProducts() as $product) { $this->pizzatypes[] = $product; } foreach ($toppingProduct->getProducts() as $product) { $this->toppings[] = $product; } }
/** * Prepara el entorno antes de la primer prueba. */ public static function setUpBeforeClass() { // Inicializamos el framework web. static::$kernel = static::createKernel(); static::$kernel->boot(); $container = static::$kernel->getContainer(); // Obtenemos algunos servicios de uso común. static::$em = $container->get('doctrine')->getManager(); static::$rootUrl = $container->getParameter('api.base_url'); if ($fixtures = static::getFixtures()) { $loader = new ContainerAwareLoader($container); array_walk($fixtures, array($loader, 'addFixture')); $purger = new ORMPurger(static::$em); $executor = new ORMExecutor(static::$em, $purger); $executor->execute($loader->getFixtures()); } }
protected function doLoadFixtures() { $paths = array(); foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/ORM'; } $loader = new DataFixturesLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $executor = new ORMExecutor($this->getEntityManager()); $executor->execute($fixtures, true); }
protected function execute(InputInterface $input, OutputInterface $output) { /** @var $doctrine \Doctrine\Common\Persistence\ManagerRegistry */ $doctrine = $this->getContainer()->get('doctrine'); $em = $doctrine->getManager($input->getOption('em')); if ($input->isInteractive() && !$input->getOption('append')) { $dialog = $this->getHelperSet()->get('dialog'); if (!$dialog->askConfirmation($output, '<question>Careful, database will be purged. Do you want to continue Y/N ?</question>', false)) { return; } } $dirOrFile = $input->getOption('fixtures'); if ($dirOrFile) { $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); } else { $paths = array(); foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/ORM'; } } $loader = new DataFixturesLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new ORMPurger($em); $purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($em, $purger); $executor->setLogger(function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }); $executor->execute($fixtures, $input->getOption('append')); // Serialize reference repository to a file. So fixtures will be accessible by names inside functional tests. $referenceRepositoryFile = $this->getContainer()->getParameter('fixtures.reference_repository_path'); $serializedReferenceRepository = (new FixtureReferenceRepositorySerializer())->serialize($executor->getReferenceRepository()); file_put_contents($referenceRepositoryFile, $serializedReferenceRepository); $output->writeln(sprintf('<info>Reference repository saved to:</info> <comment>%s</comment>', realpath($referenceRepositoryFile))); }
/** * {@inheritdoc} */ public function forwardAction(ProcessContextInterface $context) { $request = $this->getRequest(); $form = $this->createForm('sylius_setup'); $em = $this->getDoctrine()->getManager(); if ($form->handleRequest($request)->isValid()) { $params = $this->get('doctrine')->getConnection()->getParams(); $dbname = $params['dbname']; unset($params['dbname']); $schemaManager = DriverManager::getConnection($params)->getSchemaManager(); if (!in_array($dbname, $schemaManager->listDatabases())) { $schemaManager->createDatabase($dbname); } $schemaTool = new SchemaTool($em); $schemaTool->dropSchema($em->getMetadataFactory()->getAllMetadata()); $schemaTool->createSchema($em->getMetadataFactory()->getAllMetadata()); if ($form->get('load_fixtures')->getData()) { $loader = new ContainerAwareLoader($this->container); foreach ($this->get('kernel')->getBundles() as $bundle) { if (is_dir($path = $bundle->getPath() . '/DataFixtures/ORM')) { $loader->loadFromDirectory($path); } } $purger = new ORMPurger($em); $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($em, $purger); $executor->execute($loader->getFixtures()); } $user = $form->getData(); $user->setEnabled(true); $user->setRoles(array('ROLE_SYLIUS_ADMIN')); $em->persist($user); $em->flush(); $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('sylius.flashes.installed')); return $this->complete(); } return $this->render('SyliusInstallerBundle:Process/Step:setup.html.twig', array('form' => $form->createView())); }
/** * @BeforeScenario */ public function loadFixturesBeforeScenarios() { $paths = array(); foreach ($this->kernel->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/ORM'; } $loader = new DataFixturesLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $em = $this->getContainer()->get('doctrine.orm.entity_manager'); $fixtures = $loader->getFixtures(); $purger = new ORMPurger($em); //$purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($em, $purger); // $executor->setLogger(function($message) use ($output) { // $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); // }); $append = false; $executor->execute($fixtures, $append); }
protected function execute(InputInterface $input, OutputInterface $output) { /** @var $doctrine \Doctrine\Common\Persistence\ManagerRegistry */ $doctrine = $this->getContainer()->get('doctrine'); $em = $doctrine->getManager($input->getOption('em')); if ($input->isInteractive() && !$input->getOption('append')) { if (!$this->askConfirmation($input, $output, '<question>Careful, database will be purged. Do you want to continue y/N ?</question>', false)) { return; } } if ($input->getOption('shard')) { if (!$em->getConnection() instanceof PoolingShardConnection) { throw new \LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $input->getOption('em'))); } $em->getConnection()->connect($input->getOption('shard')); } $dirOrFile = $input->getOption('fixtures'); if ($dirOrFile) { $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); } else { $paths = array(); foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/ORM'; } } $loader = new DataFixturesLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } elseif (is_file($path)) { $loader->loadFromFile($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new ORMPurger($em); $purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($em, $purger); $executor->setLogger(function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }); $executor->execute($fixtures, $input->getOption('append'), $input->getOption('multiple-transactions')); }
/** * Installs data fixtures for the application * * @return array|bool Array containing the flash message data on a failure, boolean true on success */ private function performFixtureInstall() { try { $entityManager = $this->getEntityManager(); $paths = array(dirname(__DIR__) . '/InstallFixtures/ORM'); $loader = new ContainerAwareLoader($this->container); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new \InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new ORMPurger($entityManager); $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($entityManager, $purger); $executor->execute($fixtures, true); } catch (\Exception $exception) { return array('type' => 'error', 'msg' => 'mautic.installer.error.adding.fixtures', 'msgVars' => array('%exception%' => $exception->getMessage())); } return true; }
/** * @param FixtureInterface|FixtureInterface[] $data */ private function loadTestData($data) { $loader = new ContainerAwareLoader(static::$kernel->getContainer()); if (!is_array($data)) { $data = array($data); } foreach ($data as $dataSet) { $loader->addFixture($dataSet); } $this->fixtures = $loader->getFixtures(); $purger = new ORMPurger(); $executor = new ORMExecutor(static::$em, $purger); $executor->execute($loader->getFixtures()); }
/** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { if ($input->getOption('name')) { trigger_error('The name attribute for command doctrine:phpcr:fixtures:load is deprecated. It was never used.', E_USER_DEPRECATED); } if ($input->getOption('dm')) { $dmName = $input->getOption('dm'); } else { if ($input->getOption('session')) { $dmName = $input->getOption('session'); trigger_error('The session attribute for command doctrine:phpcr:fixtures:load is deprecated. Use --dm instead.', E_USER_DEPRECATED); } else { $dmName = null; } } DoctrineCommandHelper::setApplicationDocumentManager($this->getApplication(), $dmName); $dm = $this->getHelperSet()->get('phpcr')->getDocumentManager(); $noInitialize = $input->getOption('no-initialize'); if ($input->isInteractive() && !$input->getOption('append')) { /** @var $dialog DialogHelper */ $dialog = $this->getHelperSet()->get('dialog'); if (!$dialog->askConfirmation($output, '<question>Careful, database will be purged. Do you want to continue Y/N ?</question>', false)) { return 0; } } $dirOrFile = $input->getOption('fixtures'); if ($dirOrFile) { $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); } else { $paths = array(); foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/PHPCR'; } } $loader = new ContainerAwareLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new PHPCRPurger($dm); if ($noInitialize) { $initializerManager = null; } else { $initializerManager = $this->getContainer()->get('doctrine_phpcr.initializer_manager'); } $executor = new PHPCRExecutor($dm, $purger, $initializerManager); $executor->setLogger(function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }); $executor->execute($fixtures, $input->getOption('append')); return 0; }
protected function execute(InputInterface $input, OutputInterface $output) { $dm = $this->getContainer()->get('doctrine_mongodb')->getManager($input->getOption('dm')); $dirOrFile = $input->getOption('fixtures'); $bundles = $input->getOption('bundles'); if ($bundles && $dirOrFile) { throw new \InvalidArgumentException('Use only one option: --bundles or --fixtures.'); } if ($input->isInteractive() && !$input->getOption('append')) { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('Careful, database will be purged. Do you want to continue (y/N) ?', false); if (!$helper->ask($input, $output, $question)) { return; } } if ($dirOrFile) { $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); } elseif ($bundles) { $kernel = $this->getContainer()->get('kernel'); foreach ($bundles as $bundle) { $paths[] = $kernel->getBundle($bundle)->getPath(); } } else { $paths = $this->getContainer()->getParameter('doctrine_mongodb.odm.fixtures_dirs'); $paths = is_array($paths) ? $paths : array($paths); foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { $paths[] = $bundle->getPath() . '/DataFixtures/MongoDB'; } } $loader = new ContainerAwareLoader($this->getContainer()); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } else { if (is_file($path)) { $loader->loadFromFile($path); } } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new \InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new MongoDBPurger($dm); $executor = new MongoDBExecutor($dm, $purger); $executor->setLogger(function ($message) use($output) { $output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); }); $executor->execute($fixtures, $input->getOption('append')); }
/** * Installs data fixtures for the application * * @return array|bool Array containing the flash message data on a failure, boolean true on success */ private function installDatabaseFixtures() { $entityManager = $this->get('doctrine.orm.entity_manager'); $paths = [dirname(__DIR__) . '/InstallFixtures/ORM']; $loader = new ContainerAwareLoader($this->container); foreach ($paths as $path) { if (is_dir($path)) { $loader->loadFromDirectory($path); } } $fixtures = $loader->getFixtures(); if (!$fixtures) { throw new \InvalidArgumentException(sprintf('Could not find any fixtures to load in: %s', "\n\n- " . implode("\n- ", $paths))); } $purger = new ORMPurger($entityManager); $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($entityManager, $purger); $executor->execute($fixtures, true); }