首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHPUnit测试zend expressive rest api控制器?

如何使用PHPUnit测试zend expressive rest api控制器?
EN

Stack Overflow用户
提问于 2018-03-07 15:51:26
回答 1查看 648关注 0票数 0

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

这是我到目前为止所做的:

代码语言:javascript
复制
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
    }
}

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

发布于 2018-03-07 19:15:14

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

代码语言:javascript
复制
$sql = new Sql($container->get('Adapter');

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

它不是运行自动取款机的freamwork,它只是phpunit。这就是Adapter找不到数据库的原因。你必须设置freamwork。

如果您的代码是在Dependency Injection模式上编写的,则可以通过__construct方法设置适配器。这就是我们遵循SOLID原则的原因之一:Testable Code。但是看起来你是通过抽象来访问适配器的。因此,您必须设置freamwork来测试该操作。

这就是我从你的代码中理解的。如果有什么问题,请让我知道。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49146710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档