diff --git a/module/CLI/test/Command/ListShortcodesCommandTest.php b/module/CLI/test/Command/ListShortcodesCommandTest.php new file mode 100644 index 00000000..7ec0e5af --- /dev/null +++ b/module/CLI/test/Command/ListShortcodesCommandTest.php @@ -0,0 +1,119 @@ +shortUrlService = $this->prophesize(ShortUrlServiceInterface::class); + $app = new Application(); + $command = new ListShortcodesCommand($this->shortUrlService->reveal(), Translator::factory([])); + $app->add($command); + + $this->questionHelper = $command->getHelper('question'); + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function noInputCallsListJustOnce() + { + $this->questionHelper->setInputStream($this->getInputStream('\n')); + $this->shortUrlService->listShortUrls(1)->willReturn(new Paginator(new ArrayAdapter())) + ->shouldBeCalledTimes(1); + + $this->commandTester->execute(['command' => 'shortcode:list']); + } + + /** + * @test + */ + public function loadingMorePagesCallsListMoreTimes() + { + // The paginator will return more than one page for the first 3 times + $data = []; + for ($i = 0; $i < 30; $i++) { + $data[] = new ShortUrl(); + } + $data = array_chunk($data, 11); + + $questionHelper = $this->questionHelper; + $that = $this; + $this->shortUrlService->listShortUrls(Argument::any())->will(function () use (&$data, $questionHelper, $that) { + $questionHelper->setInputStream($that->getInputStream('y')); + return new Paginator(new ArrayAdapter(array_shift($data))); + })->shouldBeCalledTimes(3); + + $this->commandTester->execute(['command' => 'shortcode:list']); + } + + /** + * @test + */ + public function havingMorePagesButAnsweringNoCallsListJustOnce() + { + // The paginator will return more than one page + $data = []; + for ($i = 0; $i < 30; $i++) { + $data[] = new ShortUrl(); + } + + $this->questionHelper->setInputStream($this->getInputStream('n')); + $this->shortUrlService->listShortUrls(Argument::any())->willReturn(new Paginator(new ArrayAdapter($data))) + ->shouldBeCalledTimes(1); + + $this->commandTester->execute(['command' => 'shortcode:list']); + } + + /** + * @test + */ + public function passingPageWillMakeListStartOnThatPage() + { + $page = 5; + $this->questionHelper->setInputStream($this->getInputStream('\n')); + $this->shortUrlService->listShortUrls($page)->willReturn(new Paginator(new ArrayAdapter())) + ->shouldBeCalledTimes(1); + + $this->commandTester->execute([ + 'command' => 'shortcode:list', + '--page' => $page, + ]); + } + + protected function getInputStream($inputData) + { + $stream = fopen('php://memory', 'r+', false); + fputs($stream, $inputData); + rewind($stream); + + return $stream; + } +} diff --git a/module/CLI/test/ConfigProviderTest.php b/module/CLI/test/ConfigProviderTest.php new file mode 100644 index 00000000..8d368561 --- /dev/null +++ b/module/CLI/test/ConfigProviderTest.php @@ -0,0 +1,30 @@ +configProvider = new ConfigProvider(); + } + + /** + * @test + */ + public function confiIsProperlyReturned() + { + $config = $this->configProvider->__invoke(); + + $this->assertArrayHasKey('cli', $config); + $this->assertArrayHasKey('services', $config); + $this->assertArrayHasKey('translator', $config); + } +}