Created
September 16, 2010 21:29
-
-
Save ftassi/583208 to your computer and use it in GitHub Desktop.
Sorgenti del post http://www.ftassi.com/2010/07/integrare-web-service-soap-in-applicazioni-php-un-esempio-pratico.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class OrteStartWith { | |
public $prefix; // string | |
} | |
class OrteStartWithResponse { | |
public $OrteStartWithResult; // string | |
} | |
class GetPrefixedEntries { | |
public $prefix; // string | |
} | |
class GetPrefixedEntriesResponse { | |
public $GetPrefixedEntriesResult; // string | |
} | |
/** | |
* OrteLookup class | |
* | |
* A WebService retreiving the names of German cities starting with a specific string. | |
* | |
* @author {author} | |
* @copyright {copyright} | |
* @package {package} | |
*/ | |
class OrteLookupClient extends SoapClient { | |
private static $classmap = array( | |
'OrteStartWith' => 'OrteStartWith', | |
'OrteStartWithResponse' => 'OrteStartWithResponse', | |
'GetPrefixedEntries' => 'GetPrefixedEntries', | |
'GetPrefixedEntriesResponse' => 'GetPrefixedEntriesResponse', | |
); | |
public function OrteLookupClient($wsdl = "http://mathertel.de/AJAXEngine/S02_AJAXCoreSamples/OrteLookup.asmx?WSDL", $options = array()) { | |
foreach(self::$classmap as $key => $value) { | |
if(!isset($options['classmap'][$key])) { | |
$options['classmap'][$key] = $value; | |
} | |
} | |
parent::__construct($wsdl, $options); | |
} | |
/** | |
* | |
* | |
* @param OrteStartWith $parameters | |
* @return OrteStartWithResponse | |
*/ | |
public function OrteStartWith(OrteStartWith $parameters) { | |
return $this->__soapCall('OrteStartWith', array($parameters), array( | |
'uri' => 'http://www.mathertel.de/OrteLookup/', | |
'soapaction' => '' | |
) | |
); | |
} | |
/** | |
* Return Lookup entries that start with the given prefix. | |
* | |
* @param GetPrefixedEntries $parameters | |
* @return GetPrefixedEntriesResponse | |
*/ | |
public function GetPrefixedEntries(GetPrefixedEntries $parameters) { | |
return $this->__soapCall('GetPrefixedEntries', array($parameters), array( | |
'uri' => 'http://www.mathertel.de/OrteLookup/', | |
'soapaction' => '' | |
) | |
); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* | |
* @param OrteStartWith $parameters | |
* @return OrteStartWithResponse | |
*/ | |
public function OrteStartWith(OrteStartWith $parameters) { | |
return $this->__soapCall('OrteStartWith', array($parameters), array( | |
'uri' => 'http://www.mathertel.de/OrteLookup/', | |
'soapaction' => '' | |
) | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrteLookupService | |
{ | |
/** | |
* SoapClient | |
* | |
* @var OrteLookupClient | |
*/ | |
protected $client; | |
/** | |
* Costruttore | |
* | |
* @param OrteLookupClient $client | |
*/ | |
public function __construct(OrteLookupClient $client) | |
{ | |
$this->client = $client; | |
} | |
/** | |
* startWith | |
* | |
* Ritorna un array di città tedesce che iniziano | |
* con $string | |
* | |
* @param array $start | |
*/ | |
public function startWith($prefix) | |
{ | |
$request = new OrteStartWith(); | |
$request->prefix = $prefix; | |
$response = $this->client->OrteStartWith($request); | |
$response = explode(';', $response->OrteStartWithResult); | |
return $response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrteLookupServiceTest extends PHPUnit_Framework_TestCase | |
{ | |
public function setUp() | |
{ | |
$this->wsdl = 'http://mathertel.de/AJAXEngine/S02_AJAXCoreSamples/OrteLookup.asmx?WSDL'; | |
} | |
public function testStartWith() | |
{ | |
$start = 'foo'; | |
$cities = array('fooCity1', 'fooCity2'); | |
$client = $this->getMockSoapClient($start, $cities); | |
$service = new OrteLookupService($client); | |
$this->assertType('array', $service->startWith($start)); | |
$this->assertEquals($cities, $service->startWith($start)); | |
} | |
/** | |
* Stub per OrteLookupClient | |
* | |
* Configura un oggetto OrteLookupClient che simuli una chiamata | |
* al metodo startWith. | |
* | |
* | |
* @param string $input | |
* @param array $result | |
*/ | |
protected function getMockSoapClient($input, $result) | |
{ | |
$orteStartWith = new OrteStartWith(); | |
$orteStartWith->prefix = $input; | |
$orteStartWithResponse = new OrteStartWithResponse(); | |
$orteStartWithResponse->OrteStartWithResult = implode(';', $result); | |
$client = $this->getMock('OrteLookupClient'); | |
$client->expects($this->any()) | |
->method('OrteStartWith') | |
->with($this->equalTo($orteStartWith)) | |
->will($this->returnValue($orteStartWithResponse)); | |
return $client; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment