如何在带有数据库的Phalcon框架中配置存根连接(Pgsql)?

时间:2019-04-26 06:45:12

标签: php unit-testing phalcon stub phpunit-testing

use Phalcon\Db;
use Phalcon\Di;
use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;

ini_set("display_errors", 1);
error_reporting(E_ALL);
//define("ROOT_PATH",dirname(__DIR__));
define("APP_PATH",substr(__DIR__,0,strrpos(__DIR__,DIRECTORY_SEPARATOR)));
set_include_path(APP_PATH . PATH_SEPARATOR . get_include_path());

// Required for phalcon/incubator
include APP_PATH . "/app/library/incubator-master/vendor/autoload.php";
include APP_PATH . "/tests/UnitTestCase.php";
include APP_PATH . "/app/config/LoggerStub.php";
// Use the application autoloader to autoload the classes
// Autoload the dependencies found in composer

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader = new Phalcon\Loader();
$loader->registerDirs([
    APP_PATH,
    APP_PATH . "/app/helpers",
    APP_PATH . "/app/model"
]);

$loader->register();
$di = new FactoryDefault();
Di::reset();

// Add any needed services to the DI here
Di::setDefault($di);

$configFile = APP_PATH . "/app/config/config.php";

if (is_readable($configFile)) {
    $config = include $configFile;

    $di->set("config", $config);

}

$di->set('db', function() use ($config) {
    try {
            $dbConfig = $config->database->toArray();
            $adapter = $dbConfig['adapter'];
            unset($dbConfig['adapter']);

            $stub = $this->getMockBuilder('Phalcon\Db\Adapter\Pdo\\' . $adapter)
                ->disableOriginalConstructor()
                ->getMock();


            $stub->expects($this->at(0))
                ->method('tableExists')
                ->will($this->returnCallback(function(){
                    return true;
                }));

            $stub->expects($this->at(1))
                ->method('getMetadata')
                ->will($this->returnCallback(function(){
                    // This was where I realized that I have to stub out
                    // all calls made internally by Phalcon\Mvc\Model
                }));


            return $stub;
    }
    catch (Exception $e) {
        die("Test case not able run");
    }
});

我正在为Phalcon框架进行Phpunit测试。我的问题在这段代码中。我正在使用上面的代码与pgsql数据库进行存根连接以创建要与数据库绑定的模拟对象,但这不会损害数据库。现在的问题是它不能与存根一起使用,而存根是从此函数“ getMockBuilder()”开始使用的。

0 个答案:

没有答案