1
0
Fork 0
mirror of https://github.com/Oreolek/yii2-nested-sets.git synced 2024-06-17 07:10:46 +03:00
yii2-nested-sets/tests/ArrayDataSet.php
2015-01-05 11:03:13 +03:00

63 lines
1.5 KiB
PHP

<?php
/**
* @link https://github.com/creocoder/yii2-nested-sets
* @copyright Copyright (c) 2015 Alexander Kochetov
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace tests;
/**
* ArrayDataSet
*/
class ArrayDataSet extends \PHPUnit_Extensions_Database_DataSet_AbstractDataSet
{
/**
* @var array
*/
protected $tables = array();
/**
* @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;
}
}
/**
* @inheritdoc
*/
protected function createIterator($reverse = false)
{
return new \PHPUnit_Extensions_Database_DataSet_DefaultTableIterator($this->tables, $reverse);
}
/**
* @inheritdoc
*/
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];
}
}