| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 | <?phpnamespace GuzzleHttp\Tests\Command\Guzzle;use GuzzleHttp\Client as HttpClient;use GuzzleHttp\Command\CommandInterface;use GuzzleHttp\Command\Guzzle\Description;use GuzzleHttp\Command\Guzzle\DescriptionInterface;use GuzzleHttp\Command\Guzzle\GuzzleClient;use GuzzleHttp\Command\Guzzle\Operation;use GuzzleHttp\Command\ServiceClientInterface;use GuzzleHttp\Handler\MockHandler;use GuzzleHttp\HandlerStack;use GuzzleHttp\Psr7\Response;use GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException;use GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\OtherCustomCommandException;use Predis\Response\ResponseInterface;/** * @covers \GuzzleHttp\Command\Guzzle\Deserializer */class DeserializerTest extends \PHPUnit_Framework_TestCase{    /** @var ServiceClientInterface|\PHPUnit_Framework_MockObject_MockObject */    private $serviceClient;    /** @var CommandInterface|\PHPUnit_Framework_MockObject_MockObject */    private $command;    public function setUp()    {        $this->serviceClient = $this->getMockBuilder(GuzzleClient::class)                            ->disableOriginalConstructor()                            ->getMock();        $this->command = $this->getMockBuilder(CommandInterface::class)->getMock();    }    protected function prepareErrorResponses($commandName, array $errors = [])    {        $this->command->expects($this->once())->method('getName')->will($this->returnValue($commandName));        $description = $this->getMockBuilder(DescriptionInterface::class)->getMock();        $operation = new Operation(['errorResponses' => $errors], $description);        $description->expects($this->once())            ->method('getOperation')            ->with($commandName)            ->will($this->returnValue($operation));        $this->serviceClient->expects($this->once())            ->method('getDescription')            ->will($this->returnValue($description));    }    public function testDoNothingIfNoException()    {        $mock = new MockHandler([new Response(200)]);        $description = new Description([            'operations' => [                'foo' => [                    'uri' => 'http://httpbin.org/{foo}',                    'httpMethod' => 'GET',                    'responseModel' => 'j',                    'parameters' => [                        'bar' => [                            'type'     => 'string',                            'required' => true,                            'location' => 'uri'                        ]                    ]                ]            ],            'models' => [                'j' => [                    'type' => 'object'                ]            ]        ]);        $httpClient = new HttpClient(['handler' => $mock]);        $client = new GuzzleClient($httpClient, $description);        $client->foo(['bar' => 'baz']);    }    /**     * @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException     */    public function testCreateExceptionWithCode()    {        $response = new Response(404);        $mock = new MockHandler([$response]);        $description = new Description([            'name' => 'Test API',            'baseUri' => 'http://httpbin.org',            'operations' => [                'foo' => [                    'uri' => '/{foo}',                    'httpMethod' => 'GET',                    'responseClass' => 'Foo',                    'parameters' => [                        'bar' => [                            'type'     => 'string',                            'required' => true,                            'description' => 'Unique user name (alphanumeric)',                            'location' => 'json'                        ],                    ],                    'errorResponses' => [                        ['code' => 404, 'class' => CustomCommandException::class]                    ]                ]            ],            'models' => [                'Foo' => [                    'type' => 'object',                    'additionalProperties' => [                        'location' => 'json'                    ]                ]            ]        ]);        $httpClient = new HttpClient(['handler' => $mock]);        $client = new GuzzleClient($httpClient, $description);        $client->foo(['bar' => 'baz']);    }    public function testNotCreateExceptionIfDoesNotMatchCode()    {        $response = new Response(401);        $mock = new MockHandler([$response]);        $description = new Description([            'name' => 'Test API',            'baseUri' => 'http://httpbin.org',            'operations' => [                'foo' => [                    'uri' => '/{foo}',                    'httpMethod' => 'GET',                    'responseClass' => 'Foo',                    'parameters' => [                        'bar' => [                            'type'     => 'string',                            'required' => true,                            'description' => 'Unique user name (alphanumeric)',                            'location' => 'json'                        ],                    ],                    'errorResponses' => [                        ['code' => 404, 'class' => CustomCommandException::class]                    ]                ]            ],            'models' => [                'Foo' => [                    'type' => 'object',                    'additionalProperties' => [                        'location' => 'json'                    ]                ]            ]        ]);        $httpClient = new HttpClient(['handler' => $mock]);        $client = new GuzzleClient($httpClient, $description);        $client->foo(['bar' => 'baz']);    }    /**     * @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException     */    public function testCreateExceptionWithExactMatchOfReasonPhrase()    {        $response = new Response(404, [], null, '1.1', 'Bar');        $mock = new MockHandler([$response]);        $description = new Description([            'name' => 'Test API',            'baseUri' => 'http://httpbin.org',            'operations' => [                'foo' => [                    'uri' => '/{foo}',                    'httpMethod' => 'GET',                    'responseClass' => 'Foo',                    'parameters' => [                        'bar' => [                            'type'     => 'string',                            'required' => true,                            'description' => 'Unique user name (alphanumeric)',                            'location' => 'json'                        ],                    ],                    'errorResponses' => [                        ['code' => 404, 'phrase' => 'Bar', 'class' => CustomCommandException::class]                    ]                ]            ],            'models' => [                'Foo' => [                    'type' => 'object',                    'additionalProperties' => [                        'location' => 'json'                    ]                ]            ]        ]);        $httpClient = new HttpClient(['handler' => $mock]);        $client = new GuzzleClient($httpClient, $description);        $client->foo(['bar' => 'baz']);    }    /**     * @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\OtherCustomCommandException     */    public function testFavourMostPreciseMatch()    {        $response = new Response(404, [], null, '1.1', 'Bar');        $mock = new MockHandler([$response]);        $description = new Description([            'name' => 'Test API',            'baseUri' => 'http://httpbin.org',            'operations' => [                'foo' => [                    'uri' => '/{foo}',                    'httpMethod' => 'GET',                    'responseClass' => 'Foo',                    'parameters' => [                        'bar' => [                            'type'     => 'string',                            'required' => true,                            'description' => 'Unique user name (alphanumeric)',                            'location' => 'json'                        ],                    ],                    'errorResponses' => [                        ['code' => 404, 'class' => CustomCommandException::class],                        ['code' => 404, 'phrase' => 'Bar', 'class' => OtherCustomCommandException::class],                    ]                ]            ],            'models' => [                'Foo' => [                    'type' => 'object',                    'additionalProperties' => [                        'location' => 'json'                    ]                ]            ]        ]);        $httpClient = new HttpClient(['handler' => $mock]);        $client = new GuzzleClient($httpClient, $description);        $client->foo(['bar' => 'baz']);    }    /**     * @expectedException \GuzzleHttp\Command\Exception\CommandException     * @expectedExceptionMessage 404     */    public function testDoesNotAddResultWhenExceptionIsPresent()    {        $description = new Description([            'operations' => [                'foo' => [                    'uri' => 'http://httpbin.org/{foo}',                    'httpMethod' => 'GET',                    'responseModel' => 'j',                    'parameters' => [                        'bar' => [                            'type'     => 'string',                            'required' => true,                            'location' => 'uri'                        ]                    ]                ]            ],            'models' => [                'j' => [                    'type' => 'object'                ]            ]        ]);        $mock = new MockHandler([new Response(404)]);        $stack = HandlerStack::create($mock);        $httpClient = new HttpClient(['handler' => $stack]);        $client = new GuzzleClient($httpClient, $description);        $client->foo(['bar' => 'baz']);    }    public function testReturnsExpectedResult()    {        $loginResponse = new Response(            200,            [],            '{                "LoginResponse":{                    "result":{                        "type":4,                        "username":{                            "uid":38664492,                            "content":"skyfillers-api-test"                        },                        "token":"3FB1F21014D630481D35CBC30CBF4043"                    },                    "status":{                        "code":200,                        "content":"OK"                    }                }            }'        );        $mock = new MockHandler([$loginResponse]);        $description = new Description([            'name' => 'Test API',            'baseUri' => 'http://httpbin.org',            'operations' => [                'Login' => [                    'uri' => '/{foo}',                    'httpMethod' => 'POST',                    'responseClass' => 'LoginResponse',                    'parameters' => [                        'username' => [                            'type'     => 'string',                            'required' => true,                            'description' => 'Unique user name (alphanumeric)',                            'location' => 'json'                        ],                        'password' => [                            'type'     => 'string',                            'required' => true,                            'description' => 'User\'s password',                            'location' => 'json'                        ],                        'response' => [                            'type'     => 'string',                            'required' => false,                            'description' => 'Determines the response type: xml = result content will be xml formatted (default); plain = result content will be simple text, without structure; json  = result content will be json formatted',                            'location' => 'json'                        ],                        'token' => [                            'type'     => 'string',                            'required' => false,                            'description' => 'Provides the authentication token',                            'location' => 'json'                        ]                    ]                ]            ],            'models' => [                'LoginResponse' => [                    'type' => 'object',                    'additionalProperties' => [                        'location' => 'json'                    ]                ]            ]        ]);        $httpClient = new HttpClient(['handler' => $mock]);        $client = new GuzzleClient($httpClient, $description);        $result = $client->Login([            'username' => 'test',            'password' => 'test',            'response' => 'json',        ]);        $expected = [            'result' => [                'type' => 4,                'username' => [                    'uid' => 38664492,                    'content' => 'skyfillers-api-test'                ],                'token' => '3FB1F21014D630481D35CBC30CBF4043'            ],            'status' => [                'code' => 200,                'content' => 'OK'            ]        ];        $this->assertArraySubset($expected, $result['LoginResponse']);    }}
 |