shlink/module/Core/test/Model/ShortUrlMetaTest.php

103 lines
3.0 KiB
PHP
Raw Normal View History

2018-01-07 13:07:12 -06:00
<?php
2019-10-05 10:26:10 -05:00
2018-01-07 13:07:12 -06:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Model;
use Cake\Chronos\Chronos;
2018-01-07 13:07:12 -06:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
use stdClass;
2018-01-07 13:07:12 -06:00
class ShortUrlMetaTest extends TestCase
{
/**
* @param array $data
* @test
* @dataProvider provideInvalidData
*/
2019-02-17 13:28:34 -06:00
public function exceptionIsThrownIfProvidedDataIsInvalid(array $data): void
2018-01-07 13:07:12 -06:00
{
$this->expectException(ValidationException::class);
ShortUrlMeta::fromRawData($data);
2018-01-07 13:07:12 -06:00
}
2019-02-17 13:28:34 -06:00
public function provideInvalidData(): iterable
2018-01-07 13:07:12 -06:00
{
2019-02-17 13:28:34 -06:00
yield [[
ShortUrlInputFilter::VALID_SINCE => '',
ShortUrlInputFilter::VALID_UNTIL => '',
ShortUrlInputFilter::CUSTOM_SLUG => 'foobar',
ShortUrlInputFilter::MAX_VISITS => 'invalid',
2019-02-17 13:28:34 -06:00
]];
yield [[
ShortUrlInputFilter::VALID_SINCE => '2017',
ShortUrlInputFilter::MAX_VISITS => 5,
2019-02-17 13:28:34 -06:00
]];
yield [[
ShortUrlInputFilter::VALID_SINCE => new stdClass(),
ShortUrlInputFilter::VALID_UNTIL => 'foo',
]];
yield [[
ShortUrlInputFilter::VALID_UNTIL => 500,
ShortUrlInputFilter::DOMAIN => 4,
]];
yield [[
ShortUrlInputFilter::SHORT_CODE_LENGTH => 3,
]];
yield [[
ShortUrlInputFilter::CUSTOM_SLUG => '/',
]];
yield [[
ShortUrlInputFilter::CUSTOM_SLUG => '',
]];
yield [[
ShortUrlInputFilter::CUSTOM_SLUG => ' ',
]];
yield [[
ShortUrlInputFilter::LONG_URL => [],
]];
2018-01-07 13:07:12 -06:00
}
/**
* @test
* @dataProvider provideCustomSlugs
*/
public function properlyCreatedInstanceReturnsValues(string $customSlug, string $expectedSlug): void
2018-01-07 13:07:12 -06:00
{
$meta = ShortUrlMeta::fromRawData([
'validSince' => Chronos::parse('2015-01-01')->toAtomString(),
'customSlug' => $customSlug,
'longUrl' => '',
]);
2018-01-07 13:07:12 -06:00
2020-10-03 17:35:14 -05:00
self::assertTrue($meta->hasValidSince());
self::assertEquals(Chronos::parse('2015-01-01'), $meta->getValidSince());
2018-01-07 13:07:12 -06:00
2020-10-03 17:35:14 -05:00
self::assertFalse($meta->hasValidUntil());
self::assertNull($meta->getValidUntil());
2018-01-07 13:07:12 -06:00
2020-10-03 17:35:14 -05:00
self::assertTrue($meta->hasCustomSlug());
self::assertEquals($expectedSlug, $meta->getCustomSlug());
2018-01-07 13:07:12 -06:00
2020-10-03 17:35:14 -05:00
self::assertFalse($meta->hasMaxVisits());
self::assertNull($meta->getMaxVisits());
2018-01-07 13:07:12 -06:00
}
public function provideCustomSlugs(): iterable
{
yield ['foobar', 'foobar'];
yield ['foo bar', 'foo-bar'];
yield ['wp-admin.php', 'wp-admin.php'];
yield ['UPPER_lower', 'UPPER_lower'];
yield ['more~url_special.chars', 'more~url_special.chars'];
yield ['äéñ', 'äen'];
yield ['구글', '구글'];
yield ['グーグル', 'グーグル'];
yield ['谷歌', '谷歌'];
yield ['гугл', 'гугл'];
}
2018-01-07 13:07:12 -06:00
}