public function testExerciseRendererSetsCurrentExerciseAndRendersExercise() { $menu = $this->getMockBuilder(CliMenu::class)->disableOriginalConstructor()->getMock(); $item = $this->getMock(MenuItemInterface::class); $item->expects($this->any())->method('getText')->will($this->returnValue('Exercise 2')); $menu->expects($this->once())->method('getSelectedItem')->will($this->returnValue($item)); $menu->expects($this->once())->method('close'); $exercise1 = $this->getMock(ExerciseInterface::class); $exercise2 = $this->getMock(ExerciseInterface::class); $exercises = [$exercise1, $exercise2]; $exerciseRepository = $this->getMockBuilder(ExerciseRepository::class)->disableOriginalConstructor()->getMock(); $userState = $this->getMock(UserState::class); $userStateSerializer = $this->getMockBuilder(UserStateSerializer::class)->disableOriginalConstructor()->getMock(); $exerciseRepository->expects($this->once())->method('findByName')->with('Exercise 2')->will($this->returnValue($exercise2)); $exerciseRepository->expects($this->once())->method('findAll')->will($this->returnValue($exercises)); $userState->expects($this->once())->method('setCurrentExercise')->with('Exercise 2'); $userStateSerializer->expects($this->once())->method('serialize')->with($userState); $problemFile = sprintf('%s/%s/problem.md', sys_get_temp_dir(), $this->getName()); $exercise2->expects($this->once())->method('getProblem')->will($this->returnValue($problemFile)); if (!is_dir(dirname($problemFile))) { mkdir(dirname($problemFile), 0775, true); } file_put_contents($problemFile, '### Exercise Content'); $markdownRenderer = new MarkdownRenderer(new DocParser(Environment::createCommonMarkEnvironment()), (new CliRendererFactory())->__invoke()); $color = new Color(); $color->setForceStyle(true); $exerciseRenderer = new ExerciseRenderer('phpschool', $exerciseRepository, $userState, $userStateSerializer, $markdownRenderer, $color, new StdOutput($color, $this->getMock(TerminalInterface::class))); $this->expectOutputString(file_get_contents(__DIR__ . '/res/exercise-help-expected.txt')); $exerciseRenderer->__invoke($menu); unlink($problemFile); }
public function testVerifyDoesNotAddCompletedExerciseAndReturnsCorrectCodeOnFailure() { $file = tempnam(sys_get_temp_dir(), 'pws'); touch($file); $input = new Input('appName', ['program' => $file]); $exercise = new CliExerciseImpl(); $repo = new ExerciseRepository([$exercise]); $state = new UserState(); $state->setCurrentExercise('my-exercise'); $color = new Color(); $color->setForceStyle(true); $output = new StdOutput($color, $this->createMock(TerminalInterface::class)); $serializer = $this->createMock(UserStateSerializer::class); $serializer->expects($this->never())->method('serialize')->with($state); $renderer = $this->createMock(ResultsRenderer::class); $results = new ResultAggregator(); $results->add(new Failure($this->check, 'cba')); $dispatcher = $this->createMock(ExerciseDispatcher::class); $dispatcher->expects($this->once())->method('verify')->with($exercise, $input)->will($this->returnValue($results)); $renderer->expects($this->once())->method('render')->with($results, $exercise, $state, $output); $command = new VerifyCommand($repo, $dispatcher, $state, $serializer, $output, $renderer); $this->assertEquals(1, $command->__invoke($input)); $this->assertEquals([], $state->getCompletedExercises()); unlink($file); }
/** * @return SyntaxHighlighter */ public function __invoke() { $lexer = new Lexer(['usedAttributes' => ['comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'startTokenPos', 'endTokenPos']]); $parserFactory = new ParserFactory(); $color = new Color(); $color->setForceStyle(true); return new SyntaxHighlighter($parserFactory->create(ParserFactory::PREFER_PHP7, $lexer), new SyntaxHighlightPrinter(new SyntaxHighlighterConfig(), new ColorsAdapter($color))); }
public function testWithNoContributors() { $this->expectOutputString(''); $color = new Color(); $color->setForceStyle(true); $command = new CreditsCommand([], [], new StdOutput($color, $this->getMock(TerminalInterface::class)), $color); $command->__invoke(); }
public function testInvoke() { $this->expectOutputString(file_get_contents(__DIR__ . '/../res/app-help-expected.txt')); $color = new Color(); $color->setForceStyle(true); $command = new HelpCommand('learnyouphp', new StdOutput($color, $this->getMock(TerminalInterface::class)), $color); $command->__invoke(); }
public function testLineBreak() { $color = new Color(); $color->setForceStyle(true); $terminal = $this->getMock(TerminalInterface::class); $terminal->expects($this->once())->method('getWidth')->will($this->returnValue(10)); $renderer = new ResultsRenderer('app', $color, $terminal, new ExerciseRepository([]), (new Factory())->__invoke(), new ResultRendererFactory()); $this->assertSame("[33m──────────[0m", $renderer->lineBreak()); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $rule = new HorizontalRule(); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [], $color); $this->assertEquals("[90m------------------------------[0m", $renderer->render($rule, $cliRenderer)); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $emphasis = new Newline(); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [], $color); $this->assertEquals("\n", $renderer->render($emphasis, $cliRenderer)); }
public function testRunOutputsErrorMessage() { $color = new Color(); $color->setForceStyle(true); $output = new StdOutput($color, $this->createMock(TerminalInterface::class)); $exp = 'Nothing to run here. This exercise does not require a code solution, '; $exp .= "so there is nothing to execute.\n"; $this->expectOutputString($exp); $this->runner->run(new Input('app'), $output); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $code = new Code('SOME CODE'); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [], $color); $this->assertEquals("[33mSOME CODE[0m", $renderer->render($code, $cliRenderer)); }
/** * @return ResultsRenderer */ protected function getRenderer() { $color = new Color(); $color->setForceStyle(true); $this->terminal = $this->createMock(TerminalInterface::class); $exerciseRepo = $this->createMock(ExerciseRepository::class); $this->terminal->expects($this->any())->method('getWidth')->will($this->returnValue(20)); $syntaxHighlighter = (new Factory())->__invoke(); return new ResultsRenderer('appName', $color, $this->terminal, $exerciseRepo, $syntaxHighlighter, new ResultRendererFactory()); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $text = new Text('HEY'); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [], $color); $this->assertEquals("HEY", $renderer->render($text, $cliRenderer)); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $emphasis = new Emphasis(); $emphasis->appendChild(new Text('Some Text')); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [Text::class => new TextRenderer()], $color); $this->assertEquals("[3mSome Text[0m", $renderer->render($emphasis, $cliRenderer)); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $link = new Link('http://www.google.com'); $link->appendChild(new Text('http://www.google.com')); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [Text::class => new TextRenderer()], $color); $this->assertSame("[94m[1m[4mhttp://www.google.com[0m[0m[0m", $renderer->render($link, $cliRenderer)); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $header = new Header(2, 'HEADING!!'); $header->appendChild(new Text('HEADING!!')); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [Text::class => new TextRenderer()], $color); $this->assertEquals("\n[90m##[0m [36m[1mHEADING!![0m[0m\n", $renderer->render($header, $cliRenderer)); }
public function testRenderNonePhpCodeIsRendererYellow() { $renderer = $this->getRenderer(); $code = $this->getMockBuilder(FencedCode::class)->disableOriginalConstructor()->getMock(); $code->expects($this->once())->method('getStringContent')->will($this->returnValue('console.log("lol js???")')); $code->expects($this->once())->method('getInfoWords')->will($this->returnValue(['js'])); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [], $color); $this->assertEquals(' [33mconsole.log("lol js???")[0m', $renderer->render($code, $cliRenderer)); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $doc = new Document(); $doc->appendChild(new Paragraph()); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([Paragraph::class => new ParagraphRenderer()], [], $color); $this->assertEquals("\n\n", $renderer->render($doc, $cliRenderer)); }
/** * @return CliRenderer */ public function __invoke() { $highlighterFactory = new Factory(); $codeRender = new FencedCodeRenderer(); $codeRender->addSyntaxHighlighter('php', new PhpHighlighter($highlighterFactory->__invoke())); $blockRenderers = [Document::class => new DocumentRenderer(), Header::class => new HeaderRenderer(), HorizontalRule::class => new HorizontalRuleRenderer(), Paragraph::class => new ParagraphRenderer(), FencedCode::class => $codeRender]; $inlineBlockRenderers = [Text::class => new TextRenderer(), Code::class => new CodeRenderer(), Emphasis::class => new EmphasisRenderer(), Strong::class => new StrongRenderer(), Newline::class => new NewlineRenderer(), Link::class => new LinkRenderer()]; $colors = new Color(); $colors->setForceStyle(true); return new CliRenderer($blockRenderers, $inlineBlockRenderers, $colors); }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $paragraph = new Paragraph(); $paragraph->appendChild(new Text('Some Text 1')); $paragraph->appendChild(new Text('Some Text 2')); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([], [Text::class => new TextRenderer()], $color); $this->assertEquals("Some Text 1Some Text 2\n", $renderer->render($paragraph, $cliRenderer)); }
/** * @return ResultsRenderer */ protected function getRenderer() { if (null === $this->renderer) { $color = new Color(); $color->setForceStyle(true); $terminal = $this->prophesize(TerminalInterface::class); $terminal->getWidth()->willReturn(50); $exerciseRepo = $this->createMock(ExerciseRepository::class); $syntaxHighlighter = (new Factory())->__invoke(); $this->renderer = new ResultsRenderer('appName', $color, $terminal->reveal(), $exerciseRepo, $syntaxHighlighter, $this->getResultRendererFactory()); } return $this->renderer; }
public function testRender() { $class = $this->getRendererClass(); $renderer = new $class(); $list = new ListItem(new ListData()); $paragraph = new Paragraph(); $paragraph->appendChild(new Text('Item 1')); $list->appendChild($paragraph); $color = new Color(); $color->setForceStyle(true); $cliRenderer = new CliRenderer([Paragraph::class => new ParagraphRenderer()], [Text::class => new TextRenderer()], $color); $this->assertEquals("[33m * [0mItem 1\n", $renderer->render($list, $cliRenderer)); }
public function test() { $input = new Input('appName', ['program' => 'solution.php']); $exercise = new CliExerciseImpl(); $repo = new ExerciseRepository([$exercise]); $state = new UserState(); $state->setCurrentExercise('my-exercise'); $color = new Color(); $color->setForceStyle(true); $output = new StdOutput($color, $this->createMock(TerminalInterface::class)); $dispatcher = $this->prophesize(ExerciseDispatcher::class); $dispatcher->run($exercise, $input, $output)->shouldBeCalled(); $command = new RunCommand($repo, $dispatcher->reveal(), $state, $output); $command->__invoke($input); }
public function setUp() { $this->color = new Color(); $this->color->setForceStyle(true); $this->output = new StdOutput($this->color, $this->getMock(TerminalInterface::class)); }
public function testRenderAllFailures() { $color = new Color(); $color->setForceStyle(true); $resultRendererFactory = new ResultRendererFactory(); $resultRendererFactory->registerRenderer(Failure::class, FailureRenderer::class, function (Failure $failure) { $renderer = $this->prophesize(FailureRenderer::class); $renderer->render(Argument::type(ResultsRenderer::class))->willReturn($failure->getReason() . "\n"); return $renderer->reveal(); }); $terminal = $this->prophesize(TerminalInterface::class); $terminal->getWidth()->willReturn(100); $terminal = $terminal->reveal(); $exerciseRepo = $this->prophesize(ExerciseRepository::class); $exerciseRepo->count()->willReturn(2); $renderer = new ResultsRenderer('app', $color, $terminal, $exerciseRepo->reveal(), (new Factory())->__invoke(), $resultRendererFactory); $resultSet = new ResultAggregator(); $resultSet->add(new Failure('Failure 1', 'Failure 1')); $resultSet->add(new Failure('Failure 2', 'Failure 2')); $this->expectOutputString($this->getExpectedOutput()); $renderer->render($resultSet, $this->createMock(ExerciseInterface::class), new UserState(), new StdOutput($color, $terminal)); }
use PhpSchool\PhpWorkshop\MarkdownRenderer; use PhpSchool\PhpWorkshop\ResultRenderer\ResultsRenderer; use PhpSchool\PhpWorkshop\UserState; use PhpSchool\PhpWorkshop\UserStateSerializer; use Symfony\Component\Filesystem\Filesystem; use Faker\Factory as FakerFactory; use Faker\Generator as FakerGenerator; return ['appName' => basename($_SERVER['argv'][0]), WorkshopType::class => WorkshopType::STANDARD(), ExerciseDispatcher::class => function (ContainerInterface $c) { return new ExerciseDispatcher($c->get(RunnerManager::class), $c->get(ResultAggregator::class), $c->get(EventDispatcher::class), $c->get(CheckRepository::class)); }, ResultAggregator::class => object(ResultAggregator::class), CheckRepository::class => function (ContainerInterface $c) { return new CheckRepository([$c->get(FileExistsCheck::class), $c->get(PhpLintCheck::class), $c->get(CodeParseCheck::class), $c->get(ComposerCheck::class), $c->get(FunctionRequirementsCheck::class), $c->get(DatabaseCheck::class)]); }, CommandRouter::class => function (ContainerInterface $c) { return new CommandRouter([new CommandDefinition('menu', [], MenuCommand::class), new CommandDefinition('help', [], HelpCommand::class), new CommandDefinition('print', [], PrintCommand::class), new CommandDefinition('verify', [], VerifyCommand::class), new CommandDefinition('run', [], RunCommand::class), new CommandDefinition('credits', [], CreditsCommand::class)], 'menu', $c->get(EventDispatcher::class), $c); }, Color::class => function () { $colors = new Color(); $colors->setForceStyle(true); return $colors; }, OutputInterface::class => function (ContainerInterface $c) { return new StdOutput($c->get(Color::class), $c->get(TerminalInterface::class)); }, ExerciseRepository::class => function (ContainerInterface $c) { return new ExerciseRepository(array_map(function ($exerciseClass) use($c) { return $c->get($exerciseClass); }, $c->get('exercises'))); }, EventDispatcher::class => factory(EventDispatcherFactory::class), EventDispatcherFactory::class => object(), RunnerManager::class => function (ContainerInterface $c) { $manager = new RunnerManager(); $manager->addFactory(new CliRunnerFactory($c->get(EventDispatcher::class))); $manager->addFactory(new CgiRunnerFactory($c->get(EventDispatcher::class), $c->get(RequestRenderer::class))); $manager->addFactory(new CustomVerifyingRunnerFactory()); return $manager; }, MenuCommand::class => function (ContainerInterface $c) { return new MenuCommand($c->get('menu'));
public function testRunPassesOutputAndReturnsFailureIfARequestFails() { $color = new Color(); $color->setForceStyle(true); $output = new StdOutput($color, $this->createMock(TerminalInterface::class)); $request1 = (new Request())->withMethod('GET')->withUri(new Uri('http://some.site?number=5')); $this->exercise->expects($this->once())->method('getRequests')->will($this->returnValue([$request1])); $exp = "\n[1m[4mRequest"; $exp .= "[0m[0m\n\n"; $exp .= "URL: http://some.site?number=5\n"; $exp .= "METHOD: GET\n"; $exp .= "HEADERS: Host: some.site\n\n"; $exp .= "[1m[4mOutput"; $exp .= "[0m[0m\n\n"; $exp .= "Status: 404 Not Found\r\n"; $exp .= "Content-type: text/html; charset=UTF-8\r\n\r\n"; $exp .= "No input file specified.\n\n"; $exp .= "[33m[0m"; $this->expectOutputString($exp); $success = $this->runner->run('', $output); $this->assertFalse($success); }
public function testRunPassesOutputAndReturnsSuccessIfScriptIsSuccessful() { $color = new Color(); $color->setForceStyle(true); $output = new StdOutput($color, $this->createMock(TerminalInterface::class)); $this->exercise->expects($this->once())->method('getArgs')->will($this->returnValue([[1, 2, 3], [4, 5, 6]])); $exp = "\n[1m[4mArguments[0m[0m\n"; $exp .= "1, 2, 3\n"; $exp .= "\n[1m[4mOutput[0m[0m\n"; $exp .= "6\n"; $exp .= "[33m[0m\n"; $exp .= "[1m[4mArguments[0m[0m\n"; $exp .= "4, 5, 6\n\n"; $exp .= "[1m[4mOutput[0m[0m\n"; $exp .= "15\n"; $exp .= "[33m[0m"; $this->expectOutputString($exp); $success = $this->runner->run(new Input('app', ['program' => __DIR__ . '/../res/cli/user.php']), $output); $this->assertTrue($success); }
public function testRun() { $color = new Color(); $color->setForceStyle(true); $output = new StdOutput($color, $terminal = $this->getMock(TerminalInterface::class)); $outputRegEx = "/\n"; $outputRegEx .= '\\[1m\\[4mArguments\\[0m\\[0m'; $outputRegEx .= "\n"; $outputRegEx .= '127.0.0.1, \\d+'; $outputRegEx .= "\n\n"; $outputRegEx .= '\\[1m\\[4mOutput\\[0m\\[0m'; $outputRegEx .= "\n"; $outputRegEx .= '\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}'; $outputRegEx .= "\n/"; $this->expectOutputRegex($outputRegEx); $input = new Input('learnyouphp', ['program' => __DIR__ . '/../res/time-server/solution.php']); $this->exerciseDispatcher->run($this->exercise, $input, $output); }