Skip to content

Commit

Permalink
Test MatomoVisitSender::sendVisitsInDateRange
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Apr 13, 2024
1 parent 6121efe commit bbdbafd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions module/Core/test/Matomo/MatomoVisitSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace ShlinkioTest\Shlink\Core\Matomo;

use Exception;
use Laminas\Validator\Date;
use MatomoTracker;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Matomo\MatomoTrackerBuilderInterface;
use Shlinkio\Shlink\Core\Matomo\MatomoVisitSender;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
Expand Down Expand Up @@ -127,4 +130,45 @@ public static function provideUrlsToTrack(): iterable
'http://s2.test/bar',
];
}

#[Test]
public function multipleVisitsCanBeSent(): void
{
$dateRange = DateRange::allTime();
$visitor = Visitor::emptyInstance();
$bot = Visitor::botInstance();

$this->visitIterationRepository->expects($this->once())->method('findAllVisits')->with($dateRange)->willReturn([
Visit::forBasePath($bot),
Visit::forValidShortUrl(ShortUrl::createFake(), $visitor),
Visit::forInvalidShortUrl($visitor),
]);

$tracker = $this->createMock(MatomoTracker::class);
$tracker->method('setUrl')->willReturn($tracker);
$tracker->method('setUserAgent')->willReturn($tracker);
$tracker->method('setUrlReferrer')->willReturn($tracker);
$tracker->method('setCustomTrackingParameter')->willReturn($tracker);

$callCount = 0;
$this->trackerBuilder->expects($this->exactly(3))->method('buildMatomoTracker')->willReturnCallback(
function () use (&$callCount, $tracker) {
$callCount++;

if ($callCount === 2) {
throw new Exception('Error');
}

return $tracker;
},
);

$result = $this->visitSender->sendVisitsInDateRange($dateRange);

self::assertEquals(2, $result->successfulVisits);
self::assertEquals(1, $result->failedVisits);
self::assertCount(3, $result);
self::assertTrue($result->hasSuccesses());
self::assertTrue($result->hasFailures());
}
}

0 comments on commit bbdbafd

Please sign in to comment.