Laravel 4,单元测试,返回额外数据

时间:2013-09-17 03:04:07

标签: php unit-testing laravel

兄弟。有一种奇怪的行为。我正在为我的Laravel 4 ClosureTable包编写测试。

所有测试都会创建新的Entity(或Entitites)以供进一步使用。例如,第一个测试之一:ClosureTableTestCase::testInsertSingle。当我运行它时,我得到了这个:

Exception: SQLSTATE[HY000]: General error: 1 table pages_closure has no column named 0 (SQL: insert into "pages_closure" ("ancestor", "depth", "descendant", "0", "1", "2") values (?, ?, ?, ?, ?, ?)) (Bindings: array ( 0 => '1', 1 => '0', 2 => '1', 3 => '1', 4 => '1', 5 => '0', ))

var_dump我说了以下内容:

array(1) {
  [0] =>
  array(6) {
    'ancestor' =>
    string(1) "1"
    [0] =>
    string(1) "1"
    'descendant' =>
    string(1) "1"
    [1] =>
    string(1) "1"
    'depth' =>
    string(1) "0"
    [2] =>
    string(1) "0"
  }
}

但是当我通过网络界面创建实体时,一切都很好!

当我通过终端运行测试时,错误来自DB::select($selectQuery);调用。如你所见,我以某种方式获得了[0],[1]和[2]额外的数组键。 但是当我通过网络界面创建实体时,一切都很好!

此方法在内部运行时出错:

protected function performInsertNode($descendant, $ancestor)
{
    $table = $this->closure;
    $ak = static::ANCESTOR;
    $dk = static::DESCENDANT;
    $dpk = static::DEPTH;

    DB::transaction(function() use($table, $ak, $dk, $dpk, $descendant, $ancestor){
        $selectQuery = "
            SELECT tbl.{$ak} as {$ak}, {$descendant} as {$dk}, tbl.{$dpk}+1 as {$dpk}
            FROM {$table} AS tbl
            WHERE tbl.{$dk} = {$ancestor}
            UNION ALL
            SELECT {$descendant}, {$descendant}, 0
        ";

        $results = DB::select($selectQuery);
        array_walk($results, function(&$item){ $item = (array)$item; });

        var_dump($results);

        DB::table($table)->insert($results);
    });
}

你能指出我的错误吗?

1 个答案:

答案 0 :(得分:1)

嗯,好像app / config / database.php中的抓取设置是PDO::FETCH_BOTH,如果不是那样..你可以尝试强制设置在PDO::FETCH_ASSOC之前得到DB::select用:

Config::set('database.fetch', PDO::FETCH_ASSOC);