2018-01-07 13:07:12 -06:00
|
|
|
<?php
|
|
|
|
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;
|
|
|
|
|
|
|
|
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::createFromRawData($data);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
]];
|
2018-01-07 13:07:12 -06:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
|
|
|
public function properlyCreatedInstanceReturnsValues(): void
|
2018-01-07 13:07:12 -06:00
|
|
|
{
|
2018-09-29 05:52:32 -05:00
|
|
|
$meta = ShortUrlMeta::createFromParams(Chronos::parse('2015-01-01')->toAtomString(), null, 'foobar');
|
2018-01-07 13:07:12 -06:00
|
|
|
|
|
|
|
$this->assertTrue($meta->hasValidSince());
|
2018-09-29 05:52:32 -05:00
|
|
|
$this->assertEquals(Chronos::parse('2015-01-01'), $meta->getValidSince());
|
2018-01-07 13:07:12 -06:00
|
|
|
|
|
|
|
$this->assertFalse($meta->hasValidUntil());
|
|
|
|
$this->assertNull($meta->getValidUntil());
|
|
|
|
|
|
|
|
$this->assertTrue($meta->hasCustomSlug());
|
|
|
|
$this->assertEquals('foobar', $meta->getCustomSlug());
|
|
|
|
|
|
|
|
$this->assertFalse($meta->hasMaxVisits());
|
|
|
|
$this->assertNull($meta->getMaxVisits());
|
|
|
|
}
|
|
|
|
}
|