2016-05-01 18:54:56 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-02-08 21:50:17 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Visit;
|
2016-05-01 18:54:56 +03:00
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-05-01 18:54:56 +03:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-07-30 23:55:28 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-07-13 13:04:21 +03:00
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
2016-07-19 19:01:39 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 23:55:28 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2021-02-09 22:25:28 +03:00
|
|
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
|
2018-10-18 21:19:22 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2021-02-08 21:50:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsTracker;
|
2020-02-01 19:34:16 +03:00
|
|
|
|
2016-05-01 18:54:56 +03:00
|
|
|
class VisitsTrackerTest extends TestCase
|
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-30 00:48:40 +03:00
|
|
|
private VisitsTracker $visitsTracker;
|
|
|
|
private ObjectProphecy $em;
|
|
|
|
private ObjectProphecy $eventDispatcher;
|
2016-07-30 23:55:28 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-07-30 23:55:28 +03:00
|
|
|
{
|
|
|
|
$this->em = $this->prophesize(EntityManager::class);
|
2019-07-13 13:04:21 +03:00
|
|
|
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
|
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
$this->visitsTracker = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal(), true);
|
2016-07-30 23:55:28 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 22:34:12 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideTrackingMethodNames
|
|
|
|
*/
|
|
|
|
public function trackPersistsVisitAndDispatchesEvent(string $method, array $args): void
|
2016-05-01 18:54:56 +03:00
|
|
|
{
|
2019-12-30 01:16:55 +03:00
|
|
|
$this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->shouldBeCalledOnce();
|
2019-11-20 22:03:06 +03:00
|
|
|
$this->em->flush()->shouldBeCalledOnce();
|
2016-07-30 23:55:28 +03:00
|
|
|
|
2021-02-09 22:34:12 +03:00
|
|
|
$this->visitsTracker->{$method}(...$args);
|
2019-07-13 13:04:21 +03:00
|
|
|
|
2021-02-09 22:25:28 +03:00
|
|
|
$this->eventDispatcher->dispatch(Argument::type(UrlVisited::class))->shouldHaveBeenCalled();
|
2016-07-30 23:55:28 +03:00
|
|
|
}
|
2021-02-09 22:34:12 +03:00
|
|
|
|
|
|
|
public function provideTrackingMethodNames(): iterable
|
|
|
|
{
|
|
|
|
yield 'track' => ['track', [ShortUrl::createEmpty(), Visitor::emptyInstance()]];
|
|
|
|
yield 'trackInvalidShortUrlVisit' => ['trackInvalidShortUrlVisit', [Visitor::emptyInstance()]];
|
|
|
|
yield 'trackBaseUrlVisit' => ['trackBaseUrlVisit', [Visitor::emptyInstance()]];
|
|
|
|
yield 'trackRegularNotFoundVisit' => ['trackRegularNotFoundVisit', [Visitor::emptyInstance()]];
|
|
|
|
}
|
2016-05-01 18:54:56 +03:00
|
|
|
}
|