Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
datashaman committed Oct 25, 2024
1 parent 394f66a commit 2f8e402
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 116 deletions.
95 changes: 47 additions & 48 deletions src/ServiceBusEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Ringierimu\ServiceBusNotificationsChannel;

use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Ramsey\Uuid\Uuid;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Ringierimu\ServiceBusNotificationsChannel\Exceptions\InvalidConfigException;
use Throwable;

Expand All @@ -25,13 +25,13 @@
class ServiceBusEvent
{
public static $actionTypes = [
'user',
'admin',
'api',
'system',
'app',
'migration',
'other',
"user",
"admin",
"api",
"system",
"app",
"migration",
"other",
];

protected $eventType;
Expand All @@ -53,9 +53,9 @@ class ServiceBusEvent
public function __construct(string $eventType, array $config = [])
{
$this->eventType = $eventType;
$this->config = $config ?: config('services.service_bus');
$this->config = $config ?: config("services.service_bus");
$this->createdAt = Carbon::now();
$this->reference = $this->generateUUID();
$this->reference = (string) Str::uuid();
}

/**
Expand Down Expand Up @@ -127,7 +127,10 @@ public function withAction(string $type, string $reference): self
$this->actionType = $type;
$this->actionReference = $reference;
} else {
throw new InvalidConfigException('Action type must be on of the following: ' . print_r(self::$actionTypes, true));
throw new InvalidConfigException(
"Action type must be on of the following: " .
print_r(self::$actionTypes, true)
);
}

return $this;
Expand Down Expand Up @@ -175,13 +178,21 @@ public function withResources(string $resourceName, array $resource)
*
* @return this
*/
public function withResource(string $resourceName, $resource, Request $request = null): self
{
public function withResource(
string $resourceName,
$resource,
Request $request = null
): self {
if (!is_array($resource)) {
if ($resource instanceof JsonResource) {
$resource = $resource->toArray($request);
} else {
throw new Exception('Unhandled resource type: ' . $resourceName . ' ' . json_encode($resource));
throw new Exception(
"Unhandled resource type: " .
$resourceName .
" " .
json_encode($resource)
);
}
}

Expand Down Expand Up @@ -227,7 +238,7 @@ public function createdAt(Carbon $createdAtDate): self
*/
protected function getCulture(): string
{
return $this->culture ?? $this->config['culture'];
return $this->culture ?? $this->config["culture"];
}

/**
Expand All @@ -240,18 +251,6 @@ protected function getPayload(): array
return $this->payload;
}

/**
* Generates a v4 UUID.
*
* @throws Throwable
*
* @return string
*/
private function generateUUID(): string
{
return Uuid::uuid4()->toString();
}

/**
* Return the event as an array that can be sent to the service.
*
Expand All @@ -261,33 +260,33 @@ private function generateUUID(): string
*/
public function getParams(): array
{
$version = intval($this->config['version']);
$version = intval($this->config["version"]);

if ($version < 2) {
return [
'events' => [$this->eventType],
'venture_reference' => $this->reference,
'reference' => $this->reference,
'venture_config_id' => $this->config['venture_config_id'],
'from' => $this->config['venture_config_id'],
'created_at' => $this->createdAt->toISOString(),
'culture' => $this->getCulture(),
'action_type' => $this->actionType,
'action_reference' => $this->actionReference,
'version' => $this->config['version'],
'route' => $this->route,
'payload' => $this->getPayload(),
"events" => [$this->eventType],
"venture_reference" => $this->reference,
"reference" => $this->reference,
"venture_config_id" => $this->config["venture_config_id"],
"from" => $this->config["venture_config_id"],
"created_at" => $this->createdAt->toISOString(),
"culture" => $this->getCulture(),
"action_type" => $this->actionType,
"action_reference" => $this->actionReference,
"version" => $this->config["version"],
"route" => $this->route,
"payload" => $this->getPayload(),
];
}

return [
'events' => [$this->eventType],
'reference' => $this->reference,
'from' => $this->config['from'] ?? $this->config['node_id'],
'created_at' => $this->createdAt->toISOString(),
'version' => $this->config['version'],
'route' => $this->route,
'payload' => $this->getPayload(),
"events" => [$this->eventType],
"reference" => $this->reference,
"from" => $this->config["from"] ?? $this->config["node_id"],
"created_at" => $this->createdAt->toISOString(),
"version" => $this->config["version"],
"route" => $this->route,
"payload" => $this->getPayload(),
];
}

Expand Down
2 changes: 0 additions & 2 deletions tests/ServiceBusChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use PHPUnit\Framework\TestCase;
use Ringierimu\ServiceBusNotificationsChannel\Exceptions\CouldNotSendNotification;
use Ringierimu\ServiceBusNotificationsChannel\ServiceBusChannel;
use Throwable;
Expand Down
119 changes: 59 additions & 60 deletions tests/ServiceBusEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Ringierimu\ServiceBusNotificationsChannel\Tests;

use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Carbon;
use Ringierimu\ServiceBusNotificationsChannel\Exceptions\InvalidConfigException;
use Ringierimu\ServiceBusNotificationsChannel\ServiceBusEvent;
use Throwable;
Expand All @@ -15,16 +14,16 @@ class ServiceBusEventTest extends TestCase
{
public function testShouldCreateServiceBusEventInstance()
{
$serviceBus = new ServiceBusEvent('test', config_v2());
$serviceBus = new ServiceBusEvent("test", config_v2());

$this->assertEquals('test', $serviceBus->getEventType());
$this->assertEquals("test", $serviceBus->getEventType());
}

public function testShouldCreateServiceBusEventInstanceViaStaticCall()
{
$serviceBus = ServiceBusEvent::create('test', config_v2());
$serviceBus = ServiceBusEvent::create("test", config_v2());

$this->assertEquals('test', $serviceBus->getEventType());
$this->assertEquals("test", $serviceBus->getEventType());
}

/**
Expand All @@ -34,13 +33,13 @@ public function testShouldThrowInvalidConfigException()
{
$this->expectException(InvalidConfigException::class);

ServiceBusEvent::create('test')
->withAction('test', uniqid())
->withCulture('en')
ServiceBusEvent::create("test")
->withAction("test", uniqid())
->withCulture("en")
->withReference(uniqid())
->withRoute('api')
->withRoute("api")
->createdAt(Carbon::now())
->withResources('resources', ['data']);
->withResources("resources", ["data"]);
}

/**
Expand All @@ -50,28 +49,28 @@ public function testShouldThrowInvalidConfigException()
public function testShouldAllocateAttributesToServiceBusObject()
{
$resource = [
'user' => 'John Doe',
'email' => 'john@doe.com',
'phone' => '0123456789',
"user" => "John Doe",
"email" => "john@doe.com",
"phone" => "0123456789",
];

$serviceBus = ServiceBusEvent::create('test', config_v2())
->withAction('other', uniqid())
->withCulture('en')
$serviceBus = ServiceBusEvent::create("test", config_v2())
->withAction("other", uniqid())
->withCulture("en")
->withReference(uniqid())
->withRoute('api')
->withRoute("api")
->createdAt(Carbon::now())
->withResources('resource', $resource);
->withResources("resource", $resource);

$serviceBusData = $serviceBus->getParams();

$this->assertNotEmpty($serviceBusData);
$this->assertArrayHasKey('events', $serviceBusData);
$this->assertArrayHasKey('payload', $serviceBusData);
$this->assertArrayHasKey('resource', $serviceBusData['payload']);
$this->assertContains('test', $serviceBusData['events']);
$this->assertArrayHasKey("events", $serviceBusData);
$this->assertArrayHasKey("payload", $serviceBusData);
$this->assertArrayHasKey("resource", $serviceBusData["payload"]);
$this->assertContains("test", $serviceBusData["events"]);

$this->assertEquals($resource, $serviceBusData['payload']['resource']);
$this->assertEquals($resource, $serviceBusData["payload"]["resource"]);
}

/**
Expand All @@ -81,29 +80,29 @@ public function testShouldAllocateAttributesToServiceBusObject()
public function testShouldAllocateAttributesToServiceBusObjectWithPayload()
{
$payload = [
'object' => [
'user' => 'John Doe',
'email' => 'john@doe.com',
'phone' => '0123456789',
"object" => [
"user" => "John Doe",
"email" => "john@doe.com",
"phone" => "0123456789",
],
];

$serviceBus = ServiceBusEvent::create('test', config_v2())
->withAction('other', uniqid())
->withCulture('en')
$serviceBus = ServiceBusEvent::create("test", config_v2())
->withAction("other", uniqid())
->withCulture("en")
->withReference(uniqid())
->withRoute('api')
->withRoute("api")
->createdAt(Carbon::now())
->withPayload($payload);

$serviceBusData = $serviceBus->getParams();

$this->assertNotEmpty($serviceBusData);
$this->assertArrayHasKey('events', $serviceBusData);
$this->assertArrayHasKey('payload', $serviceBusData);
$this->assertContains('test', $serviceBusData['events']);
$this->assertArrayHasKey("events", $serviceBusData);
$this->assertArrayHasKey("payload", $serviceBusData);
$this->assertContains("test", $serviceBusData["events"]);

$this->assertEquals($payload, $serviceBusData['payload']);
$this->assertEquals($payload, $serviceBusData["payload"]);
}

/**
Expand All @@ -112,21 +111,21 @@ public function testShouldAllocateAttributesToServiceBusObjectWithPayload()
*/
public function testShouldReturnCorrectEventForSpecificVersion()
{
$serviceBusVersion1 = ServiceBusEvent::create('test', config_v1())
->withAction('other', uniqid())
->withCulture('en')
$serviceBusVersion1 = ServiceBusEvent::create("test", config_v1())
->withAction("other", uniqid())
->withCulture("en")
->withReference(uniqid())
->withRoute('api')
->withRoute("api")
->withPayload([
'listing' => [],
"listing" => [],
])
->createdAt(Carbon::now());

$serviceBusVersion2 = ServiceBusEvent::create('test', config_v2())
$serviceBusVersion2 = ServiceBusEvent::create("test", config_v2())
->withReference(uniqid())
->withRoute('api')
->withRoute("api")
->withPayload([
'listing' => [],
"listing" => [],
])
->createdAt(Carbon::now());

Expand All @@ -136,25 +135,25 @@ public function testShouldReturnCorrectEventForSpecificVersion()
$this->assertNotEmpty($serviceBusDataVersion1);
$this->assertNotEmpty($serviceBusDataVersion2);

$this->assertArrayHasKey('events', $serviceBusDataVersion1);
$this->assertArrayHasKey('payload', $serviceBusDataVersion1);
$this->assertArrayHasKey('from', $serviceBusDataVersion1);
$this->assertArrayHasKey('venture_config_id', $serviceBusDataVersion1);
$this->assertArrayHasKey('venture_reference', $serviceBusDataVersion1);
$this->assertArrayHasKey('reference', $serviceBusDataVersion1);
$this->assertArrayHasKey("events", $serviceBusDataVersion1);
$this->assertArrayHasKey("payload", $serviceBusDataVersion1);
$this->assertArrayHasKey("from", $serviceBusDataVersion1);
$this->assertArrayHasKey("venture_config_id", $serviceBusDataVersion1);
$this->assertArrayHasKey("venture_reference", $serviceBusDataVersion1);
$this->assertArrayHasKey("reference", $serviceBusDataVersion1);

$this->assertArrayHasKey('from', $serviceBusDataVersion2);
$this->assertArrayHasKey('events', $serviceBusDataVersion2);
$this->assertArrayHasKey('payload', $serviceBusDataVersion2);
$this->assertArrayHasKey('reference', $serviceBusDataVersion2);
$this->assertArrayHasKey("from", $serviceBusDataVersion2);
$this->assertArrayHasKey("events", $serviceBusDataVersion2);
$this->assertArrayHasKey("payload", $serviceBusDataVersion2);
$this->assertArrayHasKey("reference", $serviceBusDataVersion2);

$this->assertNotEmpty($serviceBusDataVersion1['venture_config_id']);
$this->assertNotEmpty($serviceBusDataVersion1['venture_reference']);
$this->assertNotEmpty($serviceBusDataVersion1["venture_config_id"]);
$this->assertNotEmpty($serviceBusDataVersion1["venture_reference"]);

$this->assertNotEmpty($serviceBusDataVersion2['from']);
$this->assertNotEmpty($serviceBusDataVersion2['reference']);
$this->assertNotEmpty($serviceBusDataVersion2["from"]);
$this->assertNotEmpty($serviceBusDataVersion2["reference"]);

$this->assertContains('test', $serviceBusDataVersion1['events']);
$this->assertContains('test', $serviceBusDataVersion2['events']);
$this->assertContains("test", $serviceBusDataVersion1["events"]);
$this->assertContains("test", $serviceBusDataVersion2["events"]);
}
}
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ringierimu\ServiceBusNotificationsChannel\Tests;

class TestCase extends \Orchestra\Testbench\TestCase
{
}
Loading

0 comments on commit 2f8e402

Please sign in to comment.