Migrated ResolveUrlCommandTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya
2022-10-22 13:44:10 +02:00
parent 4872bd3a92
commit 41e903cf26

View File

@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl; namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand; use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
@@ -23,12 +23,12 @@ class ResolveUrlCommandTest extends TestCase
use CliTestUtilsTrait; use CliTestUtilsTrait;
private CommandTester $commandTester; private CommandTester $commandTester;
private ObjectProphecy $urlResolver; private MockObject $urlResolver;
protected function setUp(): void protected function setUp(): void
{ {
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class); $this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
$this->commandTester = $this->testerForCommand(new ResolveUrlCommand($this->urlResolver->reveal())); $this->commandTester = $this->testerForCommand(new ResolveUrlCommand($this->urlResolver));
} }
/** @test */ /** @test */
@@ -37,9 +37,9 @@ class ResolveUrlCommandTest extends TestCase
$shortCode = 'abc123'; $shortCode = 'abc123';
$expectedUrl = 'http://domain.com/foo/bar'; $expectedUrl = 'http://domain.com/foo/bar';
$shortUrl = ShortUrl::withLongUrl($expectedUrl); $shortUrl = ShortUrl::withLongUrl($expectedUrl);
$this->urlResolver->resolveShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode))->willReturn( $this->urlResolver->expects($this->once())->method('resolveShortUrl')->with(
$shortUrl, $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)),
)->shouldBeCalledOnce(); )->willReturn($shortUrl);
$this->commandTester->execute(['shortCode' => $shortCode]); $this->commandTester->execute(['shortCode' => $shortCode]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
@@ -52,9 +52,9 @@ class ResolveUrlCommandTest extends TestCase
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain('abc123'); $identifier = ShortUrlIdentifier::fromShortCodeAndDomain('abc123');
$shortCode = $identifier->shortCode; $shortCode = $identifier->shortCode;
$this->urlResolver->resolveShortUrl($identifier) $this->urlResolver->expects($this->once())->method('resolveShortUrl')->with(
->willThrow(ShortUrlNotFoundException::fromNotFound($identifier)) $this->equalTo($identifier)
->shouldBeCalledOnce(); )->willThrowException(ShortUrlNotFoundException::fromNotFound($identifier));
$this->commandTester->execute(['shortCode' => $shortCode]); $this->commandTester->execute(['shortCode' => $shortCode]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();