urlShortener = $this->prophesize(UrlShortener::class); $command = new GenerateShortcodeCommand($this->urlShortener->reveal(), Translator::factory([]), [ 'schema' => 'http', 'hostname' => 'foo.com', ]); $app = new Application(); $app->add($command); $this->commandTester = new CommandTester($command); } /** * @test */ public function properShortCodeIsCreatedIfLongUrlIsCorrect() { $this->urlShortener->urlToShortCode(Argument::cetera()) ->willReturn( (new ShortUrl())->setShortCode('abc123') ->setLongUrl('') ) ->shouldBeCalledTimes(1); $this->commandTester->execute([ 'command' => 'shortcode:generate', 'longUrl' => 'http://domain.com/foo/bar', ]); $output = $this->commandTester->getDisplay(); $this->assertTrue(strpos($output, 'http://foo.com/abc123') > 0); } /** * @test */ public function exceptionWhileParsingLongUrlOutputsError() { $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(new InvalidUrlException()) ->shouldBeCalledTimes(1); $this->commandTester->execute([ 'command' => 'shortcode:generate', 'longUrl' => 'http://domain.com/invalid', ]); $output = $this->commandTester->getDisplay(); $this->assertContains( 'Provided URL "http://domain.com/invalid" is invalid.', $output ); } }