From 2f8e402c4fb71844354fa407170867f02fbfa839 Mon Sep 17 00:00:00 2001 From: Marlin Forbes Date: Fri, 25 Oct 2024 12:24:56 +0200 Subject: [PATCH] wip --- src/ServiceBusEvent.php | 95 +++++++++++++------------ tests/ServiceBusChannelTest.php | 2 - tests/ServiceBusEventTest.php | 119 ++++++++++++++++---------------- tests/TestCase.php | 7 ++ tests/TestNotification.php | 12 ++-- 5 files changed, 119 insertions(+), 116 deletions(-) create mode 100644 tests/TestCase.php diff --git a/src/ServiceBusEvent.php b/src/ServiceBusEvent.php index 80b8e3a..aa7d902 100644 --- a/src/ServiceBusEvent.php +++ b/src/ServiceBusEvent.php @@ -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; @@ -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; @@ -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(); } /** @@ -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; @@ -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) + ); } } @@ -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"]; } /** @@ -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. * @@ -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(), ]; } diff --git a/tests/ServiceBusChannelTest.php b/tests/ServiceBusChannelTest.php index 5a0368d..5c68db2 100644 --- a/tests/ServiceBusChannelTest.php +++ b/tests/ServiceBusChannelTest.php @@ -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; diff --git a/tests/ServiceBusEventTest.php b/tests/ServiceBusEventTest.php index 702715c..6dffedf 100644 --- a/tests/ServiceBusEventTest.php +++ b/tests/ServiceBusEventTest.php @@ -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; @@ -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()); } /** @@ -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"]); } /** @@ -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"]); } /** @@ -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"]); } /** @@ -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()); @@ -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"]); } } diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..bd50e7a --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,7 @@ +withAction('other', uniqid()) - ->withCulture('en') + return ServiceBusEvent::create("test") + ->withAction("other", uniqid()) + ->withCulture("en") ->withReference(uniqid()) - ->withRoute('api') + ->withRoute("api") ->createdAt(Carbon::now()) - ->withResources('resources', ['data']); + ->withResources("resources", ["data"]); } }