【问题标题】:How to test a zend expressive rest api controller using PHPUnit?如何使用 PHPUnit 测试一个 zend 表达性休息 api 控制器?
【发布时间】:2018-08-15 05:57:09
【问题描述】:

我正在尝试通过测试使用 zend expressive 构建的 rest api 控制器来学习 PHPUnit,但在网上找不到任何关于如何执行相同操作的资源。 Official documentation for zend framework 中的代码不适用于 zend expressive。所以我尝试关注PHPUnit documentation

这是我目前所做的:-

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 测试 rest api 控制器的方法是否正确,因为我是 php 和 phpunit 的新手。如果能帮助我指出正确的方向,我将不胜感激。

【问题讨论】:

  • 好吧..这是数据库连接错误。页面可以在浏览器上运行吗?如果您在配置 freamwrok 时遇到问题。你检查过这个页面吗? zendframework.github.io/zend-test/phpunit/…?
  • @MehmetSÖĞÜNMEZ 如果我在 getDataSet() 方法中执行此操作,它将显示表格。 $dataset = $this->getConnection()->createDataSet()->getTable('account_view'); print $dataset; 同样正如我所提到的,文档中的代码不起作用。它找不到 Zend\Test 模块。

标签: php unit-testing zend-framework phpunit mezzio


【解决方案1】:

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

$sql = new Sql($container->get('Adapter');

所以您的适配器工厂工作并检查 db-connection 的配置。但是你没有初始化 framwork。你没有阅读你的配置,你的模块没有发送他们的配置。因为您刚刚使用 new 关键字创建了控制器。

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

如果您在Dependency Injection 模式上编写代码,则可以通过__construct 方法设置适配器。这是我们遵循SOLID 原则的原因之一:Testable Code。但似乎您正在通过抽象访问您的适配器。所以你必须设置框架来测试这个动作。

这是我从您的代码中了解到的。如果有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 2011-08-08
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2012-07-16
    • 2017-03-10
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多