2019-08-24 03:25:43 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2019-08-24 03:25:43 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Model;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
|
|
|
|
2020-03-14 13:24:21 -05:00
|
|
|
use function random_int;
|
2019-08-24 03:25:43 -05:00
|
|
|
use function str_repeat;
|
2020-03-14 13:24:21 -05:00
|
|
|
use function strlen;
|
2019-08-24 03:25:43 -05:00
|
|
|
use function substr;
|
|
|
|
|
|
|
|
class VisitorTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideParams
|
|
|
|
*/
|
|
|
|
public function providedFieldsValuesAreCropped(array $params, array $expected): void
|
|
|
|
{
|
|
|
|
$visitor = new Visitor(...$params);
|
|
|
|
['userAgent' => $userAgent, 'referer' => $referer, 'remoteAddress' => $remoteAddress] = $expected;
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($userAgent, $visitor->getUserAgent());
|
|
|
|
self::assertEquals($referer, $visitor->getReferer());
|
|
|
|
self::assertEquals($remoteAddress, $visitor->getRemoteAddress());
|
2019-08-24 03:25:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideParams(): iterable
|
|
|
|
{
|
|
|
|
yield 'all values are bigger' => [
|
|
|
|
[str_repeat('a', 1000), str_repeat('b', 2000), str_repeat('c', 500)],
|
|
|
|
[
|
|
|
|
'userAgent' => str_repeat('a', Visitor::USER_AGENT_MAX_LENGTH),
|
|
|
|
'referer' => str_repeat('b', Visitor::REFERER_MAX_LENGTH),
|
|
|
|
'remoteAddress' => str_repeat('c', Visitor::REMOTE_ADDRESS_MAX_LENGTH),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
yield 'some values are smaller' => [
|
|
|
|
[str_repeat('a', 10), str_repeat('b', 2000), null],
|
|
|
|
[
|
|
|
|
'userAgent' => str_repeat('a', 10),
|
|
|
|
'referer' => str_repeat('b', Visitor::REFERER_MAX_LENGTH),
|
|
|
|
'remoteAddress' => null,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
yield 'random strings' => [
|
|
|
|
[
|
|
|
|
$userAgent = $this->generateRandomString(2000),
|
|
|
|
$referer = $this->generateRandomString(50),
|
|
|
|
null,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'userAgent' => substr($userAgent, 0, Visitor::USER_AGENT_MAX_LENGTH),
|
|
|
|
'referer' => $referer,
|
|
|
|
'remoteAddress' => null,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2020-03-14 13:24:21 -05:00
|
|
|
|
|
|
|
private function generateRandomString(int $length): string
|
|
|
|
{
|
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
$charactersLength = strlen($characters);
|
|
|
|
$randomString = '';
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$randomString .= $characters[random_int(0, $charactersLength - 1)];
|
|
|
|
}
|
|
|
|
return $randomString;
|
|
|
|
}
|
2019-08-24 03:25:43 -05:00
|
|
|
}
|