Вход Регистрация
Файл: vkolhoze.com/vendor/guzzle/guzzle/tests/Guzzle/Tests/Service/Command/DefaultRequestSerializerTest.php
Строк: 118
<?php

namespace GuzzleTestsServiceCommand;

use 
GuzzleServiceCommandDefaultRequestSerializer;
use 
GuzzleHttpMessageEntityEnclosingRequest;
use 
GuzzleServiceClient;
use 
GuzzleServiceDescriptionServiceDescription;
use 
GuzzleServiceDescriptionOperation;
use 
GuzzleServiceDescriptionParameter;
use 
GuzzleServiceCommandLocationVisitorRequestHeaderVisitor;
use 
GuzzleServiceCommandLocationVisitorVisitorFlyweight;

/**
 * @covers GuzzleServiceCommandDefaultRequestSerializer
 */
class DefaultRequestSerializerTest extends GuzzleTestsGuzzleTestCase
{
    
/** @var EntityEnclosingRequest */
    
protected $request;

    
/** @var GuzzleServiceCommandAbstractCommand */
    
protected $command;

    
/** @var Client */
    
protected $client;

    
/** @var DefaultRequestSerializer */
    
protected $serializer;

    
/** @var Operation */
    
protected $operation;

    public function 
setUp()
    {
        
$this->serializer DefaultRequestSerializer::getInstance();
        
$this->client = new Client('http://foo.com/baz');
        
$this->operation = new Operation(array('httpMethod' => 'POST'));
        
$this->command $this->getMockBuilder('GuzzleServiceCommandAbstractCommand')
            ->
setConstructorArgs(array(array(), $this->operation))
            ->
getMockForAbstractClass();
        
$this->command->setClient($this->client);
    }

    public function 
testAllowsCustomVisitor()
    {
        
$this->serializer->addVisitor('custom', new HeaderVisitor());
        
$this->command['test'] = '123';
        
$this->operation->addParam(new Parameter(array('name' => 'test''location' => 'custom')));
        
$request $this->serializer->prepare($this->command);
        
$this->assertEquals('123', (string) $request->getHeader('test'));
    }

    public function 
testUsesRelativePath()
    {
        
$this->operation->setUri('bar');
        
$request $this->serializer->prepare($this->command);
        
$this->assertEquals('http://foo.com/baz/bar', (string) $request->getUrl());
    }

    public function 
testUsesRelativePathWithUriLocations()
    {
        
$this->command['test'] = '123';
        
$this->operation->setUri('bar/{test}');
        
$this->operation->addParam(new Parameter(array('name' => 'test''location' => 'uri')));
        
$request $this->serializer->prepare($this->command);
        
$this->assertEquals('http://foo.com/baz/bar/123', (string) $request->getUrl());
    }

    public function 
testAllowsCustomFactory()
    {
        
$f = new VisitorFlyweight();
        
$serializer = new DefaultRequestSerializer($f);
        
$this->assertSame($f$this->readAttribute($serializer'factory'));
    }

    public function 
testMixedParams()
    {
        
$this->operation->setUri('bar{?limit,fields}');
        
$this->operation->addParam(new Parameter(array(
            
'name' => 'limit',
            
'location' => 'uri',
            
'required' => false,
        )));
        
$this->operation->addParam(new Parameter(array(
            
'name' => 'fields',
            
'location' => 'uri',
            
'required' => true,
        )));

        
$this->command['fields'] = array('id''name');

        
$request $this->serializer->prepare($this->command);
        
$this->assertEquals('http://foo.com/baz/bar?fields='.urlencode('id,name'), (string) $request->getUrl());
    }

    public function 
testValidatesAdditionalParameters()
    {
        
$description ServiceDescription::factory(array(
            
'operations' => array(
                
'foo' => array(
                    
'httpMethod' => 'PUT',
                    
'parameters' => array(
                        
'bar' => array('location' => 'header')
                    ),
                    
'additionalParameters' => array(
                        
'location' => 'json'
                    
)
                )
            )
        ));

        
$client = new Client();
        
$client->setDescription($description);
        
$command $client->getCommand('foo');
        
$command['bar'] = 'test';
        
$command['hello'] = 'abc';
        
$request $command->prepare();
        
$this->assertEquals('test', (string) $request->getHeader('bar'));
        
$this->assertEquals('{"hello":"abc"}', (string) $request->getBody());
    }
}
Онлайн: 1
Реклама