2019-12-27 13:12:13 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-11 11:00:29 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
|
2019-12-27 13:12:13 -06:00
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Exception;
|
|
|
|
use Fig\Http\Message\RequestMethodInterface;
|
|
|
|
use GuzzleHttp\ClientInterface;
|
|
|
|
use GuzzleHttp\Promise\FulfilledPromise;
|
|
|
|
use GuzzleHttp\Promise\RejectedPromise;
|
2019-12-28 03:43:13 -06:00
|
|
|
use GuzzleHttp\RequestOptions;
|
|
|
|
use PHPUnit\Framework\Assert;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-22 01:08:49 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-12-27 13:12:13 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2021-01-17 04:42:35 -06:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
2019-12-27 13:12:13 -06:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\NotifyVisitToWebHooks;
|
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
2021-10-09 03:53:21 -05:00
|
|
|
use Shlinkio\Shlink\Core\Options\WebhookOptions;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
2022-09-23 12:03:32 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
2022-09-23 11:05:17 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
2019-12-27 13:12:13 -06:00
|
|
|
|
|
|
|
use function count;
|
|
|
|
use function Functional\contains;
|
|
|
|
|
|
|
|
class NotifyVisitToWebHooksTest extends TestCase
|
|
|
|
{
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & ClientInterface $httpClient;
|
|
|
|
private MockObject & EntityManagerInterface $em;
|
|
|
|
private MockObject & LoggerInterface $logger;
|
2019-12-27 13:12:13 -06:00
|
|
|
|
2022-09-11 05:02:49 -05:00
|
|
|
protected function setUp(): void
|
2019-12-27 13:12:13 -06:00
|
|
|
{
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->httpClient = $this->createMock(ClientInterface::class);
|
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2019-12-27 13:12:13 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-12-27 13:12:13 -06:00
|
|
|
public function emptyWebhooksMakeNoFurtherActions(): void
|
|
|
|
{
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->em->expects($this->never())->method('find');
|
2019-12-27 13:12:13 -06:00
|
|
|
|
2019-12-28 06:07:11 -06:00
|
|
|
$this->createListener([])(new VisitLocated('1'));
|
2019-12-27 13:12:13 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2019-12-27 13:12:13 -06:00
|
|
|
public function invalidVisitDoesNotPerformAnyRequest(): void
|
|
|
|
{
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '1')->willReturn(null);
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->httpClient->expects($this->never())->method('requestAsync');
|
|
|
|
$this->logger->expects($this->once())->method('warning')->with(
|
2022-10-23 11:15:57 -05:00
|
|
|
'Tried to notify webhooks for visit with id "{visitId}", but it does not exist.',
|
|
|
|
['visitId' => '1'],
|
2019-12-27 13:12:13 -06:00
|
|
|
);
|
|
|
|
|
2019-12-28 06:07:11 -06:00
|
|
|
$this->createListener(['foo', 'bar'])(new VisitLocated('1'));
|
2019-12-27 13:12:13 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test]
|
2021-10-09 03:53:21 -05:00
|
|
|
public function orphanVisitDoesNotPerformAnyRequestWhenDisabled(): void
|
|
|
|
{
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '1')->willReturn(
|
|
|
|
Visit::forBasePath(Visitor::emptyInstance()),
|
|
|
|
);
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->httpClient->expects($this->never())->method('requestAsync');
|
|
|
|
$this->logger->expects($this->never())->method('warning');
|
2021-10-09 03:53:21 -05:00
|
|
|
|
|
|
|
$this->createListener(['foo', 'bar'], false)(new VisitLocated('1'));
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:42:18 -06:00
|
|
|
#[Test, DataProvider('provideVisits')]
|
2021-10-09 03:35:10 -05:00
|
|
|
public function expectedRequestsArePerformedToWebhooks(Visit $visit, array $expectedResponseKeys): void
|
2019-12-27 13:12:13 -06:00
|
|
|
{
|
|
|
|
$webhooks = ['foo', 'invalid', 'bar', 'baz'];
|
|
|
|
$invalidWebhooks = ['invalid', 'baz'];
|
|
|
|
|
2022-10-23 11:15:57 -05:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Visit::class, '1')->willReturn($visit);
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->httpClient->expects($this->exactly(count($webhooks)))->method('requestAsync')->with(
|
2022-10-23 11:15:57 -05:00
|
|
|
RequestMethodInterface::METHOD_POST,
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->istype('string'),
|
|
|
|
$this->callback(function (array $requestOptions) use ($expectedResponseKeys) {
|
2019-12-28 03:43:13 -06:00
|
|
|
Assert::assertArrayHasKey(RequestOptions::HEADERS, $requestOptions);
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::JSON, $requestOptions);
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::TIMEOUT, $requestOptions);
|
2022-10-22 01:08:49 -05:00
|
|
|
Assert::assertEquals(10, $requestOptions[RequestOptions::TIMEOUT]);
|
|
|
|
Assert::assertEquals(['User-Agent' => 'Shlink:v1.2.3'], $requestOptions[RequestOptions::HEADERS]);
|
2021-10-09 03:35:10 -05:00
|
|
|
|
|
|
|
$json = $requestOptions[RequestOptions::JSON];
|
|
|
|
Assert::assertCount(count($expectedResponseKeys), $json);
|
|
|
|
foreach ($expectedResponseKeys as $key) {
|
|
|
|
Assert::assertArrayHasKey($key, $json);
|
|
|
|
}
|
2019-12-28 03:43:13 -06:00
|
|
|
|
2022-10-22 01:08:49 -05:00
|
|
|
return true;
|
2020-01-01 13:48:31 -06:00
|
|
|
}),
|
2022-10-22 01:08:49 -05:00
|
|
|
)->willReturnCallback(function ($_, $webhook) use ($invalidWebhooks) {
|
2021-10-09 03:35:10 -05:00
|
|
|
$shouldReject = contains($invalidWebhooks, $webhook);
|
|
|
|
return $shouldReject ? new RejectedPromise(new Exception('')) : new FulfilledPromise('');
|
2019-12-27 13:12:13 -06:00
|
|
|
});
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->logger->expects($this->exactly(count($invalidWebhooks)))->method('warning')->with(
|
2022-10-23 11:15:57 -05:00
|
|
|
'Failed to notify visit with id "{visitId}" to webhook "{webhook}". {e}',
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->callback(function (array $extra): bool {
|
2019-12-28 03:43:13 -06:00
|
|
|
Assert::assertArrayHasKey('webhook', $extra);
|
|
|
|
Assert::assertArrayHasKey('visitId', $extra);
|
|
|
|
Assert::assertArrayHasKey('e', $extra);
|
|
|
|
|
2022-10-22 01:08:49 -05:00
|
|
|
return true;
|
2020-01-01 13:48:31 -06:00
|
|
|
}),
|
2019-12-27 13:12:13 -06:00
|
|
|
);
|
|
|
|
|
2019-12-28 06:07:11 -06:00
|
|
|
$this->createListener($webhooks)(new VisitLocated('1'));
|
2019-12-27 13:12:13 -06:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:32:38 -06:00
|
|
|
public static function provideVisits(): iterable
|
2021-10-09 03:35:10 -05:00
|
|
|
{
|
|
|
|
yield 'regular visit' => [
|
2023-01-22 04:27:16 -06:00
|
|
|
Visit::forValidShortUrl(ShortUrl::createFake(), Visitor::emptyInstance()),
|
2021-10-09 03:35:10 -05:00
|
|
|
['shortUrl', 'visit'],
|
|
|
|
];
|
2023-02-09 02:32:38 -06:00
|
|
|
yield 'orphan visit' => [Visit::forBasePath(Visitor::emptyInstance()), ['visit']];
|
2021-10-09 03:35:10 -05:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:53:21 -05:00
|
|
|
private function createListener(array $webhooks, bool $notifyOrphanVisits = true): NotifyVisitToWebHooks
|
2019-12-27 13:12:13 -06:00
|
|
|
{
|
|
|
|
return new NotifyVisitToWebHooks(
|
2022-10-22 01:08:49 -05:00
|
|
|
$this->httpClient,
|
|
|
|
$this->em,
|
|
|
|
$this->logger,
|
2021-10-09 03:53:21 -05:00
|
|
|
new WebhookOptions(
|
2022-01-16 08:34:07 -06:00
|
|
|
['webhooks' => $webhooks, 'notify_orphan_visits_to_webhooks' => $notifyOrphanVisits],
|
2021-10-09 03:53:21 -05:00
|
|
|
),
|
2021-02-01 15:55:52 -06:00
|
|
|
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
2022-09-17 06:01:28 -05:00
|
|
|
new AppOptions('Shlink', '1.2.3'),
|
2019-12-27 13:12:13 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|