容器实例化不起作用

时间:2017-08-16 06:55:49

标签: php mysql laravel laravel-5

我正试图从测试功能中的表中获取所有记录 调用DB::table('degrees')->get()会返回所有预期值,
但是尝试运行以下代码会返回一个空集合:

$db = $this->app->make('db');
$db->connection('testing');
$db->table('degrees')->get();

我能想到的唯一问题是,项目配置不正确。

我的phpunit.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
 <phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="bootstrap/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false">
  <testsuites>
    <testsuite name="Feature">
        <directory suffix="Test.php">./tests/Feature</directory>
  </testsuite>

  <testsuite name="Unit">
        <directory suffix="Test.php">./tests/Unit</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">./app</directory>
    </whitelist>
  </filter>
  <php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
  </php>
</phpunit>

我的createApplication功能:

/**
 * Creates the application.
 *
 * @return Application
 */
public function createApplication(): Application
{
    $app = require __DIR__ . '/../bootstrap/app.php';
    $app->make(Kernel::class)->bootstrap();
    return $app;
}

.env.testing

的数据库配置
DB_CONNECTION=testing
DB_HOST=192.168.70.10
DB_DATABASE=testing
DB_USERNAME=homestead
DB_PASSWORD=secret  

database.php档案

'testing' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'testing'),
        'username' => env('DB_USERNAME', 'homestead'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ]

我可以通过致电Degree::all()DB::table('degrees')->get()确认表格中有数据,但可以打电话

$db=$this->app->make('db');
$db->connection('testing');
$db->table('degrees')->get();

返回空Collection

Laravel:5.4.0, PhpUnit:5.7.21

0 个答案:

没有答案