Skip to content

Commit

Permalink
Merge pull request shlinkio#10 from acelaya-forks/feature/short-url-t…
Browse files Browse the repository at this point in the history
…ests

Feature/short url tests
  • Loading branch information
acelaya authored Dec 4, 2021
2 parents adabe2e + d1d3833 commit 394b553
Show file tree
Hide file tree
Showing 13 changed files with 813 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]
## [0.1.0] - 2021-12-04
### Added
* *Nothing*
* First release

### Changed
* *Nothing*
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"test": "phpdbg -qrr vendor/bin/phpunit --order-by=random --testdox --colors=always",
"test:ci": "@test --coverage-clover=build/clover.xml --coverage-xml=build/coverage-xml --log-junit=build/junit.xml",
"test:pretty": "@test --coverage-html build/coverage-html",
"infect": "infection --threads=4 --min-msi=80 --log-verbosity=default --only-covered --only-covering-test-cases",
"infect": "infection --threads=4 --min-msi=90 --log-verbosity=default --only-covered --only-covering-test-cases",
"infect:ci": "@infect --coverage=build --skip-initial-tests",
"infect:show": "@infect --show-mutations",
"infect:show:ci": "@infect --show-mutations --coverage=build",
Expand Down
4 changes: 2 additions & 2 deletions src/ShortUrls/ShortUrlsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function listShortUrls(): ShortUrlsList
*/
public function listShortUrlsWithFilter(ShortUrlsFilter $filter): ShortUrlsList
{
$buildQueryWithPage = static function (int $page) use ($filter): array {
$query = $filter->toArray();
$query = $filter->toArray();
$buildQueryWithPage = static function (int $page) use ($query): array {
$query['itemsPerPage'] = ShortUrlsList::ITEMS_PER_PAGE;
$query['page'] = $page;

Expand Down
48 changes: 48 additions & 0 deletions test/ShortUrls/Exception/DeleteShortUrlThresholdExceptionTest.php
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];
}
}
43 changes: 43 additions & 0 deletions test/ShortUrls/Exception/InvalidLongUrlExceptionTest.php
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];
}
}
46 changes: 46 additions & 0 deletions test/ShortUrls/Exception/NonUniqueSlugExceptionTest.php
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 test/ShortUrls/Exception/ShortUrlNotFoundExceptionTest.php
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];
}
}
76 changes: 76 additions & 0 deletions test/ShortUrls/Model/ShortUrlCreationTest.php
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'],
];
}
}
69 changes: 69 additions & 0 deletions test/ShortUrls/Model/ShortUrlEditionTest.php
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,
],
];
}
}
Loading

0 comments on commit 394b553

Please sign in to comment.