/** * {@inheritDoc} Cleans database table before doing so. * * @param Fixture $fixture * @return bool */ public function persist(Fixture $fixture) { $database = $this->config['database']; $table = $fixture->getName(); $this->getConnection()->query("SET FOREIGN_KEY_CHECKS = 0;"); $this->getConnection()->query("TRUNCATE `{$database}`.`{$table}`;"); $success = true; foreach ($fixture as $fixtureData) { $columns = array_keys($fixtureData); $columns = implode('`, `', $columns); $values = []; foreach ($fixtureData as $attribute => $value) { $values[':' . $attribute] = $value; } $placeholders = implode(", ", array_keys($values)); $sql = "INSERT INTO `{$database}`.`{$table}` (`{$columns}`) VALUES({$placeholders});"; $pdoStatement = $this->getConnection()->prepare($sql); try { $success = $success && $pdoStatement->execute($values); } catch (\PDOException $e) { throw new PersisterException("ERROR with fixture '{$table}': " . $e->getMessage()); } } $this->getConnection()->query("SET FOREIGN_KEY_CHECKS = 1;"); return $success; }
/** * Adds a fixture to the collection. * * @param Fixture $fixture * @return $this * @throws \TheIconic\Fixtures\Exception\FixtureException */ public function add(Fixture $fixture) { if (isset($this->fixtures[$fixture->getName()])) { throw new FixtureException('Fixture ' . $fixture->getName() . ' already defined'); } else { $this->fixtures[$fixture->getName()] = $fixture; } return $this; }
/** * {@inheritDoc} */ public function replaceValues(Fixture $fixture, array $replacementPlaceholders) { // Only attempt to replace values when fixture is marked for it if (array_key_exists($fixture->getName(), $replacementPlaceholders)) { $replacementPlaceholders = $replacementPlaceholders[$fixture->getName()]; $replacedData = $this->replacePlaceholders($fixture, $replacementPlaceholders); $fixture->setData($replacedData); } return $fixture; }
/** * Parses a MySQL dump XML file. * * @param $source * @return Fixture * @throws \TheIconic\Fixtures\Exception\InvalidParserException */ public function parse($source) { $fixtureArray = []; $z = new \XMLReader(); $z->open($source); $doc = new \DOMDocument(); while ($z->read() && $z->name !== 'table_data') { } $tableName = $z->getAttribute('name'); $rowNum = 0; while ($z->read() && $z->name !== 'row') { } while ($z->name === 'row') { $node = simplexml_import_dom($doc->importNode($z->expand(), true)); $totalAttributes = $node->count(); for ($i = 0; $i < $totalAttributes; $i++) { foreach ($node->field[$i]->attributes() as $attribute) { $attribute = (string) $attribute; $value = (string) $node->field[$i]; $namespaces = $node->field[$i]->getNamespaces(true); if (empty($namespaces)) { $fixtureArray[$tableName][$rowNum][$attribute] = $value; } } } $rowNum++; $z->next('row'); } if (empty($fixtureArray)) { throw new InvalidParserException("It was not possible to parse the XML file: {$source}"); } return Fixture::create($fixtureArray); }
/** * Parses a MySQL dump XML file. * * @param $source * @return Fixture */ public function parse($source) { try { return parent::parse($source); } catch (InvalidParserException $e) { $z = new \XMLReader(); $z->open($source); while ($z->read() && $z->name !== 'table_data') { } $tableName = $z->getAttribute('name'); $fixtureArray[$tableName] = []; return Fixture::create($fixtureArray); } }
/** * Parses a Yaml file. * * @param $source * @return Fixture */ public function parse($source) { $fixtureArray = Yaml::parse(file_get_contents($source)); return Fixture::create($fixtureArray); }
public function setUp() { $this->testFixture = Fixture::create($this->testParsedData); }
/** * {@inheritDoc} Cleans database table before doing so. * * @param Fixture $fixture * @return bool */ public function persist(Fixture $fixture) { $fixtureName = $fixture->getName(); return $this->getConnection()->set($fixtureName, $fixture->getIterator()->getArrayCopy()); }
/** * @expectedException \TheIconic\Fixtures\Exception\PersisterException */ public function testInvalidPersist() { $persister = new TheIconic\Fixtures\Persister\PDO\MysqlPersister($_ENV['pdo_host'], $_ENV['pdo_database'], $_ENV['pdo_username'], $_ENV['pdo_password']); $persister->persist(Fixture::create($this->testParsedBadData)); }