Skip to content

Commit 5289783

Browse files
authored
feat(108): Allow client class configuration (#108)
* Allow soap client model configuration This way, users can overwrite the client model used by the factory, like so (in a service provider): ```php SoapFactory::useClientModel(CustomSoapClient::class); ``` * Add test for client configuration * Update client class property name & add documentation
1 parent 40cb201 commit 5289783

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

docs/client/configuration.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,27 @@ Headers may be added to requests using the `withHeaders` method. This `withHeade
3030
'X-First' => 'foo',
3131
'X-Second' => 'bar'
3232
])->baseWsdl('http://test.com'/v1?wsdl)->call('Get_Users');
33+
34+
## Custom Client Class
35+
36+
You are free to extend the SOAP client class used internally by this
37+
package, by defining your own class and extending the package client:
38+
39+
use CodeDredd\Soap\SoapClient as BaseClient;
40+
41+
class SoapClient extends BaseClient
42+
{
43+
// ...
44+
}
45+
46+
After defining your class, you may instruct the factory to use your
47+
custom class. Typically, this will happen in the `boot` method of
48+
your application's `App\Providers\AppServiceProvider` class:
49+
50+
use App\Soap\SoapClient;
51+
use CodeDredd\Soap\SoapFactory;
52+
53+
public function boot()
54+
{
55+
SoapFactory::useClientClass(SoapClient::class);
56+
}

src/SoapFactory.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class SoapFactory
1616
__call as macroCall;
1717
}
1818

19+
/**
20+
* The client class name.
21+
*
22+
* @var string
23+
*/
24+
public static $clientClass = 'CodeDredd\Soap\SoapClient';
25+
1926
/**
2027
* The stub callables that will handle requests.
2128
*
@@ -74,7 +81,7 @@ public function __call($method, $parameters)
7481
return (new SoapTesting($this))->{$method}(...$parameters);
7582
}
7683

77-
return tap(new SoapClient($this), function ($request) {
84+
return tap($this->client(), function ($request) {
7885
$request->stub($this->stubCallbacks);
7986
})->{$method}(...$parameters);
8087
}
@@ -252,4 +259,24 @@ public function recorded($callback)
252259
return $callback($pair[0], $pair[1]);
253260
});
254261
}
262+
263+
/**
264+
* Get a new client class instance.
265+
*
266+
* @return SoapClient
267+
*/
268+
public function client()
269+
{
270+
return new static::$clientClass($this);
271+
}
272+
273+
/**
274+
* Set the client class name.
275+
*
276+
* @param string $clientClass
277+
*/
278+
public static function useClientClass(string $clientClass): void
279+
{
280+
static::$clientClass = $clientClass;
281+
}
255282
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodeDredd\Soap\Tests\Fixtures;
4+
5+
use CodeDredd\Soap\SoapClient;
6+
7+
class CustomSoapClient extends SoapClient
8+
{
9+
public function buildClient(string $setup = '')
10+
{
11+
$this->baseWsdl(__DIR__.'/Wsdl/weather.wsdl');
12+
$this->withHandlerOptions([
13+
'handler' => $this->buildHandlerStack(),
14+
]);
15+
$this->refreshEngine();
16+
$this->isClientBuilded = true;
17+
18+
return $this;
19+
}
20+
}

tests/Unit/SoapClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use CodeDredd\Soap\Client\Request;
66
use CodeDredd\Soap\Client\Response;
77
use CodeDredd\Soap\Facades\Soap;
8+
use CodeDredd\Soap\SoapClient;
9+
use CodeDredd\Soap\SoapFactory;
10+
use CodeDredd\Soap\Tests\Fixtures\CustomSoapClient;
811
use CodeDredd\Soap\Tests\TestCase;
912

1013
class SoapClientTest extends TestCase
@@ -122,4 +125,14 @@ public function soapActionProvider()
122125
'with_fake_string' => ['Get_Post', $fakeResponse, ['response' => 'Test']],
123126
];
124127
}
128+
129+
public function testSoapClientClassMayBeCustomized(): void
130+
{
131+
Soap::fake();
132+
$client = Soap::buildClient('laravel_soap');
133+
$this->assertInstanceOf(SoapClient::class, $client);
134+
SoapFactory::useClientClass(CustomSoapClient::class);
135+
$client = Soap::buildClient('laravel_soap');
136+
$this->assertInstanceOf(CustomSoapClient::class, $client);
137+
}
125138
}

0 commit comments

Comments
 (0)