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

93 lines
2.8 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\ShortUrlMetaInputFilter;
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 [[
ShortUrlMetaInputFilter::VALID_SINCE => '',
ShortUrlMetaInputFilter::VALID_UNTIL => '',
ShortUrlMetaInputFilter::CUSTOM_SLUG => 'foobar',
ShortUrlMetaInputFilter::MAX_VISITS => 'invalid',
]];
yield [[
ShortUrlMetaInputFilter::VALID_SINCE => '2017',
ShortUrlMetaInputFilter::MAX_VISITS => 5,
]];
yield [[
ShortUrlMetaInputFilter::VALID_SINCE => new stdClass(),
ShortUrlMetaInputFilter::VALID_UNTIL => 'foo',
]];
yield [[
ShortUrlMetaInputFilter::VALID_UNTIL => 500,
ShortUrlMetaInputFilter::DOMAIN => 4,
]];
yield [[
ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => 3,
]];
yield [[
ShortUrlMetaInputFilter::CUSTOM_SLUG => '/',
]];
yield [[
ShortUrlMetaInputFilter::CUSTOM_SLUG => '',
]];
yield [[
ShortUrlMetaInputFilter::CUSTOM_SLUG => ' ',
]];
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],
);
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'];
}
2018-01-07 13:07:12 -06:00
}