Skip to content

Commit

Permalink
Merge pull request #12 from Ricorocks-Digital-Agency/ray-support
Browse files Browse the repository at this point in the history
Adds support for Spatie's Ray library
  • Loading branch information
lukeraymonddowning authored Feb 3, 2021
2 parents 66c25cb + f8363be commit 9e45c20
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A Laravel SOAP client that provides a clean interface for handling requests and
- [Faking](#faking)
- [Configuration](#configuration)
* [Include](#include)

- [Ray Support](#ray-support)


## Installation
Expand Down Expand Up @@ -278,6 +278,20 @@ You can even use dot syntax on your array keys to permeate deeper into the reque
Soap::include(['login.credentials' => soap_node(['user' => '...', 'password' => '...'])])->for('...');
```

## Ray Support

This package comes with first party support for Ray, an awesome debugging tool by Spatie! We offer a couple of methods that you
can use to start debugging immediately.

> Obviously, you'll need Ray installed in your project for this to work.
### `ray()->showSoapRequests()`
This enables Ray support in the SOAP package. Any SOAP requests made will be recorded in the Ray
app for you to inspect.

### `ray()->stopShowingSoapRequests()`
This disables Ray support in the SOAP package. Requests will stop being recorded if previously
enabled.

### Changelog

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.4",
"orchestra/testbench": "^6.5"
"orchestra/testbench": "^6.5",
"spatie/ray": "^1.17"
},
"license": "MIT",
"authors": [
Expand Down
13 changes: 13 additions & 0 deletions src/Providers/SoapServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\ServiceProvider;
use RicorocksDigitalAgency\Soap\Parameters\Builder;
use RicorocksDigitalAgency\Soap\Parameters\IntelligentBuilder;
use RicorocksDigitalAgency\Soap\Ray\SoapWatcher;
use RicorocksDigitalAgency\Soap\Request\Request;
use RicorocksDigitalAgency\Soap\Request\SoapClientRequest;
use RicorocksDigitalAgency\Soap\Soap;
Expand All @@ -16,10 +17,22 @@ public function register()
$this->app->singleton('soap', fn() => app(Soap::class));
$this->app->bind(Request::class, SoapClientRequest::class);
$this->app->bind(Builder::class, IntelligentBuilder::class);

$this->registerRay();
}

public function boot()
{
require_once __DIR__ . '/../helpers.php';
}

protected function registerRay()
{
if (!SoapWatcher::rayExists()) {
return;
}

$this->app->singleton(SoapWatcher::class);
app(SoapWatcher::class)->register();
}
}
48 changes: 48 additions & 0 deletions src/Ray/SoapWatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php


namespace RicorocksDigitalAgency\Soap\Ray;

use RicorocksDigitalAgency\Soap\Facades\Soap;
use RicorocksDigitalAgency\Soap\Request\Request;
use RicorocksDigitalAgency\Soap\Response\Response;
use Spatie\LaravelRay\Ray;
use Spatie\LaravelRay\Watchers\Watcher;
use Spatie\Ray\Ray as SpatieRay;

class SoapWatcher extends Watcher
{
public static function rayExists(): bool
{
return class_exists("Spatie\\Ray\\Ray");
}

public function register(): void
{
SpatieRay::macro('showSoapRequests', fn() => app(SoapWatcher::class)->enable());
SpatieRay::macro('stopShowingSoapRequests', fn() => app(SoapWatcher::class)->disable());

Soap::afterRequesting(
function ($request, $response) {
if (!app(SoapWatcher::class)->enabled()) {
return;
}

$this->handleRequest($request, $response);
}
);
}

protected function handleRequest(Request $request, Response $response)
{
app(Ray::class)->table(
[
'Endpoint' => $request->getEndpoint(),
'Method' => $request->getMethod(),
'Request' => $request->getBody(),
'Response' => $response->response,
],
"SOAP"
);
}
}
1 change: 1 addition & 0 deletions tests/SoapClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RicorocksDigitalAgency\Soap\Facades\Soap;
use RicorocksDigitalAgency\Soap\Response\Response;
use Spatie\Ray\Ray;

class SoapClassTest extends TestCase
{
Expand Down
5 changes: 3 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use RicorocksDigitalAgency\Soap\Providers\SoapServiceProvider;
use RicorocksDigitalAgency\Soap\Tests\Mocks\MockSoapClient;
use Spatie\LaravelRay\RayServiceProvider;

abstract class TestCase extends OrchestraTestCase
{
Expand All @@ -17,7 +18,7 @@ protected function fakeClient()

protected function getPackageProviders($app)
{
return [SoapServiceProvider::class];
return [RayServiceProvider::class, SoapServiceProvider::class];
}

}
}

0 comments on commit 9e45c20

Please sign in to comment.