diff --git a/module/CLI/test/Command/Domain/ListDomainsCommandTest.php b/module/CLI/test/Command/Domain/ListDomainsCommandTest.php index 4df11de4..eb586478 100644 --- a/module/CLI/test/Command/Domain/ListDomainsCommandTest.php +++ b/module/CLI/test/Command/Domain/ListDomainsCommandTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\CLI\Command\Domain\ListDomainsCommand; use Shlinkio\Shlink\CLI\Util\ExitCodes; +use Shlinkio\Shlink\Core\Config\NotFoundRedirects; use Shlinkio\Shlink\Core\Domain\DomainServiceInterface; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Entity\Domain; @@ -28,10 +29,38 @@ class ListDomainsCommandTest extends TestCase $this->commandTester = $this->testerForCommand(new ListDomainsCommand($this->domainService->reveal())); } - /** @test */ - public function allDomainsAreProperlyPrinted(): void + /** + * @test + * @dataProvider provideInputsAndOutputs + */ + public function allDomainsAreProperlyPrinted(array $input, string $expectedOutput): void { - $expectedOutput = <<configureNotFoundRedirects(new NotFoundRedirects( + null, + 'https://foo.com/baz-domain/regular', + 'https://foo.com/baz-domain/invalid', + )); + + $listDomains = $this->domainService->listDomains()->willReturn([ + DomainItem::forDefaultDomain('foo.com', new NotFoundRedirectOptions([ + 'base_url' => 'https://foo.com/default/base', + 'invalid_short_url' => 'https://foo.com/default/invalid', + ])), + DomainItem::forExistingDomain(new Domain('bar.com')), + DomainItem::forExistingDomain($bazDomain), + ]); + + $this->commandTester->execute($input); + + self::assertEquals($expectedOutput, $this->commandTester->getDisplay()); + self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode()); + $listDomains->shouldHaveBeenCalledOnce(); + } + + public function provideInputsAndOutputs(): iterable + { + $withoutRedirectsOutput = <<domainService->listDomains()->willReturn([ - DomainItem::forDefaultDomain('foo.com', new NotFoundRedirectOptions()), - DomainItem::forExistingDomain(new Domain('bar.com')), - DomainItem::forExistingDomain(new Domain('baz.com')), - ]); + $withRedirectsOutput = <<commandTester->execute([]); + OUTPUT; - self::assertEquals($expectedOutput, $this->commandTester->getDisplay()); - self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode()); - $listDomains->shouldHaveBeenCalledOnce(); + yield 'no args' => [[], $withoutRedirectsOutput]; + yield 'no show redirects' => [['--show-redirects' => false], $withoutRedirectsOutput]; + yield 'show redirects' => [['--show-redirects' => true], $withRedirectsOutput]; } } diff --git a/module/Core/test/Domain/DomainServiceTest.php b/module/Core/test/Domain/DomainServiceTest.php index de58b1b5..dc0119c0 100644 --- a/module/Core/test/Domain/DomainServiceTest.php +++ b/module/Core/test/Domain/DomainServiceTest.php @@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use Shlinkio\Shlink\Core\Config\NotFoundRedirects; use Shlinkio\Shlink\Core\Domain\DomainService; use Shlinkio\Shlink\Core\Domain\Model\DomainItem; use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface; @@ -151,6 +152,36 @@ class DomainServiceTest extends TestCase $flush->shouldHaveBeenCalledOnce(); } + /** + * @test + * @dataProvider provideFoundDomains + */ + public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain): void + { + $authority = 'example.com'; + $repo = $this->prophesize(DomainRepositoryInterface::class); + $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain); + $getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal()); + $persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class)); + $flush = $this->em->flush(); + + $result = $this->domainService->configureNotFoundRedirects($authority, new NotFoundRedirects( + 'foo.com', + 'bar.com', + 'baz.com', + )); + + if ($foundDomain !== null) { + self::assertSame($result, $foundDomain); + } + self::assertEquals('foo.com', $result->baseUrlRedirect()); + self::assertEquals('bar.com', $result->regular404Redirect()); + self::assertEquals('baz.com', $result->invalidShortUrlRedirect()); + $getRepo->shouldHaveBeenCalledOnce(); + $persist->shouldHaveBeenCalledOnce(); + $flush->shouldHaveBeenCalledTimes(2); + } + public function provideFoundDomains(): iterable { yield 'domain not found' => [null];