예제 #1
0
 /**
  * {@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;
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 /**
  * {@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;
 }
예제 #4
0
 /**
  * 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);
 }
예제 #5
0
 /**
  * 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);
     }
 }
예제 #6
0
 /**
  * Parses a Yaml file.
  *
  * @param $source
  * @return Fixture
  */
 public function parse($source)
 {
     $fixtureArray = Yaml::parse(file_get_contents($source));
     return Fixture::create($fixtureArray);
 }
예제 #7
0
 public function setUp()
 {
     $this->testFixture = Fixture::create($this->testParsedData);
 }
예제 #8
0
 /**
  * {@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());
 }
예제 #9
0
 /**
  * @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));
 }