mirror of
https://github.com/Oreolek/yii2-nested-sets.git
synced 2024-06-17 07:10:46 +03:00
Added new ArrayDataSet for unit tests purposes
This commit is contained in:
parent
580e981919
commit
590d8e38c3
62
tests/ArrayDataSet.php
Normal file
62
tests/ArrayDataSet.php
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @link https://github.com/creocoder/yii2-nested-sets-behavior
|
||||||
|
* @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];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue