PHPUnit:如何要求我的应用程序配置文件


PHPUnit: How to require my apps config file

我有一个文件叫application.config.php在我的应用程序根目录。我想在测试中需要它或自动加载它。配置文件如下所示:

<?php
// database connection
$config = array(
  'database' => array(
    'dsn' => 'mysql:host=localhost;dbname=budgetz',
    'user' => 'budgetz_user',
    'password' => 't1nth3p4rk',
  ),
);

我的应用程序使用这些连接到数据库。所以,为了测试我的模型,他们还需要连接到数据库,…或者某个数据库。是否只是在测试文件中要求它的行中的一些东西的问题:

<?php
require_once 'vendor/autoload.php';
require_once 'application.config.php';
class MapperTest extends PHPUnit_Framework_TestCase {
    public function testFetchOne() {
        $dbAdapter = new DatabaseAdapter($config['database']);
        $userMapper = new UserMapper($dbAdapter); // using UserMapper but any child of Mapper will do
        $user = $userMapper->fetchOne(1);
        $this->assertsEquals(1, $user->id, 'message');
    }
}

我试过了,但是我得到了错误:

There was 1 error:
1) MapperTest::testFetchOne
Undefined variable: config
/var/www/new_orm/test/MapperTest.php:8

我做错了什么?此外,我感谢任何人在这里提供一些最佳实践的建议。也许这种要求每个页面都有一个配置文件的方法有点过时了。由于

全局变量是一个选项,但不是一个好选项。创建类,扩展PHPUnit_Framework_TestCase。然后使用setup设置您的配置。例如

class myTestCase  extends PHPUnit_Framework_TestCase {
        private $config;
        public function setUp() {
            $this->config = ....
        }
        public function getConfig() {
           return $this->configl
        }

那么您的测试用例应该扩展myTestCase。你可以用

访问config
$this->getConfig();

无论如何,访问dev db不是一个好主意,也许mockwork with the db更好?

Try

public function testFetchOne() {
    global $config;
相关文章: