mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Migrated NotifyVisitToWebHooksTest to use PHPUnit mocks
This commit is contained in:
@@ -12,10 +12,8 @@ use GuzzleHttp\Promise\FulfilledPromise;
|
|||||||
use GuzzleHttp\Promise\RejectedPromise;
|
use GuzzleHttp\Promise\RejectedPromise;
|
||||||
use GuzzleHttp\RequestOptions;
|
use GuzzleHttp\RequestOptions;
|
||||||
use PHPUnit\Framework\Assert;
|
use PHPUnit\Framework\Assert;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToWebHooks;
|
use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToWebHooks;
|
||||||
@@ -32,66 +30,52 @@ use function Functional\contains;
|
|||||||
|
|
||||||
class NotifyVisitToWebHooksTest extends TestCase
|
class NotifyVisitToWebHooksTest extends TestCase
|
||||||
{
|
{
|
||||||
use ProphecyTrait;
|
private MockObject $httpClient;
|
||||||
|
private MockObject $em;
|
||||||
private ObjectProphecy $httpClient;
|
private MockObject $logger;
|
||||||
private ObjectProphecy $em;
|
|
||||||
private ObjectProphecy $logger;
|
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->httpClient = $this->prophesize(ClientInterface::class);
|
$this->httpClient = $this->createMock(ClientInterface::class);
|
||||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||||
$this->logger = $this->prophesize(LoggerInterface::class);
|
$this->logger = $this->createMock(LoggerInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function emptyWebhooksMakeNoFurtherActions(): void
|
public function emptyWebhooksMakeNoFurtherActions(): void
|
||||||
{
|
{
|
||||||
$find = $this->em->find(Visit::class, '1')->willReturn(null);
|
$this->em->expects($this->never())->method('find');
|
||||||
|
|
||||||
$this->createListener([])(new VisitLocated('1'));
|
$this->createListener([])(new VisitLocated('1'));
|
||||||
|
|
||||||
$find->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function invalidVisitDoesNotPerformAnyRequest(): void
|
public function invalidVisitDoesNotPerformAnyRequest(): void
|
||||||
{
|
{
|
||||||
$find = $this->em->find(Visit::class, '1')->willReturn(null);
|
$this->em->expects($this->once())->method('find')->with(
|
||||||
$requestAsync = $this->httpClient->requestAsync(
|
$this->equalTo(Visit::class),
|
||||||
RequestMethodInterface::METHOD_POST,
|
$this->equalTo('1'),
|
||||||
Argument::type('string'),
|
)->willReturn(null);
|
||||||
Argument::type('array'),
|
$this->httpClient->expects($this->never())->method('requestAsync');
|
||||||
)->willReturn(new FulfilledPromise(''));
|
$this->logger->expects($this->once())->method('warning')->with(
|
||||||
$logWarning = $this->logger->warning(
|
$this->equalTo('Tried to notify webhooks for visit with id "{visitId}", but it does not exist.'),
|
||||||
'Tried to notify webhooks for visit with id "{visitId}", but it does not exist.',
|
$this->equalTo(['visitId' => '1']),
|
||||||
['visitId' => '1'],
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->createListener(['foo', 'bar'])(new VisitLocated('1'));
|
$this->createListener(['foo', 'bar'])(new VisitLocated('1'));
|
||||||
|
|
||||||
$find->shouldHaveBeenCalledOnce();
|
|
||||||
$logWarning->shouldHaveBeenCalledOnce();
|
|
||||||
$requestAsync->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function orphanVisitDoesNotPerformAnyRequestWhenDisabled(): void
|
public function orphanVisitDoesNotPerformAnyRequestWhenDisabled(): void
|
||||||
{
|
{
|
||||||
$find = $this->em->find(Visit::class, '1')->willReturn(Visit::forBasePath(Visitor::emptyInstance()));
|
$this->em->expects($this->once())->method('find')->with(
|
||||||
$requestAsync = $this->httpClient->requestAsync(
|
$this->equalTo(Visit::class),
|
||||||
RequestMethodInterface::METHOD_POST,
|
$this->equalTo('1'),
|
||||||
Argument::type('string'),
|
)->willReturn(Visit::forBasePath(Visitor::emptyInstance()));
|
||||||
Argument::type('array'),
|
$this->httpClient->expects($this->never())->method('requestAsync');
|
||||||
)->willReturn(new FulfilledPromise(''));
|
$this->logger->expects($this->never())->method('warning');
|
||||||
$logWarning = $this->logger->warning(Argument::cetera());
|
|
||||||
|
|
||||||
$this->createListener(['foo', 'bar'], false)(new VisitLocated('1'));
|
$this->createListener(['foo', 'bar'], false)(new VisitLocated('1'));
|
||||||
|
|
||||||
$find->shouldHaveBeenCalledOnce();
|
|
||||||
$logWarning->shouldNotHaveBeenCalled();
|
|
||||||
$requestAsync->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,16 +87,19 @@ class NotifyVisitToWebHooksTest extends TestCase
|
|||||||
$webhooks = ['foo', 'invalid', 'bar', 'baz'];
|
$webhooks = ['foo', 'invalid', 'bar', 'baz'];
|
||||||
$invalidWebhooks = ['invalid', 'baz'];
|
$invalidWebhooks = ['invalid', 'baz'];
|
||||||
|
|
||||||
$find = $this->em->find(Visit::class, '1')->willReturn($visit);
|
$this->em->expects($this->once())->method('find')->with(
|
||||||
$requestAsync = $this->httpClient->requestAsync(
|
$this->equalTo(Visit::class),
|
||||||
RequestMethodInterface::METHOD_POST,
|
$this->equalTo('1'),
|
||||||
Argument::type('string'),
|
)->willReturn($visit);
|
||||||
Argument::that(function (array $requestOptions) use ($expectedResponseKeys) {
|
$this->httpClient->expects($this->exactly(count($webhooks)))->method('requestAsync')->with(
|
||||||
|
$this->equalTo(RequestMethodInterface::METHOD_POST),
|
||||||
|
$this->istype('string'),
|
||||||
|
$this->callback(function (array $requestOptions) use ($expectedResponseKeys) {
|
||||||
Assert::assertArrayHasKey(RequestOptions::HEADERS, $requestOptions);
|
Assert::assertArrayHasKey(RequestOptions::HEADERS, $requestOptions);
|
||||||
Assert::assertArrayHasKey(RequestOptions::JSON, $requestOptions);
|
Assert::assertArrayHasKey(RequestOptions::JSON, $requestOptions);
|
||||||
Assert::assertArrayHasKey(RequestOptions::TIMEOUT, $requestOptions);
|
Assert::assertArrayHasKey(RequestOptions::TIMEOUT, $requestOptions);
|
||||||
Assert::assertEquals($requestOptions[RequestOptions::TIMEOUT], 10);
|
Assert::assertEquals(10, $requestOptions[RequestOptions::TIMEOUT]);
|
||||||
Assert::assertEquals($requestOptions[RequestOptions::HEADERS], ['User-Agent' => 'Shlink:v1.2.3']);
|
Assert::assertEquals(['User-Agent' => 'Shlink:v1.2.3'], $requestOptions[RequestOptions::HEADERS]);
|
||||||
|
|
||||||
$json = $requestOptions[RequestOptions::JSON];
|
$json = $requestOptions[RequestOptions::JSON];
|
||||||
Assert::assertCount(count($expectedResponseKeys), $json);
|
Assert::assertCount(count($expectedResponseKeys), $json);
|
||||||
@@ -120,30 +107,24 @@ class NotifyVisitToWebHooksTest extends TestCase
|
|||||||
Assert::assertArrayHasKey($key, $json);
|
Assert::assertArrayHasKey($key, $json);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $requestOptions;
|
return true;
|
||||||
}),
|
}),
|
||||||
)->will(function (array $args) use ($invalidWebhooks) {
|
)->willReturnCallback(function ($_, $webhook) use ($invalidWebhooks) {
|
||||||
[, $webhook] = $args;
|
|
||||||
$shouldReject = contains($invalidWebhooks, $webhook);
|
$shouldReject = contains($invalidWebhooks, $webhook);
|
||||||
|
|
||||||
return $shouldReject ? new RejectedPromise(new Exception('')) : new FulfilledPromise('');
|
return $shouldReject ? new RejectedPromise(new Exception('')) : new FulfilledPromise('');
|
||||||
});
|
});
|
||||||
$logWarning = $this->logger->warning(
|
$this->logger->expects($this->exactly(count($invalidWebhooks)))->method('warning')->with(
|
||||||
'Failed to notify visit with id "{visitId}" to webhook "{webhook}". {e}',
|
$this->equalTo('Failed to notify visit with id "{visitId}" to webhook "{webhook}". {e}'),
|
||||||
Argument::that(function (array $extra) {
|
$this->callback(function (array $extra): bool {
|
||||||
Assert::assertArrayHasKey('webhook', $extra);
|
Assert::assertArrayHasKey('webhook', $extra);
|
||||||
Assert::assertArrayHasKey('visitId', $extra);
|
Assert::assertArrayHasKey('visitId', $extra);
|
||||||
Assert::assertArrayHasKey('e', $extra);
|
Assert::assertArrayHasKey('e', $extra);
|
||||||
|
|
||||||
return $extra;
|
return true;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->createListener($webhooks)(new VisitLocated('1'));
|
$this->createListener($webhooks)(new VisitLocated('1'));
|
||||||
|
|
||||||
$find->shouldHaveBeenCalledOnce();
|
|
||||||
$requestAsync->shouldHaveBeenCalledTimes(count($webhooks));
|
|
||||||
$logWarning->shouldHaveBeenCalledTimes(count($invalidWebhooks));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideVisits(): iterable
|
public function provideVisits(): iterable
|
||||||
@@ -158,9 +139,9 @@ class NotifyVisitToWebHooksTest extends TestCase
|
|||||||
private function createListener(array $webhooks, bool $notifyOrphanVisits = true): NotifyVisitToWebHooks
|
private function createListener(array $webhooks, bool $notifyOrphanVisits = true): NotifyVisitToWebHooks
|
||||||
{
|
{
|
||||||
return new NotifyVisitToWebHooks(
|
return new NotifyVisitToWebHooks(
|
||||||
$this->httpClient->reveal(),
|
$this->httpClient,
|
||||||
$this->em->reveal(),
|
$this->em,
|
||||||
$this->logger->reveal(),
|
$this->logger,
|
||||||
new WebhookOptions(
|
new WebhookOptions(
|
||||||
['webhooks' => $webhooks, 'notify_orphan_visits_to_webhooks' => $notifyOrphanVisits],
|
['webhooks' => $webhooks, 'notify_orphan_visits_to_webhooks' => $notifyOrphanVisits],
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user