urlShortener = $this->prophesize(UrlShortener::class); $command = new ResolveUrlCommand($this->urlShortener->reveal()); $app = new Application(); $app->add($command); $this->commandTester = new CommandTester($command); } /** @test */ public function correctShortCodeResolvesUrl(): void { $shortCode = 'abc123'; $expectedUrl = 'http://domain.com/foo/bar'; $shortUrl = new ShortUrl($expectedUrl); $this->urlShortener->shortCodeToUrl($shortCode, null)->willReturn($shortUrl) ->shouldBeCalledOnce(); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); $this->assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output); } /** @test */ public function incorrectShortCodeOutputsErrorMessage(): void { $shortCode = 'abc123'; $this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(EntityDoesNotExistException::class) ->shouldBeCalledOnce(); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); $this->assertStringContainsString('Provided short code "' . $shortCode . '" could not be found.', $output); } /** @test */ public function wrongShortCodeFormatOutputsErrorMessage(): void { $shortCode = 'abc123'; $this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(new InvalidShortCodeException()) ->shouldBeCalledOnce(); $this->commandTester->execute(['shortCode' => $shortCode]); $output = $this->commandTester->getDisplay(); $this->assertStringContainsString('Provided short code "' . $shortCode . '" has an invalid format.', $output); } }