我正在尝试通过PhpUnit学习API测试。我无法理解它的流程?

时间:2016-07-11 07:37:27

标签: php testing yii phpunit

我遇到了以下代码,我只想问一些与该代码相关的问题,以便编写自己的代码?

代码是

<?php

require('vendor/autoload.php');

class BooksTest extends PHPUnit_Framework_TestCase

{ protected $client;

protected function setUp()
{
    $this->client = new GuzzleHttp\Client([
        'base_uri' => 'http://mybookstore.com'
    ]);
}

public function testGet_ValidInput_BookObject()
{
    $response = $this->client->get('/books', [
        'query' => [
            'bookId' => 'hitchhikers-guide-to-the-galaxy'
        ]
    ]);

    $this->assertEquals(200, $response->getStatusCode());

    $data = json_decode($response->getBody(), true);

    $this->assertArrayHasKey('bookId', $data);
    $this->assertArrayHasKey('title', $data);
    $this->assertArrayHasKey('author', $data);
    $this->assertEquals(42, $data['price']);
}

}

我的问题是:

1&GT;这是什么意思'base_uri'=&gt; 'http://mybookstore.com'?

1 个答案:

答案 0 :(得分:0)

base_uri是进行测试的uri。在这种情况下http://bookstore.com。在你的函数testGet_ValidInput_BookObject()中,使用get'/ books'然后它会在http://bookstore.com/books中查找。