コード例 #1
0
 /**
  * @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;
 }
コード例 #2
0
 public function testSuccessIsReturnedIfOutputIsCorrect()
 {
     $results = $this->exerciseDispatcher->verify($this->exercise, __DIR__ . '/../res/time-server/solution.php');
     $this->assertCount(2, $results);
     $success = iterator_to_array($results)[0];
     $this->assertInstanceOf(Success::class, $success);
 }
コード例 #3
0
 public function testVerifyPostExecuteIsStillDispatchedEvenIfRunnerThrowsException()
 {
     $this->eventDispatcher->expects($this->exactly(3))->method('dispatch')->withConsecutive([$this->isInstanceOf(Event::class)], [$this->isInstanceOf(Event::class)], [$this->isInstanceOf(Event::class)]);
     $this->createExercise();
     $this->mockRunner();
     $this->runner->expects($this->once())->method('verify')->with($this->file)->will($this->throwException(new RuntimeException()));
     $this->setExpectedException(RuntimeException::class);
     $this->exerciseDispatcher->verify($this->exercise, $this->file);
 }
コード例 #4
0
ファイル: VerifyCommand.php プロジェクト: jacmoe/php-workshop
 /**
  * @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;
 }
コード例 #5
0
 public function testAlteringDatabaseInSolutionDoesNotEffectDatabaseInUserSolution()
 {
     $solution = SingleFileSolution::fromFile(realpath(__DIR__ . '/../res/database/solution-alter-db.php'));
     $this->exercise->expects($this->once())->method('getSolution')->will($this->returnValue($solution));
     $this->exercise->expects($this->any())->method('getArgs')->will($this->returnValue([]));
     $this->exercise->expects($this->atLeastOnce())->method('getType')->will($this->returnValue(ExerciseType::CLI()));
     $this->exercise->expects($this->once())->method('configure')->will($this->returnCallback(function (ExerciseDispatcher $dispatcher) {
         $dispatcher->requireCheck(DatabaseCheck::class);
     }));
     $this->exercise->expects($this->once())->method('seed')->with($this->isInstanceOf(PDO::class))->will($this->returnCallback(function (PDO $db) {
         $db->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT)');
         $stmt = $db->prepare('INSERT into users (name, age, gender) VALUES (:name, :age, :gender)');
         $stmt->execute([':name' => 'Jimi Hendrix', ':age' => 27, ':gender' => 'Male']);
     }));
     $this->exercise->expects($this->once())->method('verify')->with($this->isInstanceOf(PDO::class))->will($this->returnCallback(function (PDO $db) {
         $users = $db->query('SELECT * FROM users');
         $users = $users->fetchAll(PDO::FETCH_ASSOC);
         $this->assertEquals([['id' => 1, 'name' => 'Jimi Hendrix', 'age' => '27', 'gender' => 'Male'], ['id' => 2, 'name' => 'Kurt Cobain', 'age' => '27', 'gender' => 'Male']], $users);
     }));
     $results = new ResultAggregator();
     $eventDispatcher = new EventDispatcher($results);
     $checkRepository = new CheckRepository([$this->check]);
     $dispatcher = new ExerciseDispatcher(new RunnerFactory(), $results, $eventDispatcher, $checkRepository);
     $dispatcher->verify($this->exercise, __DIR__ . '/../res/database/user-solution-alter-db.php');
 }
コード例 #6
0
 public function testVerifyPostExecuteIsStillDispatchedEvenIfRunnerThrowsException()
 {
     $input = new Input('app', ['program' => $this->file]);
     $exercise = new CliExerciseImpl('Some Exercise');
     $eventDispatcher = $this->prophesize(EventDispatcher::class);
     $eventDispatcher->dispatch(Argument::that(function ($event) {
         return $event instanceof EventInterface && $event->getName() === 'verify.start';
     }))->shouldBeCalled();
     $eventDispatcher->dispatch(Argument::that(function ($event) {
         return $event instanceof EventInterface && $event->getName() === 'verify.pre.execute';
     }))->shouldBeCalled();
     $eventDispatcher->dispatch(Argument::that(function ($event) {
         return $event instanceof EventInterface && $event->getName() === 'verify.post.execute';
     }))->shouldBeCalled();
     $runner = $this->prophesize(ExerciseRunnerInterface::class);
     $runner->getRequiredChecks()->willReturn([]);
     $runner->verify($input)->willThrow(new RuntimeException());
     $runnerManager = $this->prophesize(RunnerManager::class);
     $runnerManager->getRunner($exercise)->willReturn($runner->reveal());
     $exerciseDispatcher = new ExerciseDispatcher($runnerManager->reveal(), new ResultAggregator(), $eventDispatcher->reveal(), new CheckRepository());
     $this->expectException(RuntimeException::class);
     $exerciseDispatcher->verify($exercise, $input);
 }