urlResolver = $this->createMock(ShortUrlResolverInterface::class); $this->commandTester = $this->testerForCommand(new ResolveUrlCommand($this->urlResolver)); } /** @test */ public function correctShortCodeResolvesUrl(): void { $shortCode = 'abc123'; $expectedUrl = 'http://domain.com/foo/bar'; $shortUrl = ShortUrl::withLongUrl($expectedUrl); $this->urlResolver->expects($this->once())->method('resolveShortUrl')->with( $this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode)), )->willReturn($shortUrl); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); self::assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output); } /** @test */ public function incorrectShortCodeOutputsErrorMessage(): void { $identifier = ShortUrlIdentifier::fromShortCodeAndDomain('abc123'); $shortCode = $identifier->shortCode; $this->urlResolver->expects($this->once())->method('resolveShortUrl')->with( $this->equalTo($identifier) )->willThrowException(ShortUrlNotFoundException::fromNotFound($identifier)); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); self::assertStringContainsString(sprintf('No URL found with short code "%s"', $shortCode), $output); } }