Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
 public function testWithNoContributors()
 {
     $this->expectOutputString('');
     $color = new Color();
     $color->setForceStyle(true);
     $command = new CreditsCommand([], [], new StdOutput($color, $this->getMock(TerminalInterface::class)), $color);
     $command->__invoke();
 }
Ejemplo n.º 4
0
 /**
  * @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)));
 }
Ejemplo n.º 5
0
 /**
  * @param string $string
  * @param string $colour
  *
  * @return string
  */
 public function colour($string, $colour)
 {
     try {
         return $this->color->__invoke($string)->apply($colour)->__toString();
     } catch (NoStyleFoundException $e) {
         return $string;
     }
 }
Ejemplo n.º 6
0
 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();
 }
Ejemplo n.º 7
0
 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("──────────", $renderer->lineBreak());
 }
Ejemplo n.º 8
0
 public function error()
 {
     if ($this->color) {
         $this->writer->write($this->color->apply('bg_red', 'X'));
         $this->progress();
     } else {
         parent::error();
     }
 }
 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("SOME CODE", $renderer->render($code, $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 testRender()
 {
     $class = $this->getRendererClass();
     $renderer = new $class();
     $rule = new HorizontalRule();
     $color = new Color();
     $color->setForceStyle(true);
     $cliRenderer = new CliRenderer([], [], $color);
     $this->assertEquals("------------------------------", $renderer->render($rule, $cliRenderer));
 }
 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 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);
 }
 /**
  * @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());
 }
Ejemplo n.º 15
0
 /**
  * @return void
  */
 public function __invoke()
 {
     $this->output->writeLine($this->color->__invoke('Usage')->yellow()->bold());
     $this->output->writeLine("");
     $this->output->writeLine(sprintf("  %s", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Show a menu to interactively select a workshop.");
     $this->output->writeLine(sprintf("  %s print", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Print the instructions for the currently selected workshop.");
     $this->output->writeLine(sprintf("  %s verify program.php", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Verify your program against the expected output.");
     $this->output->writeLine(sprintf("  %s help", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Show this help.");
     $this->output->writeLine(sprintf("  %s credits", $this->color->__invoke($this->appName)->green()));
     $this->output->writeLine("    Show the people who made this happen.");
     $this->output->writeLine("");
     $this->output->writeLine($this->color->__invoke('Having trouble with a PHPSchool exercise?')->yellow()->bold());
     $this->output->writeLine("");
     $this->output->writeLine("  A team of expert helper elves is eagerly waiting to assist you in");
     $this->output->writeLine("  mastering the basics of PHP, simply go to:");
     $this->output->writeLine("    https://github.com/php-school/discussions");
     $this->output->writeLine("  and add a New Issue and let us know what you're having trouble");
     $this->output->writeLine("  with. There are no dumb questions!");
     $this->output->writeLine("");
     $this->output->writeLine("  If you're looking for general help with PHP, the #php");
     $this->output->writeLine("  channel on Freenode IRC is usually a great place to find someone");
     $this->output->writeLine("  willing to help. There is also the PHP StackOverflow Chat:");
     $this->output->writeLine("    https://chat.stackoverflow.com/rooms/11/php");
     $this->output->writeLine("");
     $this->output->writeLine($this->color->__invoke('Found a bug with PHPSchool or just want to contribute?')->yellow()->bold());
     $this->output->writeLine("  The official repository for PHPSchool is:");
     $this->output->writeLine("    https://github.com/php-school/php-workshop");
     $this->output->writeLine("  Feel free to file a bug report or (preferably) a pull request.");
     $this->output->writeLine("");
 }
 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("http://www.google.com", $renderer->render($link, $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));
 }
 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## HEADING!!\n", $renderer->render($header, $cliRenderer));
 }
Ejemplo n.º 19
0
 /**
  * @param string $string
  * @param array|string $colourOrStyle
  *
  * @return string
  *
  */
 public function style($string, $colourOrStyle)
 {
     if (is_array($colourOrStyle)) {
         $this->color->__invoke($string);
         while ($style = array_shift($colourOrStyle)) {
             $this->color->apply($style);
         }
         return $this->color->__toString();
     }
     return $this->color->__invoke($string)->apply($colourOrStyle, $string);
 }
Ejemplo n.º 20
0
 public function testGivenInvalidUserStyleNameShouldThrowAnException()
 {
     $color = new Color();
     try {
         $color->setUserStyles(array('foo-bar' => 'red'));
         $this->fail('must throw an InvalidArgumentException');
     } catch (InvalidStyleNameException $e) {
         assertInstanceOf('InvalidArgumentException', $e);
         assertSame('foo-bar is not a valid style name', $e->getMessage());
     }
 }
 /**
  * @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();
     $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("Some Text", $renderer->render($emphasis, $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('    console.log("lol js???")', $renderer->render($code, $cliRenderer));
 }
 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(" * Item 1\n", $renderer->render($list, $cliRenderer));
 }
Ejemplo n.º 27
0
 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);
 }
 /**
  * @return int|void
  */
 public function __invoke()
 {
     if (empty($this->coreContributors)) {
         return;
     }
     $this->output->writeLine($this->color->__invoke("PHP School is bought to you by...")->yellow()->__toString());
     $this->output->emptyLine();
     $this->writeContributors($this->coreContributors);
     if (empty($this->appContributors)) {
         return;
     }
     $this->output->emptyLine();
     $this->output->emptyLine();
     $this->output->writeLine($this->color->__invoke("This workshop is brought to you by...")->yellow()->__toString());
     $this->output->writeLine("");
     $this->writeContributors($this->appContributors);
 }
 public function format(array $record)
 {
     $record['padded_level'] = str_pad($record['level_name'], 8);
     $record['message'] = str_replace("\n", "\n         ", $record['message']);
     $output = parent::format($record);
     if (dbsteward::$ENABLE_COLOR) {
         $c = new Color($output);
         switch ($record['level']) {
             case Logger::DEBUG:
                 $c->dark_gray();
                 break;
             case Logger::WARNING:
                 $c->yellow();
                 break;
             case Logger::ERROR:
                 $c->red();
                 break;
         }
         return $c . PHP_EOL;
     } else {
         return $output . PHP_EOL;
     }
 }
Ejemplo n.º 30
0
 /**
  * Shorthand for sending output to stdout and appending to log buffer at the same time.
  *
  * @param string $output
  * @param string $eol
  */
 public function say($output = '', $eol = PHP_EOL)
 {
     if (is_array($output)) {
         $output = join(',', $output);
     }
     $this->logOutput .= $output . $eol;
     switch ($output) {
         case 'SUCCESS':
             $output = $this->color->green($output);
             $output = $this->color->bold($output);
             break;
         case 'NOT FOUND':
         case 'INVALID URL':
             $output = $this->color->red($output);
             $output = $this->color->bold($output);
             break;
         default:
             break;
     }
     echo $output . $eol;
 }