forked from shlinkio/shlink-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request shlinkio#10 from acelaya-forks/feature/short-url-t…
…ests Feature/short url tests
- Loading branch information
Showing
13 changed files
with
813 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
test/ShortUrls/Exception/DeleteShortUrlThresholdExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\ShortUrls\Exception; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\SDK\Http\Exception\HttpException; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Exception\DeleteShortUrlThresholdException; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Model\ShortUrlIdentifier; | ||
|
||
class DeleteShortUrlThresholdExceptionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider provideExceptions | ||
*/ | ||
public function exceptionIsProperlyCreated( | ||
HttpException $prev, | ||
ShortUrlIdentifier $expectedIdentifier, | ||
int $expectedThreshold, | ||
string $expectedMessage, | ||
int $expectedCode, | ||
): void { | ||
$e = DeleteShortUrlThresholdException::fromHttpException($prev); | ||
|
||
self::assertEquals($expectedIdentifier, $e->identifier()); | ||
self::assertEquals($expectedThreshold, $e->threshold()); | ||
self::assertEquals($expectedMessage, $e->getMessage()); | ||
self::assertEquals($expectedCode, $e->getCode()); | ||
} | ||
|
||
public function provideExceptions(): iterable | ||
{ | ||
yield [HttpException::fromPayload([]), ShortUrlIdentifier::fromShortCode(''), 0, '', -1]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 404, | ||
]), ShortUrlIdentifier::fromShortCode(''), 0, $message, $code]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 400, | ||
'shortCode' => $shortCode = 'foo', | ||
'domain' => $domain = 'doma.in', | ||
'threshold' => $threshold = 15, | ||
]), ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, $domain), $threshold, $message, $code]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\ShortUrls\Exception; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\SDK\Http\Exception\HttpException; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Exception\InvalidLongUrlException; | ||
|
||
class InvalidLongUrlExceptionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider provideExceptions | ||
*/ | ||
public function exceptionIsProperlyCreated( | ||
HttpException $prev, | ||
string $expectedLongUrl, | ||
string $expectedMessage, | ||
int $expectedCode, | ||
): void { | ||
$e = InvalidLongUrlException::fromHttpException($prev); | ||
|
||
self::assertEquals($expectedLongUrl, $e->longUrl()); | ||
self::assertEquals($expectedMessage, $e->getMessage()); | ||
self::assertEquals($expectedCode, $e->getCode()); | ||
} | ||
|
||
public function provideExceptions(): iterable | ||
{ | ||
yield [HttpException::fromPayload([]), '', '', -1]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 400, | ||
]), '', $message, $code]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 404, | ||
'url' => $url = 'https://foo.com/baz', | ||
]), $url, $message, $code]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\ShortUrls\Exception; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\SDK\Http\Exception\HttpException; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Exception\NonUniqueSlugException; | ||
|
||
class NonUniqueSlugExceptionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider provideExceptions | ||
*/ | ||
public function exceptionIsProperlyCreated( | ||
HttpException $prev, | ||
string $expectedCustomSlug, | ||
?string $expectedDomain, | ||
string $expectedMessage, | ||
int $expectedCode, | ||
): void { | ||
$e = NonUniqueSlugException::fromHttpException($prev); | ||
|
||
self::assertEquals($expectedCustomSlug, $e->customSlug()); | ||
self::assertEquals($expectedDomain, $e->domain()); | ||
self::assertEquals($expectedMessage, $e->getMessage()); | ||
self::assertEquals($expectedCode, $e->getCode()); | ||
} | ||
|
||
public function provideExceptions(): iterable | ||
{ | ||
yield [HttpException::fromPayload([]), '', null, '', -1]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 400, | ||
]), '', null, $message, $code]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 404, | ||
'customSlug' => $customSlug = 'baz', | ||
'domain' => $domain = 'doma.in', | ||
]), $customSlug, $domain, $message, $code]; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
test/ShortUrls/Exception/ShortUrlNotFoundExceptionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\ShortUrls\Exception; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\SDK\Http\Exception\HttpException; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Exception\ShortUrlNotFoundException; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Model\ShortUrlIdentifier; | ||
|
||
class ShortUrlNotFoundExceptionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider provideExceptions | ||
*/ | ||
public function exceptionIsProperlyCreated( | ||
HttpException $prev, | ||
ShortUrlIdentifier $expectedIdentifier, | ||
string $expectedMessage, | ||
int $expectedCode, | ||
): void { | ||
$e = ShortUrlNotFoundException::fromHttpException($prev); | ||
|
||
self::assertEquals($expectedIdentifier, $e->identifier()); | ||
self::assertEquals($expectedMessage, $e->getMessage()); | ||
self::assertEquals($expectedCode, $e->getCode()); | ||
} | ||
|
||
public function provideExceptions(): iterable | ||
{ | ||
yield [HttpException::fromPayload([]), ShortUrlIdentifier::fromShortCode(''), '', -1]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 404, | ||
]), ShortUrlIdentifier::fromShortCode(''), $message, $code]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 400, | ||
'shortCode' => $shortCode = 'foo', | ||
]), ShortUrlIdentifier::fromShortCode($shortCode), $message, $code]; | ||
yield [HttpException::fromPayload([ | ||
'detail' => $message = 'This is the message', | ||
'status' => $code = 400, | ||
'shortCode' => $shortCode = 'foo', | ||
'domain' => $domain = 'doma.in', | ||
]), ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, $domain), $message, $code]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\ShortUrls\Model; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Model\ShortUrlCreation; | ||
|
||
class ShortUrlCreationTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider provideConfigs | ||
*/ | ||
public function payloadIsBuiltAsExpected(callable $createObject, array $expectedPayload): void | ||
{ | ||
/** @var ShortUrlCreation $creation */ | ||
$creation = $createObject(); | ||
|
||
self::assertEquals($expectedPayload, $creation->jsonSerialize()); | ||
} | ||
|
||
public function provideConfigs(): iterable | ||
{ | ||
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2021-01-01'); | ||
|
||
yield [fn () => ShortUrlCreation::forLongUrl('https://foo.com'), ['longUrl' => 'https://foo.com']]; | ||
yield [ | ||
fn () => ShortUrlCreation::forLongUrl('https://foo.com')->returnExistingMatchingShortUrl(), | ||
['longUrl' => 'https://foo.com', 'findIfExists' => true], | ||
]; | ||
yield [ | ||
fn () => ShortUrlCreation::forLongUrl('https://foo.com') | ||
->withTags('foo', 'bar') | ||
->validSince($date) | ||
->withCustomSlug('some-slug'), | ||
[ | ||
'longUrl' => 'https://foo.com', | ||
'tags' => ['foo', 'bar'], | ||
'customSlug' => 'some-slug', | ||
'validSince' => $date->format(DateTimeInterface::ATOM), | ||
], | ||
]; | ||
yield [ | ||
fn () => ShortUrlCreation::forLongUrl('https://foo.com') | ||
->withCustomSlug('some-slug') | ||
->withShortCodeLength(50), | ||
['longUrl' => 'https://foo.com', 'shortCodeLength' => 50], | ||
]; | ||
yield [ | ||
fn () => ShortUrlCreation::forLongUrl('https://foo.com') | ||
->withShortCodeLength(50) | ||
->withCustomSlug('some-slug'), | ||
['longUrl' => 'https://foo.com', 'customSlug' => 'some-slug'], | ||
]; | ||
yield [ | ||
fn () => ShortUrlCreation::forLongUrl('https://foo.com') | ||
->notValidatingTheLongUrl() | ||
->crawlable(), | ||
['longUrl' => 'https://foo.com', 'validateUrl' => false, 'crawlable' => true], | ||
]; | ||
yield [ | ||
fn () => ShortUrlCreation::forLongUrl('https://foo.com')->validatingTheLongUrl(), | ||
['longUrl' => 'https://foo.com', 'validateUrl' => true], | ||
]; | ||
yield [ | ||
fn () => ShortUrlCreation::forLongUrl('https://foo.com') | ||
->withoutQueryForwardingOnRedirect() | ||
->forDomain('doma.in'), | ||
['longUrl' => 'https://foo.com', 'forwardQuery' => false, 'domain' => 'doma.in'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\ShortUrls\Model; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Shlinkio\Shlink\SDK\ShortUrls\Model\ShortUrlEdition; | ||
|
||
class ShortUrlEditionTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider provideConfigs | ||
*/ | ||
public function payloadIsBuiltAsExpected(callable $createObject, array $expectedPayload): void | ||
{ | ||
/** @var ShortUrlEdition $creation */ | ||
$creation = $createObject(); | ||
|
||
self::assertEquals($expectedPayload, $creation->jsonSerialize()); | ||
} | ||
|
||
public function provideConfigs(): iterable | ||
{ | ||
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2021-01-01'); | ||
|
||
yield [fn () => ShortUrlEdition::create(), []]; | ||
yield [ | ||
fn () => ShortUrlEdition::create() | ||
->withTags('foo', 'bar') | ||
->validUntil($date) | ||
->withTitle('the title') | ||
->withMaxVisits(50), | ||
[ | ||
'tags' => ['foo', 'bar'], | ||
'maxVisits' => 50, | ||
'validUntil' => $date->format(DateTimeInterface::ATOM), | ||
'title' => 'the title', | ||
], | ||
]; | ||
yield [ | ||
fn () => ShortUrlEdition::create() | ||
->withLongUrl('https://edited.com/foo/bar') | ||
->notValidatingTheLongUrl() | ||
->withoutTags(), | ||
['longUrl' => 'https://edited.com/foo/bar', 'validateUrl' => false, 'tags' => []], | ||
]; | ||
yield [ | ||
fn () => ShortUrlEdition::create() | ||
->removingValidUntil() | ||
->removingValidSince() | ||
->removingMaxVisits() | ||
->removingTitle() | ||
->notCrawlable() | ||
->withQueryForwardingOnRedirect(), | ||
[ | ||
'maxVisits' => null, | ||
'validUntil' => null, | ||
'validSince' => null, | ||
'title' => null, | ||
'forwardQuery' => true, | ||
'crawlable' => false, | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.