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); }
/** * @param CliMenu $menu */ public function __invoke(CliMenu $menu) { $menu->close(); $item = $menu->getSelectedItem(); $exercise = $this->exerciseRepository->findByName($item->getText()); $exercises = $this->exerciseRepository->findAll(); $this->userState->setCurrentExercise($item->getText()); $this->userStateSerializer->serialize($this->userState); $numExercises = count($exercises); $exerciseIndex = array_search($exercise, $exercises) + 1; $output = "\n"; $output .= $this->color->__invoke(' LEARN YOU THE PHP FOR MUCH WIN! ')->magenta()->bold() . "\n"; $output .= $this->color->__invoke('*********************************')->magenta()->bold() . "\n"; $output .= "\n"; $output .= $this->color->__invoke(" " . $exercise->getName())->yellow()->bold() . "\n"; $output .= $this->color->__invoke(sprintf(" Exercise %d of %d\n\n", $exerciseIndex, $numExercises))->yellow(); $content = file_get_contents($exercise->getProblem()); $doc = $this->markdownRenderer->render($content); $doc = str_replace('{appname}', $this->appName, $doc); $output .= $doc; $output .= "\n"; $output .= $this->helpLine('To print these instructions again, run', 'print'); $output .= $this->helpLine('To execute your program in a test environment, run', 'run program.php'); $output .= $this->helpLine('To verify your program, run', 'verify program.php'); $output .= $this->helpLine('For help run', 'help'); $output .= "\n\n"; $this->output->write($output); }
/** * @return int|void */ public function __invoke() { $currentExercise = $this->userState->getCurrentExercise(); $exercise = $this->exerciseRepository->findByName($currentExercise); $markDown = file_get_contents($exercise->getProblem()); $doc = $this->markdownRenderer->render($markDown); $doc = str_replace('{appname}', $this->appName, $doc); $this->output->write($doc); }
/** * @param Event $event */ public function __invoke(Event $event) { /** @var CommandDefinition $command */ $command = $event->getParameter('command'); if (!in_array($command->getName(), ['verify', 'run'])) { return; } $currentExercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $this->runnerManager->configureInput($currentExercise, $command); }
/** * @param Event $event */ public function __invoke(Event $event) { /** @var CommandDefinition $command */ $command = $event->getParameter('command'); if (!in_array($command->getName(), ['verify', 'run', 'print'])) { return; } if (!$this->userState->isAssignedExercise()) { throw new \RuntimeException('No active exercise. Select one from the menu'); } }
/** * @param Input $input The command line arguments passed to the command. * * @return int|void */ public function __invoke(Input $input) { $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $results = $this->exerciseDispatcher->verify($exercise, $input); if ($results->isSuccessful()) { $this->userState->addCompletedExercise($exercise->getName()); $this->userStateSerializer->serialize($this->userState); } $this->resultsRenderer->render($results, $exercise, $this->userState, $this->output); return $results->isSuccessful() ? 0 : 1; }
/** * @return int|void */ public function __invoke() { if (!$this->userState->isAssignedExercise()) { $this->output->printError("No active exercises. Select one from the menu"); return 1; } $currentExercise = $this->userState->getCurrentExercise(); $exercise = $this->exerciseRepository->findByName($currentExercise); $markDown = file_get_contents($exercise->getProblem()); $doc = $this->markdownRenderer->render($markDown); $doc = str_replace('{appname}', $this->appName, $doc); $this->output->write($doc); }
/** * @param ExerciseInterface $exercise * @param UserState $userState * @param WorkshopType $type * @return bool */ private function isExerciseDisabled(ExerciseInterface $exercise, UserState $userState, WorkshopType $type) { static $previous = null; if (null === $previous || !$type->isTutorialMode()) { $previous = $exercise; return false; } if (in_array($previous->getName(), $userState->getCompletedExercises())) { $previous = $exercise; return false; } $previous = $exercise; return true; }
/** * @param string $appName * @param string $program * * @return int|void */ public function __invoke($appName, $program) { if (!file_exists($program)) { $this->output->printError(sprintf('Could not run. File: "%s" does not exist', $program)); return 1; } $program = realpath($program); if (!$this->userState->isAssignedExercise()) { $this->output->printError("No active exercises. Select one from the menu"); return 1; } $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $this->exerciseDispatcher->run($exercise, $program, $this->output); }
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 testExerciseIsPrintedIfAssigned() { $file = tempnam(sys_get_temp_dir(), 'pws'); file_put_contents($file, '### Exercise 1'); $exercise = $this->createMock(ExerciseInterface::class); $exercise->expects($this->once())->method('getProblem')->will($this->returnValue($file)); $exercise->expects($this->once())->method('getName')->will($this->returnValue('current-exercise')); $repo = new ExerciseRepository([$exercise]); $state = new UserState(); $state->setCurrentExercise('current-exercise'); $output = $this->createMock(OutputInterface::class); $renderer = $this->createMock(MarkdownRenderer::class); $renderer->expects($this->once())->method('render')->with('### Exercise 1')->will($this->returnValue('### Exercise 1')); $output->expects($this->once())->method('write')->with('### Exercise 1'); $command = new PrintCommand('phpschool', $repo, $state, $renderer, $output); $command->__invoke(); unlink($file); }
/** * @param string $appName * @param string $program * * @return int|void */ public function __invoke($appName, $program) { if (!file_exists($program)) { $this->output->printError(sprintf('Could not verify. File: "%s" does not exist', $program)); return 1; } $program = realpath($program); if (!$this->userState->isAssignedExercise()) { $this->output->printError("No active exercises. Select one from the menu"); return 1; } $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $results = $this->exerciseDispatcher->verify($exercise, $program); if ($results->isSuccessful()) { $this->userState->addCompletedExercise($exercise->getName()); $this->userStateSerializer->serialize($this->userState); } $this->resultsRenderer->render($results, $exercise, $this->userState, $this->output); return $results->isSuccessful() ? 0 : 1; }
public function testCompletedExercise() { $state = new UserState(['exercise1']); $this->assertTrue($state->completedExercise('exercise1')); }
/** * @param ExerciseInterface $exercise * @param UserState $userState * @param OutputInterface $output */ private function renderSuccessInformation(ExerciseInterface $exercise, UserState $userState, OutputInterface $output) { $output->emptyLine(); $output->writeLine($this->style(" PASS!", ['green', 'bg_default', 'bold'])); $output->emptyLine(); $output->writeLine("Here's the official solution in case you want to compare notes:"); $output->writeLine($this->lineBreak()); foreach ($exercise->getSolution()->getFiles() as $file) { $output->writeLine($this->style($file->getRelativePath(), ['bold', 'cyan', 'underline'])); $output->emptyLine(); $code = $file->getContents(); if (pathinfo($file->getRelativePath(), PATHINFO_EXTENSION) === 'php') { $code = $this->syntaxHighlighter->highlight($code); } //make sure there is a new line at the end $code = preg_replace('/\\n$/', '', $code) . "\n"; $output->write($code); $output->writeLine($this->lineBreak()); } $completedCount = count($userState->getCompletedExercises()); $numExercises = $this->exerciseRepository->count(); $output->writeLine(sprintf('You have %d challenges left.', $numExercises - $completedCount)); $output->writeLine(sprintf('Type "%s" to show the menu.', $this->appName)); $output->emptyLine(); }
/** * @param Input $input The command line arguments passed to the command. * * @return int|void */ public function __invoke(Input $input) { $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $this->exerciseDispatcher->run($exercise, $input, $this->output); }