を使用し、モック応答を使用してサーバーをエンキューする。
ServiceClient
GuzzlephPを使用した効率的な単体テストでは、Composerを使用したテスト環境のセットアップ、PHPunitの構成、およびテストクラスの作成が含まれます。
手作りのカスタム応答は、さまざまなAPIシナリオをシミュレートし、単体テストを強化します。ServiceClient
:でディレクトリを作成します
:composer.json
{ "require": { "php": ">=5.3.3" }, "require-dev": { "phpunit/phpunit": "4.0.*", "guzzle/guzzle": "~3.7" } }
:composer install
tests
bootstrap.php
phpunit.xml.dist
ディレクトリで
bootstrap.php
<?php error_reporting(E_ALL | E_STRICT); require dirname(__DIR__) . '/vendor/autoload.php';
phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8"?> <phpunit bootstrap="./bootstrap.php" colors="true"> <testsuites> <testsuite name="Guzzle Tests"> <directory suffix="Test.php"></directory> </testsuite> </testsuites> </phpunit>
SitePointGuzzleTest.php
よくある質問(FAQ):tests
<?php use Guzzle\Tests\GuzzleTestCase; use Guzzle\Plugin\Mock\MockPlugin; use Guzzle\Http\Message\Response; use Guzzle\Http\Client as HttpClient; use Guzzle\Service\Client as ServiceClient; use Guzzle\Http\EntityBody; class SitePointGuzzleTest extends GuzzleTestCase { protected $_client; }
Guzzlephpとphpunitを統合します。
処理例外。
非同期リクエストのテスト。ServiceClient
投稿リクエスト、ファイルのアップロード、リダイレクト、およびCookieのテスト
提供されたFAQは、各シナリオのベストプラクティスを示す簡潔なコードの例を提供します。 これにより、この記事は、GuzzlephPの単位テストスキルを改善しようとする開発者にとって貴重なリソースになります。以上がガズルによる単体テストの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。