如何使用PHPUnit测试zend表达式rest api控制器?

如何使用PHPUnit测试zend表达式rest api控制器?,php,unit-testing,zend-framework,phpunit,mezzio,Php,Unit Testing,Zend Framework,Phpunit,Mezzio,我试图通过测试一个使用zend expressive构建的rest api控制器来学习PHPUnit,但是在网上找不到任何关于如何执行相同操作的资源。中的代码不适用于zend expressive。所以我试着跟随 以下是我到目前为止所做的工作:- use App1\Api\AccountApi; use PHPUnit\Framework\TestCase; use PHPUnit\DbUnit\TestCaseTrait; class AccountTest extends TestCase

我试图通过测试一个使用zend expressive构建的rest api控制器来学习PHPUnit,但是在网上找不到任何关于如何执行相同操作的资源。中的代码不适用于zend expressive。所以我试着跟随

以下是我到目前为止所做的工作:-

use App1\Api\AccountApi;
use PHPUnit\Framework\TestCase;
use PHPUnit\DbUnit\TestCaseTrait;

class AccountTest extends TestCase {
    use TestCaseTrait;
    static private $pdo = null;

    private $conn = null;

    public function setUp(){
       /* Avoiding truncate operation of getDataSet() since we are trying to access view and not a table from database. */
    }


    public function getConnection() {
        if ($this->conn === null) {
            if (self::$pdo == null) {
                self::$pdo = new PDO('mysql:host=localhost;dbname=workflow','root', 'root');
            }
            $this->conn = $this->createDefaultDBConnection(self::$pdo, ':host=localhost;dbname=workflow');
        }

        return $this->conn;
    }

    public function getDataSet()
    {
        $dataset = $this->getConnection()->createDataSet();
        return $dataset;
    }

    public function testIndexAction()
    {
        $api = new AccountApi();
        $response = $api->indexAction();  // Invokes a findAll() operation in the model which basically does a 'select * from workflow.account'
        print $response; // to see if a response is returning
    }
}
但当我运行上述代码时,它会抛出以下错误:

1) AgentTest::testIndexAction
Zend\Db\Adapter\Exception\RuntimeException: Connect Error: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is k
nown.

C:\webserver\GIT\gco3api\vendor\zendframework\zend-db\src\Adapter\Driver\Pdo\Connection.php:283
C:\webserver\GIT\gco3api\vendor\zendframework\zend-db\src\Adapter\Driver\Pdo\Pdo.php:255
C:\webserver\GIT\gco3api\vendor\zendframework\zend-db\src\Sql\Sql.php:138
C:\webserver\GIT\gco3api\src\Core\src\Abstracts\Model.php:453
C:\webserver\GIT\gco3api\src\Core\src\Abstracts\Model.php:422
C:\webserver\GIT\gco3api\src\App1\src\Api\AccountApi.php:28
C:\webserver\GIT\gco3api\test\AppTest\App1\AccountTest.php:52

我真的不知道我使用PHPUnit测试restapi控制器的方法是否正确,因为我是php和PHPUnit新手。如果您能帮我指出正确的方向,我将不胜感激。

好的,您的连接正常,但
\Zend\Db\Adapter
不知道如何连接Db
Sql
对象需要一个
适配器
,所以我猜您在第453行的
Model.php
中做了类似的事情:

$sql = new Sql($container->get('Adapter');
因此,适配器工厂可以工作并检查db连接的配置。但你没有初始化freamwork。您没有读取配置,您的模块没有发送配置。因为您刚刚用
new
关键字创建了控制器

不是freamwork在运行atm,只是phpunit。这就是
Adapter
找不到数据库的原因。你必须设置freamwork

如果您在依赖项注入模式下编写代码,则可以通过
\u construct
方法设置适配器。这是我们遵循的原则之一:
可测试代码
。但看起来您是通过抽象访问适配器的。因此,您必须设置freamwork来测试该操作


这就是我从你的代码中理解的。如果有任何问题,请告诉我。

好的,您的连接正常,但
\Zend\Db\Adapter
不知道如何连接数据库
Sql
对象需要一个
适配器
,所以我猜您在第453行的
Model.php
中做了类似的事情:

$sql = new Sql($container->get('Adapter');
因此,适配器工厂可以工作并检查db连接的配置。但你没有初始化freamwork。您没有读取配置,您的模块没有发送配置。因为您刚刚用
new
关键字创建了控制器

不是freamwork在运行atm,只是phpunit。这就是
Adapter
找不到数据库的原因。你必须设置freamwork

如果您在依赖项注入模式下编写代码,则可以通过
\u construct
方法设置适配器。这是我们遵循的原则之一:
可测试代码
。但看起来您是通过抽象访问适配器的。因此,您必须设置freamwork来测试该操作


这就是我从你的代码中理解的。如果有什么不对劲,请告诉我。

好的。。这是数据库连接错误。页面在浏览器上工作吗?如果您在配置freamwrok时遇到问题。你看过这一页了吗@MehmetSĞÜNMEZ如果我在getDataSet()方法中执行此操作,它将显示表<代码>$dataset=$this->getConnection()->createDataSet()->getTable('account_view');打印$dataset正如我所提到的,文档中的代码不起作用。它找不到Zend\Test模块。嗯。。这是数据库连接错误。页面在浏览器上工作吗?如果您在配置freamwrok时遇到问题。你看过这一页了吗@MehmetSĞÜNMEZ如果我在getDataSet()方法中执行此操作,它将显示表<代码>$dataset=$this->getConnection()->createDataSet()->getTable('account_view');打印$dataset正如我所提到的,文档中的代码不起作用。找不到Zend\Test模块。