shlink/module/Core/test/Visit/VisitsTrackerTest.php

48 lines
1.4 KiB
PHP
Raw Normal View History

2016-05-01 10:54:56 -05:00
<?php
2019-10-05 10:26:10 -05:00
2017-10-12 03:13:20 -05:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Visit;
2016-05-01 10:54:56 -05:00
use Doctrine\ORM\EntityManager;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
2016-05-01 10:54:56 -05:00
use Prophecy\Argument;
2020-11-02 04:50:19 -06:00
use Prophecy\PhpUnit\ProphecyTrait;
2016-07-30 15:55:28 -05:00
use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface;
2016-07-19 11:01:39 -05:00
use Shlinkio\Shlink\Core\Entity\ShortUrl;
2016-07-30 15:55:28 -05:00
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlVisited;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\VisitsTracker;
2016-05-01 10:54:56 -05:00
class VisitsTrackerTest extends TestCase
{
2020-11-02 04:50:19 -06:00
use ProphecyTrait;
private VisitsTracker $visitsTracker;
private ObjectProphecy $em;
private ObjectProphecy $eventDispatcher;
2016-07-30 15:55:28 -05:00
2019-02-16 03:53:45 -06:00
public function setUp(): void
2016-07-30 15:55:28 -05:00
{
$this->em = $this->prophesize(EntityManager::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$this->visitsTracker = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal(), true);
2016-07-30 15:55:28 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
public function trackPersistsVisit(): void
2016-05-01 10:54:56 -05:00
{
$shortCode = '123ABC';
$this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->shouldBeCalledOnce();
$this->em->flush()->shouldBeCalledOnce();
2016-07-30 15:55:28 -05:00
$this->visitsTracker->track(ShortUrl::withLongUrl($shortCode), Visitor::emptyInstance());
$this->eventDispatcher->dispatch(Argument::type(ShortUrlVisited::class))->shouldHaveBeenCalled();
2016-07-30 15:55:28 -05:00
}
2016-05-01 10:54:56 -05:00
}