Skip to content
Merged
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
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"workerman/workerman": "^4.2.1"
},
"require-dev": {
"blackfire/php-sdk": "^2.5.4",
"blackfire/php-sdk": "^2.5.7",
"chubbyphp/chubbyphp-dev-helper": "dev-master",
"chubbyphp/chubbyphp-mock": "^1.8",
"infection/infection": "^0.29.8",
"chubbyphp/chubbyphp-mock": "^2.0@dev",
"infection/infection": "^0.29.12",
"php-coveralls/php-coveralls": "^2.7",
"phpstan/extension-installer": "^1.4.3",
"phpstan/phpstan": "^2.0.3",
"phpunit/phpunit": "^11.5.0"
"phpstan/phpstan": "^2.1.6",
"phpunit/phpunit": "^11.5.9"
},
"autoload": {
"psr-4": { "Chubbyphp\\WorkermanRequestHandler\\": "src/" }
Expand Down
184 changes: 101 additions & 83 deletions tests/Unit/Adapter/BlackfireOnMessageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Blackfire\Exception\LogicException;
use Blackfire\Probe;
use Blackfire\Profile\Configuration;
use Chubbyphp\Mock\Argument\ArgumentInstanceOf;
use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Chubbyphp\Mock\MockMethod\WithCallback;
use Chubbyphp\Mock\MockMethod\WithException;
use Chubbyphp\Mock\MockMethod\WithoutReturn;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockObjectBuilder;
use Chubbyphp\WorkermanRequestHandler\Adapter\BlackfireOnMessageAdapter;
use Chubbyphp\WorkermanRequestHandler\OnMessageInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
Expand All @@ -26,158 +28,174 @@
*/
final class BlackfireOnMessageAdapterTest extends TestCase
{
use MockByCallsTrait;

#[DoesNotPerformAssertions]
public function testInvokeWithoutHeaderWithoutConfigAndWithoutLogger(): void
{
/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class);
$builder = new MockObjectBuilder();

/** @var WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []);

/** @var MockObject|WorkermanRequest $workermanRequest */
$workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [
Call::create('header')->with('x-blackfire-query', null)->willReturn(null),
/** @var WorkermanRequest $workermanRequest */
$workermanRequest = $builder->create(WorkermanRequest::class, [
new WithReturn('header', ['x-blackfire-query', null], null),
]);

/** @var MockObject|OnMessageInterface $onMessage */
$onMessage = $this->getMockByCalls(OnMessageInterface::class, [
Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest),
/** @var OnMessageInterface $onMessage */
$onMessage = $builder->create(OnMessageInterface::class, [
new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]),
]);

/** @var Client|MockObject $client */
$client = $this->getMockByCalls(Client::class);
/** @var Client $client */
$client = $builder->create(Client::class, []);

$adapter = new BlackfireOnMessageAdapter($onMessage, $client);
$adapter($workermanTcpConnection, $workermanRequest);
}

public function testInvokeWithoutConfigAndWithoutLogger(): void
{
/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class);
$builder = new MockObjectBuilder();

/** @var MockObject|WorkermanRequest $workermanRequest */
$workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [
Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'),
/** @var WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []);

/** @var WorkermanRequest $workermanRequest */
$workermanRequest = $builder->create(WorkermanRequest::class, [
new WithReturn('header', ['x-blackfire-query', null], 'workerman'),
]);

/** @var MockObject|OnMessageInterface $onMessage */
$onMessage = $this->getMockByCalls(OnMessageInterface::class, [
Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest),
/** @var OnMessageInterface $onMessage */
$onMessage = $builder->create(OnMessageInterface::class, [
new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]),
]);

/** @var MockObject|Probe $probe */
$probe = $this->getMockByCalls(Probe::class);
/** @var Probe $probe */
$probe = $builder->create(Probe::class, []);

/** @var Client $client */
$client = $builder->create(Client::class, [
new WithCallback('createProbe', static function (Configuration $configuration, bool $enabled) use ($probe): Probe {
self::assertTrue($enabled);

/** @var Client|MockObject $client */
$client = $this->getMockByCalls(Client::class, [
Call::create('createProbe')->with(new ArgumentInstanceOf(Configuration::class), true)->willReturn($probe),
Call::create('endProbe')->with($probe),
return $probe;
}),
new WithoutReturn('endProbe', [$probe]),
]);

$adapter = new BlackfireOnMessageAdapter($onMessage, $client);
$adapter($workermanTcpConnection, $workermanRequest);
}

#[DoesNotPerformAssertions]
public function testInvokeWithConfigAndWithLogger(): void
{
/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class);
$builder = new MockObjectBuilder();

/** @var MockObject|WorkermanRequest $workermanRequest */
$workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [
Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'),
/** @var WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []);

/** @var WorkermanRequest $workermanRequest */
$workermanRequest = $builder->create(WorkermanRequest::class, [
new WithReturn('header', ['x-blackfire-query', null], 'workerman'),
]);

/** @var MockObject|OnMessageInterface $onMessage */
$onMessage = $this->getMockByCalls(OnMessageInterface::class, [
Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest),
/** @var OnMessageInterface $onMessage */
$onMessage = $builder->create(OnMessageInterface::class, [
new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]),
]);

/** @var Configuration|MockObject $config */
$config = $this->getMockByCalls(Configuration::class);
/** @var Configuration $config */
$config = $builder->create(Configuration::class, []);

/** @var MockObject|Probe $probe */
$probe = $this->getMockByCalls(Probe::class);
/** @var Probe $probe */
$probe = $builder->create(Probe::class, []);

/** @var Client|MockObject $client */
$client = $this->getMockByCalls(Client::class, [
Call::create('createProbe')->with($config, true)->willReturn($probe),
Call::create('endProbe')->with($probe),
/** @var Client $client */
$client = $builder->create(Client::class, [
new WithReturn('createProbe', [$config, true], $probe),
new WithoutReturn('endProbe', [$probe]),
]);

/** @var LoggerInterface|MockObject $logger */
$logger = $this->getMockByCalls(LoggerInterface::class);
/** @var LoggerInterface $logger */
$logger = $builder->create(LoggerInterface::class, []);

$adapter = new BlackfireOnMessageAdapter($onMessage, $client, $config, $logger);
$adapter($workermanTcpConnection, $workermanRequest);
}

#[DoesNotPerformAssertions]
public function testInvokeWithExceptionOnCreateProbe(): void
{
/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class);
$builder = new MockObjectBuilder();

/** @var WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []);

/** @var MockObject|WorkermanRequest $workermanRequest */
$workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [
Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'),
/** @var WorkermanRequest $workermanRequest */
$workermanRequest = $builder->create(WorkermanRequest::class, [
new WithReturn('header', ['x-blackfire-query', null], 'workerman'),
]);

/** @var MockObject|OnMessageInterface $onMessage */
$onMessage = $this->getMockByCalls(OnMessageInterface::class, [
Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest),
/** @var OnMessageInterface $onMessage */
$onMessage = $builder->create(OnMessageInterface::class, [
new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]),
]);

/** @var Configuration|MockObject $config */
$config = $this->getMockByCalls(Configuration::class);
/** @var Configuration $config */
$config = $builder->create(Configuration::class, []);

$exception = new LogicException('Something went wrong');

/** @var Client|MockObject $client */
$client = $this->getMockByCalls(Client::class, [
Call::create('createProbe')->with($config, true)->willThrowException($exception),
/** @var Client $client */
$client = $builder->create(Client::class, [
new WithException('createProbe', [$config, true], $exception),
]);

/** @var LoggerInterface|MockObject $logger */
$logger = $this->getMockByCalls(LoggerInterface::class, [
Call::create('error')->with('Blackfire exception: Something went wrong', []),
/** @var LoggerInterface $logger */
$logger = $builder->create(LoggerInterface::class, [
new WithoutReturn('error', ['Blackfire exception: Something went wrong', []]),
]);

$adapter = new BlackfireOnMessageAdapter($onMessage, $client, $config, $logger);
$adapter($workermanTcpConnection, $workermanRequest);
}

#[DoesNotPerformAssertions]
public function testInvokeWithExceptionOnProbeEnd(): void
{
/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class);
$builder = new MockObjectBuilder();

/** @var WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []);

/** @var MockObject|WorkermanRequest $workermanRequest */
$workermanRequest = $this->getMockByCalls(WorkermanRequest::class, [
Call::create('header')->with('x-blackfire-query', null)->willReturn('workerman'),
/** @var WorkermanRequest $workermanRequest */
$workermanRequest = $builder->create(WorkermanRequest::class, [
new WithReturn('header', ['x-blackfire-query', null], 'workerman'),
]);

/** @var MockObject|OnMessageInterface $onMessage */
$onMessage = $this->getMockByCalls(OnMessageInterface::class, [
Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest),
/** @var OnMessageInterface $onMessage */
$onMessage = $builder->create(OnMessageInterface::class, [
new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]),
]);

/** @var Configuration|MockObject $config */
$config = $this->getMockByCalls(Configuration::class);
/** @var Configuration $config */
$config = $builder->create(Configuration::class, []);

/** @var MockObject|Probe $probe */
$probe = $this->getMockByCalls(Probe::class);
/** @var Probe $probe */
$probe = $builder->create(Probe::class, []);

$exception = new LogicException('Something went wrong');

/** @var Client|MockObject $client */
$client = $this->getMockByCalls(Client::class, [
Call::create('createProbe')->with($config, true)->willReturn($probe),
Call::create('endProbe')->with($probe)->willThrowException($exception),
/** @var Client $client */
$client = $builder->create(Client::class, [
new WithReturn('createProbe', [$config, true], $probe),
new WithException('endProbe', [$probe], $exception),
]);

/** @var LoggerInterface|MockObject $logger */
$logger = $this->getMockByCalls(LoggerInterface::class, [
Call::create('error')->with('Blackfire exception: Something went wrong', []),
/** @var LoggerInterface $logger */
$logger = $builder->create(LoggerInterface::class, [
new WithoutReturn('error', ['Blackfire exception: Something went wrong', []]),
]);

$adapter = new BlackfireOnMessageAdapter($onMessage, $client, $config, $logger);
Expand Down
23 changes: 11 additions & 12 deletions tests/Unit/Adapter/NewRelicOnMessageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ function newrelic_end_transaction(bool $ignore = false): void

namespace Chubbyphp\Tests\WorkermanRequestHandler\Unit\Adapter
{
use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Chubbyphp\Mock\MockMethod\WithoutReturn;
use Chubbyphp\Mock\MockObjectBuilder;
use Chubbyphp\WorkermanRequestHandler\Adapter\NewRelicOnMessageAdapter;
use Chubbyphp\WorkermanRequestHandler\Adapter\TestNewRelicEndTransaction;
use Chubbyphp\WorkermanRequestHandler\Adapter\TestNewRelicStartTransaction;
use Chubbyphp\WorkermanRequestHandler\OnMessageInterface;
use PHPUnit\Framework\TestCase;
use PHPUnit\WorkermanRequestHandler\MockObject\MockObject;
use Workerman\Connection\TcpConnection as WorkermanTcpConnection;
use Workerman\Protocols\Http\Request as WorkermanRequest;

Expand All @@ -87,22 +86,22 @@ function newrelic_end_transaction(bool $ignore = false): void
*/
final class NewRelicOnMessageAdapterTest extends TestCase
{
use MockByCallsTrait;

public function testInvoke(): void
{
TestNewRelicStartTransaction::reset();
TestNewRelicEndTransaction::reset();

/** @var MockObject|WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $this->getMockByCalls(WorkermanTcpConnection::class);
$builder = new MockObjectBuilder();

/** @var WorkermanTcpConnection $workermanTcpConnection */
$workermanTcpConnection = $builder->create(WorkermanTcpConnection::class, []);

/** @var MockObject|WorkermanRequest $workermanRequest */
$workermanRequest = $this->getMockByCalls(WorkermanRequest::class);
/** @var WorkermanRequest $workermanRequest */
$workermanRequest = $builder->create(WorkermanRequest::class, []);

/** @var MockObject|OnMessageInterface $onMessage */
$onMessage = $this->getMockByCalls(OnMessageInterface::class, [
Call::create('__invoke')->with($workermanTcpConnection, $workermanRequest),
/** @var OnMessageInterface $onMessage */
$onMessage = $builder->create(OnMessageInterface::class, [
new WithoutReturn('__invoke', [$workermanTcpConnection, $workermanRequest]),
]);

$adapter = new NewRelicOnMessageAdapter($onMessage, 'myapp');
Expand Down
Loading
Loading