Skip to content

Commit

Permalink
Laravel version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-nuwber committed Feb 27, 2025
1 parent ec9a9cd commit 26bc6f6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 32 deletions.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Once again, the RabbitEvents library helps you publish an event and handle it in
1. [Listener component](#listener)
1. [Examples](./examples)
1. [Speeding up RabbitEvents](#speeding-up-rabbitevents)
1. [Testing](#testing)
1. [Non-standard use](#non-standard-use)
1. [License](#license)

Expand Down Expand Up @@ -123,6 +124,69 @@ composer require enqueue/amqp-ext
```
No additional configuration is required.

## Testing <a name="testing"></a>

We always write tests. Tests in our applications contain many mocks and fakes to test how events are published.

There is the `PublishableEventTesting` trait that provides assertion methods in an Event class that you want to test.

**Event.php**

```php
<?php

namespace App\BroadcastEvents;

use RabbitEvents\Publisher\ShouldPublish;
use RabbitEvents\Publisher\Support\Publishable;
use RabbitEvents\Publisher\Support\PublishableEventTesting;

class Event implements ShouldPublish
{
use Publishable;
use PublishableEventTesting;

public function __construct(private array $payload)
{
}

public function publishEventKey(): string
{
return 'something.happened';
}

public function toPublish(): array
{
return $this->payload;
}
}
```

**Test.php**

```php
<?php

use \App\RabbitEvents\Event;
use \App\RabbitEvents\AnotherEvent;

Event::fake();

$payload = [
'key1' => 'value1',
'key2' => 'value2',
];

Event::publish($payload);

Event::assertPublished('something.happened', $payload);

AnotherEvent::assertNotPublished();
```

If the assertion does not pass, `Mockery\Exception\InvalidCountException` will be thrown.\
Don't forget to call `\Mockery::close()` in `tearDown` or similar methods of your tests.

## Non-standard use <a name="#non-standard-use"></a>

If you're using only one part of RabbitEvents, you should know a few things:
Expand Down
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"require": {
"php": "^8.1",
"ext-json": "*",
"enqueue/amqp-lib": "^0.10.19",
"illuminate/support": "9 - 11",
"illuminate/events": "9 - 11",
"illuminate/console": "9 - 11",
"illuminate/container": "9 - 11"
"enqueue/amqp-lib": "^0.10",
"illuminate/support": "9 - 12",
"illuminate/events": "9 - 12",
"illuminate/console": "9 - 12",
"illuminate/container": "9 - 12"
},
"replace": {
"rabbitevents/foundation": "self.version",
Expand All @@ -27,8 +27,8 @@
"require-dev": {
"mockery/mockery": "^1.6.0",
"phpunit/phpunit": "^10.5|^11.0",
"vlucas/phpdotenv": "^v5.4.1",
"phpstan/phpstan": "^1.10"
"vlucas/phpdotenv": "^v5.6",
"phpstan/phpstan": "^2.1"
},
"autoload": {
"psr-4": {
Expand All @@ -43,6 +43,10 @@
"RabbitEvents\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit -c phpunit.xml.dist",
"analyse": "vendor/bin/phpstan analyse"
},
"extra": {
"laravel": {
"providers": [
Expand Down
6 changes: 3 additions & 3 deletions src/RabbitEvents/Publisher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ There is the `PublishableEventTesting` trait that provides assertion methods in

namespace App\BroadcastEvents;

use Nuwber\Events\Event\Publishable;
use Nuwber\Events\Event\ShouldPublish;
use Nuwber\Events\Event\Testing\PublishableEventTesting;
use RabbitEvents\Publisher\ShouldPublish;
use RabbitEvents\Publisher\Support\Publishable;
use RabbitEvents\Publisher\Support\PublishableEventTesting;

class Event implements ShouldPublish
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function fake(): void
Container::getInstance()->instance(Publisher::class, \Mockery::spy(Publisher::class));
}

public static function assertPublished(string $event, array $payload = null): void
public static function assertPublished(string $event, ?array $payload = null): void
{
Container::getInstance()->get(Publisher::class)
->shouldHaveReceived()
Expand Down
43 changes: 22 additions & 21 deletions tests/Listener/Message/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ public function testRegularExceptionThenRelease()
$exceptionMessage = 'Failed handler exception';
$this->expectExceptionMessage($exceptionMessage);

$handler = new FakeHandler(
$this->message,
function () use ($exceptionMessage) {
throw new \Exception($exceptionMessage);
}
);
$handler = new FakeHandler();

$handler->message = $this->message;
$handler->callback = function () use ($exceptionMessage) {
throw new \Exception($exceptionMessage);
};

$processor = new Processor(new FakeHandlerFactory(), $this->events);

Expand All @@ -138,12 +138,11 @@ public function testDonNotReleaseIfLastAttempt()
{
$this->expectException(\RuntimeException::class);

$handler = new FakeHandler(
$this->message,
function () {
throw new \RuntimeException();
}
);
$handler = new FakeHandler();
$handler->message = $this->message;
$handler->callback = function () {
throw new \RuntimeException();
};

$handler->attempts = 3;

Expand Down Expand Up @@ -207,7 +206,11 @@ public function make(Message $message, callable $callback, string $listenerClass
return $this->handler;
}

$handler = new FakeHandler($message, $callback, $listenerClass);
$handler = new FakeHandler();
$handler->message = $message;
$handler->callback = $callback;
$handler->setListenerClass($listenerClass);

$this->handlers[] = $handler;

return $handler;
Expand All @@ -231,15 +234,7 @@ class FakeHandler extends Handler
public $transport;

public function __construct(
?Message $message = null,
callable $callback = null,
string $listener = null,
Transport $transport = null
) {
$this->message = $message;
$this->callback = $callback ?: fn() => true;
$this->listener = $listener;
$this->transport = $transport;
}

public function handle()
Expand All @@ -248,6 +243,12 @@ public function handle()
return call_user_func($this->callback, $this);
}

public function setListenerClass(string $listener)
{
$this->listenerClass = $listener;

return $this;
}
public function getName(): string
{
return 'FakeHandler';
Expand Down

0 comments on commit 26bc6f6

Please sign in to comment.