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;
|
2019-12-27 13:12:13 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2019-12-27 13:12:13 -06:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
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\Model\Visitor;
|
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions;
|
2021-02-01 15:55:52 -06:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
|
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
|
2019-12-27 13:12:13 -06:00
|
|
|
|
|
|
|
use function count;
|
|
|
|
use function Functional\contains;
|
|
|
|
|
|
|
|
class NotifyVisitToWebHooksTest extends TestCase
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-29 15:48:40 -06:00
|
|
|
private ObjectProphecy $httpClient;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $logger;
|
2019-12-27 13:12:13 -06:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->httpClient = $this->prophesize(ClientInterface::class);
|
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
|
|
|
$this->logger = $this->prophesize(LoggerInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function emptyWebhooksMakeNoFurtherActions(): void
|
|
|
|
{
|
|
|
|
$find = $this->em->find(Visit::class, '1')->willReturn(null);
|
|
|
|
|
2019-12-28 06:07:11 -06:00
|
|
|
$this->createListener([])(new VisitLocated('1'));
|
2019-12-27 13:12:13 -06:00
|
|
|
|
|
|
|
$find->shouldNotHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function invalidVisitDoesNotPerformAnyRequest(): void
|
|
|
|
{
|
|
|
|
$find = $this->em->find(Visit::class, '1')->willReturn(null);
|
|
|
|
$requestAsync = $this->httpClient->requestAsync(
|
|
|
|
RequestMethodInterface::METHOD_POST,
|
|
|
|
Argument::type('string'),
|
2020-01-01 13:48:31 -06:00
|
|
|
Argument::type('array'),
|
2019-12-27 13:12:13 -06:00
|
|
|
)->willReturn(new FulfilledPromise(''));
|
|
|
|
$logWarning = $this->logger->warning(
|
|
|
|
'Tried to notify webhooks for visit with id "{visitId}", but it does not exist.',
|
2020-01-01 13:48:31 -06:00
|
|
|
['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
|
|
|
|
|
|
|
$find->shouldHaveBeenCalledOnce();
|
|
|
|
$logWarning->shouldHaveBeenCalledOnce();
|
|
|
|
$requestAsync->shouldNotHaveBeenCalled();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function expectedRequestsArePerformedToWebhooks(): void
|
|
|
|
{
|
|
|
|
$webhooks = ['foo', 'invalid', 'bar', 'baz'];
|
|
|
|
$invalidWebhooks = ['invalid', 'baz'];
|
|
|
|
|
2021-01-30 07:18:44 -06:00
|
|
|
$find = $this->em->find(Visit::class, '1')->willReturn(
|
2021-02-07 14:31:12 -06:00
|
|
|
Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()),
|
2021-01-30 07:18:44 -06:00
|
|
|
);
|
2019-12-27 13:12:13 -06:00
|
|
|
$requestAsync = $this->httpClient->requestAsync(
|
|
|
|
RequestMethodInterface::METHOD_POST,
|
|
|
|
Argument::type('string'),
|
2019-12-28 03:43:13 -06:00
|
|
|
Argument::that(function (array $requestOptions) {
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::HEADERS, $requestOptions);
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::JSON, $requestOptions);
|
|
|
|
Assert::assertArrayHasKey(RequestOptions::TIMEOUT, $requestOptions);
|
|
|
|
Assert::assertEquals($requestOptions[RequestOptions::TIMEOUT], 10);
|
|
|
|
Assert::assertEquals($requestOptions[RequestOptions::HEADERS], ['User-Agent' => 'Shlink:v1.2.3']);
|
|
|
|
Assert::assertArrayHasKey('shortUrl', $requestOptions[RequestOptions::JSON]);
|
|
|
|
Assert::assertArrayHasKey('visit', $requestOptions[RequestOptions::JSON]);
|
|
|
|
|
|
|
|
return $requestOptions;
|
2020-01-01 13:48:31 -06:00
|
|
|
}),
|
2019-12-27 13:12:13 -06:00
|
|
|
)->will(function (array $args) use ($invalidWebhooks) {
|
|
|
|
[, $webhook] = $args;
|
|
|
|
$e = new Exception('');
|
|
|
|
|
|
|
|
return contains($invalidWebhooks, $webhook) ? new RejectedPromise($e) : new FulfilledPromise('');
|
|
|
|
});
|
|
|
|
$logWarning = $this->logger->warning(
|
|
|
|
'Failed to notify visit with id "{visitId}" to webhook "{webhook}". {e}',
|
2019-12-28 03:43:13 -06:00
|
|
|
Argument::that(function (array $extra) {
|
|
|
|
Assert::assertArrayHasKey('webhook', $extra);
|
|
|
|
Assert::assertArrayHasKey('visitId', $extra);
|
|
|
|
Assert::assertArrayHasKey('e', $extra);
|
|
|
|
|
|
|
|
return $extra;
|
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
|
|
|
|
|
|
|
$find->shouldHaveBeenCalledOnce();
|
|
|
|
$requestAsync->shouldHaveBeenCalledTimes(count($webhooks));
|
|
|
|
$logWarning->shouldHaveBeenCalledTimes(count($invalidWebhooks));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createListener(array $webhooks): NotifyVisitToWebHooks
|
|
|
|
{
|
|
|
|
return new NotifyVisitToWebHooks(
|
|
|
|
$this->httpClient->reveal(),
|
|
|
|
$this->em->reveal(),
|
|
|
|
$this->logger->reveal(),
|
|
|
|
$webhooks,
|
2021-02-01 15:55:52 -06:00
|
|
|
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
|
2020-01-01 13:48:31 -06:00
|
|
|
new AppOptions(['name' => 'Shlink', 'version' => '1.2.3']),
|
2019-12-27 13:12:13 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|