简体   繁体   English

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

[英]Laravel 4, unit tests, extra data returned

bros. 兄弟 Got a strange behaviour. 有一种奇怪的行为。 I'm writing tests for my Laravel 4 ClosureTable package. 我正在为Laravel 4 ClosureTable软件包编写测试。

All tests create new Entity (or Entitites ) for further use. 所有测试都会创建新的Entity (或Entitites )以供进一步使用。 For example, one of the first tests: ClosureTableTestCase::testInsertSingle . 例如,第一个测试之一: ClosureTableTestCase::testInsertSingle When I run it, I got this: 当我运行它时,我得到了:

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', ))

And var_dump says me the following: 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"
  }
}

However when I create an Entity via web interface, all's fine! 但是,当我通过Web界面创建实体时,一切正常!

Errors come from DB::select($selectQuery); 错误来自DB::select($selectQuery); call when I run tests via terminal. 通过终端运行测试时致电。 As you can see, I somehow got [0], [1], and [2] extra array keys. 如您所见,我以某种方式获得了[0],[1]和[2]个额外的数组键。 However when I create an Entity via web interface, all's fine! 但是,当我通过Web界面创建实体时,一切正常!

Error is in this method that's run internally: 此方法在内部运行时出错:

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);
    });
}

Do you can point me out a mistake? 你能指出我一个错误吗?

嗯,好像是从app / config / database.php获取设置是PDO::FETCH_BOTH ,如果不是这样的话..您可以尝试强制设置在DB::select之前获取PDO::FETCH_ASSOC ,方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM