Skip to content

Commit

Permalink
Merge pull request #8 from acelaya-forks/feature/consistencies
Browse files Browse the repository at this point in the history
Feature/consistencies
  • Loading branch information
acelaya authored Oct 24, 2020
2 parents d942e85 + 54d2255 commit 9d3364f
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 62 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ 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).

## 1.0.1 - 2020-10-24

#### Added

* *Nothing*

#### Changed

* *Nothing*

#### Deprecated

* *Nothing*

#### Removed

* *Nothing*

#### Fixed

* Fixed `short-url:import` command throwing an exception if source is not provided.


## 1.0.0 - 2020-10-22

#### Added
Expand Down
10 changes: 9 additions & 1 deletion src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
$source = $input->getArgument('source');
$validSources = ImportSources::getAll();

if (! contains($validSources, $source)) {
if ($source !== null && ! contains($validSources, $source)) {
throw InvalidSourceException::fromInvalidSource($source);
}

if ($source === null) {
$source = (new SymfonyStyle($input, $output))->choice(
'What is the source you want to import from:',
$validSources,
);
$input->setArgument('source', $source);
}
}

protected function execute(InputInterface $input, OutputInterface $output): ?int
Expand Down
4 changes: 2 additions & 2 deletions src/ImportedLinksProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Shlinkio\Shlink\Importer;

use Shlinkio\Shlink\Importer\Model\ShlinkUrl;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;

interface ImportedLinksProcessorInterface
{
/**
* @param ShlinkUrl[] $shlinkUrls
* @param ImportedShlinkUrl[] $shlinkUrls
*/
public function process(iterable $shlinkUrls, string $source, array $params): void;
}
55 changes: 55 additions & 0 deletions src/Model/ImportedShlinkUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\Importer\Model;

use DateTimeInterface;

class ImportedShlinkUrl
{
private string $longUrl;
private array $tags;
private DateTimeInterface $createdAt;
private ?string $domain;
private ?string $shortCode;

public function __construct(
string $longUrl,
array $tags,
DateTimeInterface $createdAt,
?string $domain,
?string $shortCode
) {
$this->longUrl = $longUrl;
$this->tags = $tags;
$this->createdAt = $createdAt;
$this->domain = $domain;
$this->shortCode = $shortCode;
}

public function longUrl(): string
{
return $this->longUrl;
}

public function tags(): array
{
return $this->tags;
}

public function createdAt(): DateTimeInterface
{
return $this->createdAt;
}

public function domain(): ?string
{
return $this->domain;
}

public function shortCode(): ?string
{
return $this->shortCode;
}
}
49 changes: 2 additions & 47 deletions src/Model/ShlinkUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,7 @@

namespace Shlinkio\Shlink\Importer\Model;

use DateTimeInterface;

final class ShlinkUrl
/** @deprecated Use ImportedShlinkUrl instead */
class ShlinkUrl extends ImportedShlinkUrl
{
private string $longUrl;
private array $tags;
private DateTimeInterface $createdAt;
private ?string $domain;
private ?string $shortCode;

public function __construct(
string $longUrl,
array $tags,
DateTimeInterface $createdAt,
?string $domain,
?string $shortCode
) {
$this->longUrl = $longUrl;
$this->tags = $tags;
$this->createdAt = $createdAt;
$this->domain = $domain;
$this->shortCode = $shortCode;
}

public function longUrl(): string
{
return $this->longUrl;
}

public function tags(): array
{
return $this->tags;
}

public function createdAt(): DateTimeInterface
{
return $this->createdAt;
}

public function domain(): ?string
{
return $this->domain;
}

public function shortCode(): ?string
{
return $this->shortCode;
}
}
10 changes: 5 additions & 5 deletions src/Strategy/BitlyApiImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Shlinkio\Shlink\Importer\Exception\BitlyApiException;
use Shlinkio\Shlink\Importer\Exception\ImportException;
use Shlinkio\Shlink\Importer\Model\BitlyApiProgressTracker;
use Shlinkio\Shlink\Importer\Model\ShlinkUrl;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Shlinkio\Shlink\Importer\Params\BitlyApiParams;
use Shlinkio\Shlink\Importer\Util\DateHelpersTrait;
use Throwable;
Expand Down Expand Up @@ -40,7 +40,7 @@ public function __construct(ClientInterface $httpClient, RequestFactoryInterface
}

