This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from richardbrinkman/master
Added the ArrayDataSet as mentioned in the documentation.
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
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,76 @@ | ||
<?php | ||
/* | ||
* This file is part of DBUnit. | ||
* | ||
* (c) Sebastian Bergmann <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Implements the basic functionality of data sets using a PHP array. | ||
* | ||
* @package DbUnit | ||
* @author Richard Brinkman <[email protected]> | ||
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License | ||
* @version Release: @package_version@ | ||
* @link http://www.phpunit.de/ | ||
* @since Class available since Release 1.3.2 | ||
*/ | ||
class PHPUnit_Extensions_Database_DataSet_ArrayDataSet extends PHPUnit_Extensions_Database_DataSet_AbstractDataSet | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $tables = array(); | ||
|
||
/** | ||
* Constructor to build a new ArrayDataSet with the given array. | ||
* The array parameter is an associative array of tables where the key is | ||
* the table name and the value an array of rows. Each row is an associative | ||
* array by itself with keys representing the field names and the values the | ||
* actual data. | ||
* For example: | ||
* array( | ||
* "addressbook" => array( | ||
* array("id" => 1, "name" => "...", "address" => "..."), | ||
* array("id" => 2, "name" => "...", "address" => "...") | ||
* ) | ||
* ) | ||
* | ||
* @param array $data | ||
*/ | ||
public function __construct(array $data) | ||
{ | ||
foreach ($data AS $tableName => $rows) { | ||
$columns = array(); | ||
if (isset($rows[0])) { | ||
$columns = array_keys($rows[0]); | ||
} | ||
|
||
$metaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData($tableName, $columns); | ||
$table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($metaData); | ||
|
||
foreach ($rows AS $row) { | ||
$table->addRow($row); | ||
} | ||
$this->tables[$tableName] = $table; | ||
} | ||
} | ||
|
||
protected function createIterator($reverse = FALSE) | ||
{ | ||
return new PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($this->tables, $reverse); | ||
} | ||
|
||
public function getTable($tableName) | ||
{ | ||
if (!isset($this->tables[$tableName])) { | ||
throw new InvalidArgumentException("$tableName is not a table in the current database."); | ||
} | ||
|
||
return $this->tables[$tableName]; | ||
} | ||
} | ||
?> |
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