-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
377 additions
and
0 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
PHPUnit/Extensions/Database/DataSet/MysqlXmlDataSet.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
} | ||
} | ||
} | ||
?> |
172 changes: 172 additions & 0 deletions
172
PHPUnit/Extensions/Database/DataSet/Persistors/MysqlXml.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
PHPUnit/Tests/Extensions/Database/_files/XmlDataSets/MysqlXmlDataSet.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.