简体   繁体   中英

How to test a zend expressive rest api controller using PHPUnit?

I'm trying to learn PHPUnit by testing a rest api controller built using zend expressive, but couldn't find any resources online regarding how to perform the same. The code in Official documentation for zend framework is not working for zend expressive. So I have tried following PHPUnit documentation .

Here's what I have done as of now:-

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
    }
}

But when i run the above code, it throws this error:

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

I really don't know if my approach for testing rest api controllers using PHPUnit is correct, since I'm new to php and phpunit. I'd appreciate any help in pointing me to the right direction.

Okay, your connection works but \\Zend\\Db\\Adapter don't know how to connect db. Sql object requires an Adapter so i guess you're doing something like that in your Model.php on line 453:

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

So your adapter factory works and checks for config for db-connection. But you didn't initialize freamwork. You didn't read your config, your modules didn't send their config. Because you just created your controller with new keyword.

It's not freamwork running atm, it's just phpunit. That's why Adapter couldn't find database. You have to set-up freamwork.

You could set adapter via __construct method if you wrote your code on Dependency Injection pattern. That's one of reaseons we're following SOLID principles: Testable Code . But seems like you are accessing your adapter via abstraction. So you have to set up freamwork to test that action.

This's what i understood from your code. If anything is wrong, please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM