1

我在测试期间无法触发“进度”回调。

这是要测试的代码:

    $this->guzzleClient->request(
        'GET',
        'http://example.com/somefile.csv',
        [
            'sink' => $this->directory . $this->filename . '.csv',
            'progress' => function ($download_size, $downloaded, $upload_size, $uploaded) {
                $this->downloadProgress($download_size, $downloaded, $upload_size, $uploaded);
            },
        ]);

我能够模拟响应并保存文件,但它永远不会触发“进度”。注意:我使用的响应选项与我从实时服务器获得的响应选项相同。

    $mock = new MockHandler([
        new Response(
            '206',
            [
                'content-type' => 'application/octet-stream',
                'Content-Range' => 'bytes 1113-1113/11591523',
            ],
            new Stream(fopen(__DIR__ . '/test_stream_file.txt', 'r'))
        )
    ]);

    $handler = HandlerStack::create($mock);
    $client = new Client(['handler' => $handler()]);

我正在考虑仅测试模拟文件的下载,然后downloadProgress如果这是我唯一的选择,则单独测试该方法。

4

1 回答 1

1

MockHandler没有实现“进度”请求选项。

测试处理程序触发进度回调的能力将是 Guzzle 测试套件的重复。具体来说:

  1. CurlFactoryTest::testEmitsProgress ; 和
  2. StreamHandlerTest::testEmitsProgressInformation

If your goal is to test to ensure that the callback performs intended operations, separate that into a different test.

If your goal is to test handler capability, I refer you to the Guzzle Test Suite.

CurlFactory is the default handler for non-Windows systems.

StreamHandler is the default handler for Windows systems.

于 2016-06-23T12:47:22.970 回答