/**
* @return ShlinkUrl[]
* @return ImportedShlinkUrl[]
* @throws ImportException
*/
public function import(array $rawParams): iterable
Expand Down Expand Up @@ -70,7 +70,7 @@ public function import(array $rawParams): iterable
}

/**
* @return ShlinkUrl[]
* @return ImportedShlinkUrl[]
* @throws ClientExceptionInterface
* @throws JsonException
*/
Expand Down Expand Up @@ -98,7 +98,7 @@ private function loadUrlsForGroup(
static fn (array $link): bool => isset($link['long_url']) && ! empty($link['long_url']),
);

yield from map($filteredLinks, function (array $link) use ($params, $progressTracker): ShlinkUrl {
yield from map($filteredLinks, function (array $link) use ($params, $progressTracker): ImportedShlinkUrl {
$hasCreatedDate = isset($link['created_at']);
if ($hasCreatedDate) {
$progressTracker->updateLastProcessedUrlDate($link['created_at']);
Expand All @@ -113,7 +113,7 @@ private function loadUrlsForGroup(
$shortCode = ltrim($parsedLink['path'] ?? '', '/');
$tags = $params->importTags() ? $link['tags'] ?? [] : [];

return new ShlinkUrl($link['long_url'], $tags, $date, $domain, $shortCode);
return new ImportedShlinkUrl($link['long_url'], $tags, $date, $domain, $shortCode);
});
} while (! empty($pagination['next']));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Strategy/ImporterStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Shlinkio\Shlink\Importer\Strategy;

use Shlinkio\Shlink\Importer\Exception\ImportException;
use Shlinkio\Shlink\Importer\Model\ShlinkUrl;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;

interface ImporterStrategyInterface
{
/**
* @return ShlinkUrl[]
* @return ImportedShlinkUrl[]
* @throws ImportException
*/
public function import(array $params): iterable;
Expand Down
26 changes: 22 additions & 4 deletions test/Command/ImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,41 @@ public function exceptionIsThrownWhenInvalidSourceIsProvided(): void
$this->commandTester->execute(['source' => 'invalid']);
}

/** @test */
public function dependenciesAreInvokedAsExpected(): void
/**
* @test
* @dataProvider provideSource
*/
public function dependenciesAreInvokedAsExpected(?string $providedSource, bool $expectSourceQuestion): void
{
$source = ImportSources::BITLY;
$source = $providedSource ?? ImportSources::BITLY;

$requestParams = $this->paramsHelper->requestParams(Argument::type(StyleInterface::class))->willReturn([]);
$import = $this->importerStrategy->import([])->willReturn([]);
$process = $this->importedLinksProcessor->process([], $source, []);

$exitCode = $this->commandTester->execute(['source' => $source]);
if ($expectSourceQuestion) {
$this->commandTester->setInputs(['0']);
}
$exitCode = $this->commandTester->execute(['source' => $providedSource]);
$output = $this->commandTester->getDisplay();

self::assertEquals(ImportCommand::SUCCESS, $exitCode);
$this->consoleHelperManager->get($source)->shouldHaveBeenCalledOnce();
$this->importerStrategyManager->get($source)->shouldHaveBeenCalledOnce();
$requestParams->shouldHaveBeenCalledOnce();
$import->shouldHaveBeenCalledOnce();
$process->shouldHaveBeenCalledOnce();
if ($expectSourceQuestion) {
self::assertStringContainsString('What is the source you want to import from:', $output);
} else {
self::assertStringNotContainsString('What is the source you want to import from:', $output);
}
}

public function provideSource(): iterable
{
yield 'provided source' => [ImportSources::BITLY, false];
yield 'not provided source' => [null, true];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/Strategy/BitlyApiImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Shlinkio\Shlink\Importer\Exception\BitlyApiException;
use Shlinkio\Shlink\Importer\Model\ShlinkUrl;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl as ShlinkUrl;
use Shlinkio\Shlink\Importer\Strategy\BitlyApiImporter;

use function explode;
Expand Down

0 comments on commit 9d3364f

Please sign in to comment.