Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ProducerMessageInterface::isAuthDeclare() to control whether to declare automatically. #5745

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# v3.0.22 - TBD

## Added

- [#5745](https://github.com/hyperf/hyperf/pull/5745) Added `Hyperf\Amqp\Message\ProducerMessageInterface::isAuthDeclare()` to control whether to declare automatically.

## Optimized

- [#5741](https://github.com/hyperf/hyperf/pull/5741) Added deprecated comments to `Hyperf\DB\MySQLConnection`.
Expand Down
5 changes: 3 additions & 2 deletions src/amqp/src/Listener/MainWorkerStartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Instantiator\Instantiator;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessageInterface;
use Hyperf\Amqp\Producer as AmqpProducer;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\AnnotationCollector;
Expand Down Expand Up @@ -54,15 +55,15 @@ public function process(object $event): void
// Declare exchange and routingKey
$producerMessages = AnnotationCollector::getClassesByAnnotation(Producer::class);
if ($producerMessages) {
$producer = $this->container->get(\Hyperf\Amqp\Producer::class);
$producer = $this->container->get(AmqpProducer::class);
$instantiator = $this->container->get(Instantiator::class);
/**
* @var string $producerMessageClass
* @var Producer $annotation
*/
foreach ($producerMessages as $producerMessageClass => $annotation) {
$instance = $instantiator->instantiate($producerMessageClass);
if (! $instance instanceof ProducerMessageInterface) {
if (! $instance instanceof ProducerMessageInterface || ! $instance->isAutoDeclare()) {
continue;
}
$annotation->exchange && $instance->setExchange($annotation->exchange);
Expand Down
5 changes: 5 additions & 0 deletions src/amqp/src/Message/ProducerMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function serialize(): string
$packer = ApplicationContext::getContainer()->get(Packer::class);
return $packer->pack($this->payload);
}

public function isAutoDeclare(): bool
{
return true;
}
}
2 changes: 2 additions & 0 deletions src/amqp/src/Message/ProducerMessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function setPayload($data);
public function payload(): string;

public function getProperties(): array;

public function isAutoDeclare(): bool;
}
77 changes: 77 additions & 0 deletions src/amqp/tests/Listener/MainWorkerStartListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\AnnotationCollector;
use HyperfTest\Amqp\Stub\DemoDisableAutoDeclareProducer;
use HyperfTest\Amqp\Stub\DemoProducer;
use Mockery;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -76,4 +77,80 @@ public function testProcessWithDisabled()

$this->assertTrue(true);
}

public function testProcessWithEnable()
{
$container = Mockery::mock(ContainerInterface::class);

$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturnUsing(function () {
$logger = Mockery::mock(StdoutLoggerInterface::class);
$logger->shouldReceive('debug')->andReturn(null);
$logger->shouldReceive('log')->andReturn(null);
return $logger;
});

$container->shouldReceive('has')->with(ConfigInterface::class)->andReturnTrue();
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturnUsing(function () {
return new Config([
'amqp' => [
'enable' => true,
],
]);
});

$container->shouldReceive('get')->with(Instantiator::class)->andReturn(new Instantiator());

$producer = Mockery::mock(\Hyperf\Amqp\Producer::class);
$producer->shouldReceive('declare')->withAnyArgs()->once();

$container->shouldReceive('get')->with(\Hyperf\Amqp\Producer::class)->andReturn($producer);

AnnotationCollector::collectClass(DemoProducer::class, Producer::class, new Producer(
exchange: uniqid(),
routingKey: uniqid(),
));

$listener = new MainWorkerStartListener($container, $container->get(StdoutLoggerInterface::class));
$listener->process(new stdClass());

$this->assertTrue(true);
}

public function testProcessWithoutIsAutoDeclare()
{
$container = Mockery::mock(ContainerInterface::class);

$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturnUsing(function () {
$logger = Mockery::mock(StdoutLoggerInterface::class);
$logger->shouldReceive('debug')->andReturn(null);
$logger->shouldReceive('log')->andReturn(null);
return $logger;
});

$container->shouldReceive('has')->with(ConfigInterface::class)->andReturnTrue();
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturnUsing(function () {
return new Config([
'amqp' => [
'enable' => true,
],
]);
});

$container->shouldReceive('get')->with(Instantiator::class)->andReturn(new Instantiator());

$producer = Mockery::mock(\Hyperf\Amqp\Producer::class);
$producer->shouldReceive('declare')->withAnyArgs()->once();

$container->shouldReceive('get')->with(\Hyperf\Amqp\Producer::class)->andReturn($producer);

AnnotationCollector::collectClass(DemoDisableAutoDeclareProducer::class, Producer::class, new Producer(
exchange: uniqid(),
routingKey: uniqid(),
));

$listener = new MainWorkerStartListener($container, $container->get(StdoutLoggerInterface::class));
$listener->process(new stdClass());

$this->assertTrue(true);
}
}
31 changes: 31 additions & 0 deletions src/amqp/tests/Stub/DemoDisableAutoDeclareProducer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Amqp\Stub;

use Hyperf\Amqp\Message\ProducerMessage;

class DemoDisableAutoDeclareProducer extends ProducerMessage
{
protected string $exchange = 'hyperf';

protected array|string $routingKey = 'hyperf';

public function __construct($data)
{
$this->payload = $data;
}

public function isAutoDeclare(): bool
{
return false;
}
}
Loading