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;
|
|
|
|
|
2018-09-29 05:52:32 -05:00
|
|
|
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\ShortUrlMetaInputFilter;
|
2019-12-06 15:38:22 -06:00
|
|
|
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);
|
2020-01-26 01:42:51 -06:00
|
|
|
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 [[
|
|
|
|
ShortUrlMetaInputFilter::VALID_SINCE => '',
|
|
|
|
ShortUrlMetaInputFilter::VALID_UNTIL => '',
|
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => 'foobar',
|
|
|
|
ShortUrlMetaInputFilter::MAX_VISITS => 'invalid',
|
|
|
|
]];
|
|
|
|
yield [[
|
|
|
|
ShortUrlMetaInputFilter::VALID_SINCE => '2017',
|
|
|
|
ShortUrlMetaInputFilter::MAX_VISITS => 5,
|
|
|
|
]];
|
2019-12-06 15:38:22 -06:00
|
|
|
yield [[
|
|
|
|
ShortUrlMetaInputFilter::VALID_SINCE => new stdClass(),
|
|
|
|
ShortUrlMetaInputFilter::VALID_UNTIL => 'foo',
|
|
|
|
]];
|
|
|
|
yield [[
|
|
|
|
ShortUrlMetaInputFilter::VALID_UNTIL => 500,
|
|
|
|
ShortUrlMetaInputFilter::DOMAIN => 4,
|
|
|
|
]];
|
2020-02-18 11:57:24 -06:00
|
|
|
yield [[
|
|
|
|
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 3,
|
|
|
|
]];
|
2020-03-06 13:01:41 -06:00
|
|
|
yield [[
|
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => '/',
|
|
|
|
]];
|
|
|
|
yield [[
|
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => '',
|
|
|
|
]];
|
|
|
|
yield [[
|
|
|
|
ShortUrlMetaInputFilter::CUSTOM_SLUG => ' ',
|
|
|
|
]];
|
2018-01-07 13:07:12 -06:00
|
|
|
}
|
|
|
|
|
2020-06-08 11:08:46 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideCustomSlugs
|
|
|
|
*/
|
|
|
|
public function properlyCreatedInstanceReturnsValues(string $customSlug, string $expectedSlug): void
|
2018-01-07 13:07:12 -06:00
|
|
|
{
|
2020-01-26 01:42:51 -06:00
|
|
|
$meta = ShortUrlMeta::fromRawData(
|
2020-06-08 11:08:46 -05:00
|
|
|
['validSince' => Chronos::parse('2015-01-01')->toAtomString(), 'customSlug' => $customSlug],
|
2020-01-26 01:42:51 -06:00
|
|
|
);
|
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
|
|
|
}
|
2020-06-08 11:08:46 -05: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'];
|
|
|
|
}
|
2018-01-07 13:07:12 -06:00
|
|
|
}
|