/** * Sets up the tests */ public function setUp() { $dataPath = realpath(__DIR__ . '/..'); $this->config = parse_ini_file($dataPath . '/data/entities.doctrine.ini', true); $connection = DriverManager::getConnection(['url' => $this->config['databaseUrl']]); $this->transactor = new DoctrineTransactor($connection); $this->idAccessorRegistry = new IdAccessorRegistry(); $repositoryFactory = new RepositoryFactory($this->config, $connection, $this->transactor); $this->idAccessorRegistry->registerIdAccessors(User::class, function (User $user) { return $user->getId(); }, function (User $user, $id) { $user->setId($id); }); $strategy = new RecursiveDirectoryStrategy($this->config['definitionPath']); $locator = new Locator([$strategy]); $this->builder = new EntityBuilder($locator, $repositoryFactory); $this->repo = $repositoryFactory->forEntity(User::class); $this->entityRegistry = $repositoryFactory->getEntityRegistry(); $this->unitOfWork = new UnitOfWorkAccessDecorator($repositoryFactory->getUnitOfWork()); $this->dataMapper = $this->unitOfWork->getDataMapper(User::class); /** * The Ids are purposely unique so that we can identify them as such without having to first insert them to * assign unique Ids * They are also purposely set to 724, 1987, and 345 so that they won't potentially overlap with any default * values set to the Ids */ $this->entity1 = new User(724, "foo"); $this->entity2 = new User(1987, "bar"); $this->entity3 = new User(345, "baz"); }
public function setUp() { $this->onBeforeSetup(); $repositoryFactory = new RepositoryFactory($this->config, $this->connection, $this->transactor); $this->entityRegistry = $repositoryFactory->getEntityRegistry(); $this->builder = $repositoryFactory->getEntityBuilder(); $this->unitOfWork = $repositoryFactory->getUnitOfWork(); $this->idAccessorRegistry = $repositoryFactory->getIdAccessorRegistry(); $this->onAfterSetUp(); }
/** * Creates a RepositoryFactory * * @param Container $container The container * * @return RepositoryFactory */ public function createRepositoryFactory(Container $container) { $config = parse_ini_file(JPATH_ROOT . '/config/database.ini', true); $configuration = new Configuration(); // Add logger $logger = new DebugStack(); $configuration->setSQLLogger($logger); $connection = DriverManager::getConnection(['url' => $config['databaseUrl']], $configuration); $transactor = new DoctrineTransactor($connection); $repositoryFactory = new RepositoryFactory($config, $connection, $transactor); if ($container->has('dispatcher')) { $repositoryFactory->setDispatcher($container->get('dispatcher')); } return $repositoryFactory; }
/** * @param InputInterface $input The input * @param OutputInterface $output The output * * @return void */ protected function dumpSql(InputInterface $input, OutputInterface $output) { $connection = $this->repositoryFactory->getConnection(); if (!$connection instanceof Connection) { $this->writeln($output, "Connection " . get_class_vars($connection) . " does not support SQL."); return; } $logger = $connection->getConfiguration()->getSQLLogger(); if (!$logger instanceof DebugStack) { $this->writeln($output, "Debug logger is not enabled."); return; } $queries = $logger->queries; $table = $this->createTable($input, $output, ['#', 'SQL', 'Time']); foreach ($queries as $index => $query) { $sql = $query['sql']; $params = $query['params']; ksort($params); $sql = preg_replace_callback('~\\?~', function () use(&$params) { return array_shift($params); }, $sql); $sql = preg_replace('~(WHERE|LIMIT|INNER\\s+JOIN|LEFT\\s+JOIN)~', "\n \\1", $sql); $sql = preg_replace('~(AND|OR)~', "\n \\1", $sql); $table->addRow([$index, "{$sql}\n", sprintf('%.3f ms', 1000 * $query['executionMS'])]); } $table->render(); }
/** * @param $entityClass * @param $key */ private function setIdAccessors($entityClass, $key) { $idAccessorRegistry = $this->repositoryFactory->getIdAccessorRegistry(); if (is_array($key)) { $getter = function ($entity) use($key) { $id = []; foreach ($key as $property) { $id[$property] = $entity->{$property}; } return $id; }; $setter = function ($entity, $id) use($key) { foreach ($key as $property) { $entity->{$property} = $id[$property]; } }; $idAccessorRegistry->registerIdAccessors($entityClass, $getter, $setter); return; } if (!empty($key)) { $idAccessorRegistry->registerReflectionIdAccessors($entityClass, $key); } }
public function setUp() { $repositoryFactory = new RepositoryFactory($this->config, $this->connection, $this->transactor); $this->entityRegistry = $repositoryFactory->getEntityRegistry(); $this->unitOfWork = $repositoryFactory->getUnitOfWork(); }