extended_mock_http_client
v4.0.1
Versi | Membangun Status | Cakupan Kode |
---|---|---|
master |
composer require macpaw/extended_mock_http_client
Dalam file konfigurasi config/services_test.yaml
ganti layanan klien HTTP saat ini
imports :
- { resource: services.yaml }
services :
_defaults :
autowire : true
autoconfigure : true
public : true
http_client_service_name :
class : ExtendedMockHttpClientExtendedMockHttpClient
arguments :
- ' https://test.host '
Itu saja, Anda dapat menggunakannya dalam pengujian PHPUnit
abstract class AbstractFunctionalTest extends KernelTestCase
{
private ExtendedMockHttpClient $ httpClient
protected function setUp (): void
{
/** @var ExtendedMockHttpClient $mockHttpClient */
$ this -> httpClient = self :: getContainer ()-> get ( ' http_client_service_name ' );
}
}
class MyTest extends AbstractFunctionalTest
{
/**
* Create simple request using createFixture
* Request with almost empty parameters
* Check response and check called times
*/
public function testSimpleExample1 (): void
{
$ httpFixture = $ this -> client -> createFixture (
' POST ' ,
' https://test.test/foo?foo=bar ' ,
null ,
null ,
200 ,
' ok '
);
$ this -> client -> addFixture ( $ httpFixture );
$ response = $ this -> client -> request ( ' POST ' , ' https://test.test/foo?foo=bar ' );
self :: assertEquals ( 200 , $ response -> getStatusCode ());
self :: assertEquals ( ' ok ' , $ response -> getContent ());
self :: assertEquals ( 1 , $ httpFixture -> getCalledTimes ());
}
/**
* Make simple fixture using createFixture
* Request using json
* Check response
*/
public function testSimpleExample2 (): void
{
$ httpFixture = $ this -> client -> createFixture (
' POST ' ,
' https://test.test/foo?foo=bar ' ,
' {"foo":"bar","baz":123} ' ,
[
' x-header ' => ' x-value ' ,
],
200 ,
' ok '
);
$ this -> client -> addFixture ( $ httpFixture );
$ response = $ this -> client -> request ( ' POST ' , ' https://test.test/foo?foo=bar ' , [
' json ' => [
' foo ' => ' bar ' ,
' baz ' => 123
],
' headers ' => [
' x-header ' => ' x-value ' ,
]
]);
self :: assertEquals ( 200 , $ response -> getStatusCode ());
self :: assertEquals ( ' ok ' , $ response -> getContent ());
}
}
class MyTest extends AbstractFunctionalTest
{
/**
* Make fixture using builder
* Request using json
* Check response
*/
public function testBuilderExample1 (): void
{
$ builder = $ this -> client -> getHttpFixtureBuilder ();
$ httpFixture = $ builder
-> request (
$ builder -> method ([ ' PUT ' , ' POST ' ]),
$ builder -> url ( ' https://test.test/foo ' ),
$ builder -> query ([
' foo ' => ' bar ' ,
]),
$ builder -> body ( $ builder -> jsonToArray (
$ builder -> arrayContain ([
' foo ' => ' bar ' ,
])
)),
$ builder -> headers ([
' x-header ' => ' x-value ' ,
])
)
-> response ( 200 , ' ok ' )
-> build ();
$ this -> client -> addFixture ( $ httpFixture );
$ response = $ this -> client -> request ( ' POST ' , ' https://test.test/foo?foo=bar ' , [
' json ' => [
' foo ' => ' bar ' ,
' baz ' => 123
],
' headers ' => [
' x-header ' => ' x-value ' ,
]
]);
self :: assertEquals ( 200 , $ response -> getStatusCode ());
self :: assertEquals ( ' ok ' , $ response -> getContent ());
}
/**
* Make fixture using builder with MockResponse
* Request using json
* Check response
*/
public function testBuilderExample2 (): void
{
$ builder = $ this -> client -> getHttpFixtureBuilder ();
$ httpFixture = $ builder
-> request (
$ builder -> method ( ' POST ' ),
$ builder -> url ( ' https://test.test/foo ' ),
$ builder -> query ( $ builder -> queryToArray ( $ builder -> arrayContain ([
' foo ' => ' bar ' ,
]))),
$ builder -> body ( $ builder -> stringRegex ( ' /"foo":"bar"/ ' )),
$ builder -> headers ([
' x-header ' => ' x-value ' ,
])
)
-> response ( new MockResponse ( ' ok ' , [ ' http_code ' => 200 ]))
-> build ();
$ this -> client -> addFixture ( $ httpFixture );
$ response = $ this -> client -> request ( ' POST ' , ' https://test.test/foo?foo=bar ' , [
' json ' => [
' foo ' => ' bar ' ,
' baz ' => 123
],
' headers ' => [
' x-header ' => ' x-value ' ,
]
]);
self :: assertEquals ( 200 , $ response -> getStatusCode ());
self :: assertEquals ( ' ok ' , $ response -> getContent ());
}
}
class MyTest extends AbstractFunctionalTest
{
/**
* Make fixture using builder with callbacks in request and response
* Request using json
* Check response
*/
public function testCallbackExample (): void
{
$ builder = $ this -> client -> getHttpFixtureBuilder ();
$ httpFixture = $ builder
-> request (
$ builder -> method ( $ builder -> callback ( function ( string $ method ): bool {
return $ method === ' POST ' ;
})),
$ builder -> url ( $ builder -> callback ( function ( string $ url ): bool {
return $ url === ' https://test.test/foo ' ;
})),
$ builder -> query (
$ builder -> callback ( function ( string $ query ): bool {
return $ query === ' foo=bar ' ;
}),
$ builder -> queryToArray (
$ builder -> callback ( function ( array $ arrayQuery ): bool {
return array_key_exists ( ' foo ' , $ arrayQuery );
})
)
),
$ builder -> body ( $ builder -> callback ( function ( string $ jsonBody ): bool {
$ arrayBody = json_decode ( $ jsonBody , true );
return isset ( $ arrayBody [ ' foo ' ]);
})),
$ builder -> headers ( $ builder -> callback ( function ( array $ headers ): bool {
return array_key_exists ( ' x-header ' , $ headers );
}))
)
-> response (
function ( string $ method , string $ url , string $ query , string $ body , array $ headers ): MockResponse {
$ stringHeaders = [];
foreach ( $ headers as $ key => $ value ) {
$ stringHeaders [] = " $ key : $ value " ;
}
return new MockResponse ( json_encode ([
' method ' => $ method ,
' url ' => $ url ,
' query ' => $ query ,
' body ' => $ body ,
' headers ' => $ headers ,
]));
}
)
-> build ();
$ this -> client -> addFixture ( $ httpFixture );
$ response = $ this -> client -> request ( ' POST ' , ' https://test.test/foo?foo=bar ' , [
' json ' => [
' foo ' => ' bar ' ,
' baz ' => 123
],
' headers ' => [
' x-header ' => ' x-value ' ,
]
]);
self :: assertEquals ( 200 , $ response -> getStatusCode ());
$ responseArray = json_decode ( $ response -> getContent (), true );
self :: assertEquals ( ' POST ' , $ responseArray [ ' method ' ]);
self :: assertEquals ( ' https://test.test/foo ' , $ responseArray [ ' url ' ]);
self :: assertEquals ( ' foo=bar ' , $ responseArray [ ' query ' ]);
self :: assertEquals ( ' {"foo":"bar","baz":123} ' , $ responseArray [ ' body ' ]);
self :: assertArrayHasKey ( ' x-header ' , $ responseArray [ ' headers ' ]);
}
}
Buat kelas pembanding, itu harus mengimplementasikan ComparatorInterface
use ExtendedMockHttpClient HttpFixture Request Comparator ComparatorInterface ;
class CustomComparator implements ComparatorInterface
{
/**
* @var string
*/
private $ stringPart1 ;
/**
* @var string
*/
private $ stringPart2 ;
public static function getName (): string
{
return ' custom ' ;
}
public function __construct ( string $ stringPart1 , string $ stringPart2 )
{
$ this -> stringPart1 = $ stringPart1 ;
$ this -> stringPart2 = $ stringPart2 ;
}
public function __invoke ( $ value ): bool
{
return $ value === " $ this -> stringPart1 . $ this -> stringPart2 " ;
}
}
Timpa HttpFixtureFactory
untuk menambahkan tempat Anda dapat menggunakan pembanding baru
services :
ExtendedMockHttpClientFactoryHttpFixtureFactory :
arguments :
- ' %allowed_nested_keys% '
calls :
- add : ['body', 'custom']
- add : ['method', 'custom']
- add : ['query', 'custom']
...
Gunakan pembanding baru dalam pengujian
class MyTest extends AbstractFunctionalTest
{
/**
* Make fixture using builder with custom comparator
* Request using string body
* Check response
*/
public function testCustomComparator (): void
{
$ builder = $ this -> client -> getHttpFixtureBuilder ();
$ httpFixture = $ builder
-> request (
$ builder -> body ( new CustomComparator ( ' foo ' , ' bar ' ))
)
-> response ( 200 , ' ok ' )
-> build ();
$ this -> client -> addFixture ( $ httpFixture );
$ response = $ this -> client -> request ( ' GET ' , ' https://test.test ' , [
' body ' => ' foo.bar '
]);
self :: assertEquals ( 200 , $ response -> getStatusCode ());
self :: assertEquals ( ' ok ' , $ response -> getContent ());
}
}
Buat kelas pembuat khusus yang berdasarkan pembuat asli
use ExtendedMockHttpClient Builder HttpFixtureBuilder as BaseHttpFixtureBuilder ;
use ExtendedMockHttpClient Tests Fixture Application HttpFixture Request Comparator CustomComparator ;
class HttpFixtureBuilder extends BaseHttpFixtureBuilder
{
public function custom ( string $ stringPart1 , string $ stringPart2 ): CustomComparator
{
return new CustomComparator ( $ stringPart1 , $ stringPart2 );
}
}
Buat kelas pabrik pembuat khusus yang berdasarkan pada pabrik pembuat asli
use ExtendedMockHttpClient Factory HttpFixtureBuilderFactory as BaseHttpFixtureBuilderFactory ;
use ExtendedMockHttpClient Builder HttpFixtureBuilder as BaseHttpFixtureBuilder ;
use ExtendedMockHttpClient Tests Fixture Application Builder HttpFixtureBuilder ;
class HttpFixtureBuilderFactory extends BaseHttpFixtureBuilderFactory
{
public function create (): BaseHttpFixtureBuilder
{
return new HttpFixtureBuilder ( $ this -> httpFixtureFactory );
}
}
Timpa layanan pabrik pembuat
services :
ExtendedMockHttpClientFactoryHttpFixtureBuilderFactory :
class : ExtendedMockHttpClientTestsFixtureApplicationFactoryHttpFixtureBuilderFactory
Gunakan pembuat yang diperbarui dalam pengujian
class MyTest extends AbstractFunctionalTest
{
/**
* Make fixture using overwrote builder with custom comparator
* Request using string body
* Check response
*/
public function testBuilderOverwrote (): void
{
/** @var HttpFixtureBuilder $builder */
$ builder = $ this -> client -> getHttpFixtureBuilder ();
$ httpFixture = $ builder
-> request (
$ builder -> body ( $ builder -> custom ( ' foo ' , ' bar ' ))
)
-> response ( 200 , ' ok ' )
-> build ();
$ this -> client -> addFixture ( $ httpFixture );
$ response = $ this -> client -> request ( ' GET ' , ' https://test.test ' , [
' body ' => ' foo.bar '
]);
self :: assertEquals ( 200 , $ response -> getStatusCode ());
self :: assertEquals ( ' ok ' , $ response -> getContent ());
}
}