Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch '3.5'

  • Loading branch information...
commit 08cf185ff6bd8e5ccdf5a416b8659e6736651baf 2 parents 21f788c + 3e6a0a2
@sebastianbergmann authored
View
126 PHPUnit/Extensions/Database/DataSet/MysqlXmlDataSet.php
@@ -0,0 +1,126 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Matthew Turland <tobias382@gmail.com>
+ * @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.5.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+
+require_once 'PHPUnit/Extensions/Database/DataSet/AbstractXmlDataSet.php';
+require_once 'PHPUnit/Extensions/Database/DataSet/Persistors/MysqlXml.php';
+
+PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');
+
+/**
+ * Data set implementation for the output of mysqldump --xml -t.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Matthew Turland <tobias382@gmail.com>
+ * @copyright 2010 Matthew Turland <tobias382@gmail.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: @package_version@
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.5.0
+ */
+class PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet extends PHPUnit_Extensions_Database_DataSet_AbstractXmlDataSet
+{
+
+ protected function getTableInfo(Array &$tableColumns, Array &$tableValues)
+ {
+ if ($this->xmlFileContents->getName() != 'mysqldump') {
+ throw new Exception('The root element of a MySQL XML data set file must be called <mysqldump>');
+ }
+
+ foreach ($this->xmlFileContents->xpath('./database/table_data') as $tableElement) {
+ if (empty($tableElement['name'])) {
+ throw new Exception('<table_data> elements must include a name attribute');
+ }
+
+ $tableName = (string)$tableElement['name'];
+
+ if (!isset($tableColumns[$tableName])) {
+ $tableColumns[$tableName] = array();
+ }
+
+ if (!isset($tableValues[$tableName])) {
+ $tableValues[$tableName] = array();
+ }
+
+ foreach ($tableElement->xpath('./row/field') as $columnElement) {
+ $columnName = (string)$columnElement['name'];
+ if (empty($columnName)) {
+ throw new Exception('<field> element name attributes cannot be empty');
+ }
+
+ if (!in_array($columnName, $tableColumns[$tableName])) {
+ $tableColumns[$tableName][] = $columnName;
+ }
+ }
+
+ foreach ($this->xmlFileContents->xpath('./database/table_data[@name="' . $tableName . '"]/row') as $rowElement) {
+ $rowValues = array();
+ foreach ($tableColumns[$tableName] as $columnName) {
+ $fields = $rowElement->xpath('./field[@name="' . $columnName . '"]');
+ $column = array_shift($fields);
+ $null = isset($column['nil']);
+ $columnValue = $null ? NULL : (string)$column;
+ $rowValues[$columnName] = $columnValue;
+ }
+ $tableValues[$tableName][] = $rowValues;
+ }
+ }
+ }
+
+ public static function write(PHPUnit_Extensions_Database_DataSet_IDataSet $dataset, $filename)
+ {
+ $pers = new PHPUnit_Extensions_Database_DataSet_Persistors_MysqlXml();
+ $pers->setFileName($filename);
+
+ try {
+ $pers->write($dataset);
+ } catch (RuntimeException $e) {
+ throw new PHPUnit_Framework_Exception(__METHOD__ . ' called with an unwritable file.');
+ }
+ }
+}
+?>
View
172 PHPUnit/Extensions/Database/DataSet/Persistors/MysqlXml.php
@@ -0,0 +1,172 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Matthew Turland <tobias382@gmail.com>
+ * @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.5.0
+ */
+
+require_once 'PHPUnit/Framework.php';
+
+require_once 'PHPUnit/Extensions/Database/DataSet/Persistors/Abstract.php';
+
+PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'PHPUNIT');
+
+/**
+ * A MySQL XML dataset persistor.
+ *
+ * @category Testing
+ * @package PHPUnit
+ * @author Matthew Turland <tobias382@gmail.com>
+ * @copyright 2010 Matthew Turland <tobias382@gmail.com>
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD License
+ * @version Release: @package_version@
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.5.0
+ */
+class PHPUnit_Extensions_Database_DataSet_Persistors_MysqlXml extends PHPUnit_Extensions_Database_DataSet_Persistors_Abstract
+{
+ /**
+ * @var string
+ */
+ protected $filename;
+
+ /**
+ * @var string
+ */
+ protected $database;
+
+ /**
+ * @var resource
+ */
+ protected $fh;
+
+ /**
+ * Sets the filename that this persistor will save to.
+ *
+ * @param string $filename
+ */
+ public function setFileName($filename)
+ {
+ $this->filename = $filename;
+ }
+
+ /**
+ * Sets the name of the database.
+ *
+ * @param string $database
+ */
+ public function setDatabase($database)
+ {
+ $this->database = $database;
+ }
+
+ /**
+ * Override to save the start of a dataset.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataset
+ */
+ protected function startDataSet(PHPUnit_Extensions_Database_DataSet_IDataSet $dataset)
+ {
+ $this->fh = fopen($this->filename, 'w');
+
+ if ($this->fh === FALSE) {
+ throw new PHPUnit_Framework_Exception("Could not open {$this->filename} for writing see " . __CLASS__ . "::setFileName()");
+ }
+
+ fwrite($this->fh, '<?xml version="1.0" encoding="UTF-8"?>' . "\n");
+ fwrite($this->fh, '<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' . "\n");
+ fwrite($this->fh, '<database name="' . $this->database . '">' . "\n");
+ }
+
+ /**
+ * Override to save the end of a dataset.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_IDataSet $dataset
+ */
+ protected function endDataSet(PHPUnit_Extensions_Database_DataSet_IDataSet $dataset)
+ {
+ fwrite($this->fh, '</database>' . "\n");
+ fwrite($this->fh, '</mysqldump>' . "\n");
+ }
+
+ /**
+ * Override to save the start of a table.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_ITable $table
+ */
+ protected function startTable(PHPUnit_Extensions_Database_DataSet_ITable $table)
+ {
+ fwrite($this->fh, "\t" . '<table_data name="' . $table->getTableMetaData()->getTableName() . '">' . "\n");
+ }
+
+ /**
+ * Override to save the end of a table.
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_ITable $table
+ */
+ protected function endTable(PHPUnit_Extensions_Database_DataSet_ITable $table)
+ {
+ fwrite($this->fh, "\t" . '</table_data>' . "\n");
+ }
+
+ /**
+ * Override to save a table row.
+ *
+ * @param array $row
+ * @param PHPUnit_Extensions_Database_DataSet_ITable $table
+ */
+ protected function row(Array $row, PHPUnit_Extensions_Database_DataSet_ITable $table)
+ {
+ fwrite($this->fh, "\t" . '<row>' . "\n");
+
+ foreach ($table->getTableMetaData()->getColumns() as $columnName) {
+ fwrite($this->fh, "\t\t" . '<field name="' . $columnName . '"');
+ if (isset($row[$columnName])) {
+ fwrite($this->fh, '>' . htmlspecialchars($row[$columnName]) . '</field>' . "\n");
+ } else {
+ fwrite($this->fh, ' xsi:nil="true" />' . "\n");
+ }
+ }
+
+ fwrite($this->fh, "\t" . '</row>' . "\n");
+ }
+}
+?>
View
13 PHPUnit/Extensions/Database/TestCase.php
@@ -182,6 +182,19 @@ protected function createXMLDataSet($xmlFile)
}
/**
+ * Create a a new MysqlXmlDataSet with the given $xmlFile. (absolute path.)
+ *
+ * @param string $xmlFile
+ * @return PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet
+ * @since Method available since Release 3.5.0
+ */
+ protected function createMySQLXMLDataSet($xmlFile)
+ {
+ require_once 'PHPUnit/Extensions/Database/DataSet/MysqlXmlDataSet.php';
+ return new PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet($xmlFile);
+ }
+
+ /**
* Returns an operation factory instance that can be used to instantiate
* new operations.
*
View
9 PHPUnit/Tests/Extensions/Database/DataSet/XmlDataSetsTest.php
@@ -49,6 +49,7 @@
require_once 'PHPUnit/Extensions/Database/DataSet/DefaultTableMetaData.php';
require_once 'PHPUnit/Extensions/Database/DataSet/FlatXmlDataSet.php';
require_once 'PHPUnit/Extensions/Database/DataSet/XmlDataSet.php';
+require_once 'PHPUnit/Extensions/Database/DataSet/MysqlXmlDataSet.php';
require_once 'PHPUnit/Extensions/Database/Constraint/DataSetIsEqual.php';
/**
@@ -138,5 +139,13 @@ public function testXmlDataSet()
self::assertThat($xmlDataSet, $constraint);
}
+
+ public function testMysqlXmlDataSet()
+ {
+ $constraint = new PHPUnit_Extensions_Database_Constraint_DataSetIsEqual($this->expectedDataSet);
+ $mysqlXmlDataSet = new PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet(dirname(__FILE__).'/../_files/XmlDataSets/MysqlXmlDataSet.xml');
+
+ self::assertThat($mysqlXmlDataSet, $constraint);
+ }
}
?>
View
51 PHPUnit/Tests/Extensions/Database/_files/XmlDataSets/MysqlXmlDataSet.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<database name="database_name">
+ <table_data name="table1">
+ <row>
+ <field name="table1_id">1</field>
+ <field name="column1">tgfahgasdf</field>
+ <field name="column2">200</field>
+ <field name="column3">34.64</field>
+ <field name="column4">yghkf;a hahfg8ja h;</field>
+ </row>
+ <row>
+ <field name="table1_id">2</field>
+ <field name="column1">hk;afg</field>
+ <field name="column2">654</field>
+ <field name="column3">46.54</field>
+ <field name="column4">24rwehhads</field>
+ </row>
+ <row>
+ <field name="table1_id">3</field>
+ <field name="column1">ha;gyt</field>
+ <field name="column2">462</field>
+ <field name="column3">1654.4</field>
+ <field name="column4">asfgklg</field>
+ </row>
+ </table_data>
+ <table_data name="table2">
+ <row>
+ <field name="table2_id">1</field>
+ <field name="column5">fhah</field>
+ <field name="column6">456</field>
+ <field name="column7">46.5</field>
+ <field name="column8">fsdbghfdas</field>
+ </row>
+ <row>
+ <field name="table2_id">2</field>
+ <field name="column5">asdhfoih</field>
+ <field name="column6">654</field>
+ <field name="column7" xsi:nil="true" />
+ <field name="column8">43asdfhgj</field>
+ </row>
+ <row>
+ <field name="table2_id">3</field>
+ <field name="column5">ajsdlkfguitah</field>
+ <field name="column6">654</field>
+ <field name="column7" xsi:nil="true" />
+ <field name="column8" xsi:nil="true" />
+ </row>
+ </table_data>
+</database>
+</mysqldump>
View
6 package.xml
@@ -52,6 +52,9 @@
<file baseinstalldir="/" name="FlatXml.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
+ <file baseinstalldir="/" name="MysqlXml.php" role="php">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
<file baseinstalldir="/" name="Xml.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
@@ -139,6 +142,9 @@
<file baseinstalldir="/" name="ITableMetaData.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
+ <file baseinstalldir="/" name="MysqlXmlDataSet.php" role="php">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
<file baseinstalldir="/" name="QueryDataSet.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Please sign in to comment.
Something went wrong with that request. Please try again.