public function testListenWithMultipleEvents() { $e1 = new Event('some-event', ['arg1' => 1, 'arg2' => 2]); $e2 = new Event('some-event', ['arg1' => 1, 'arg2' => 2]); $mockCallback1 = $this->getMock('stdClass', ['callback']); $mockCallback1->expects($this->exactly(2))->method('callback')->withConsecutive([$e1], [$e2])->will($this->returnValue(true)); $this->eventDispatcher->listen(['some-event', 'second-event'], [$mockCallback1, 'callback']); $this->eventDispatcher->dispatch($e1); $this->eventDispatcher->dispatch($e2); }
/** * Runs a student's solution by invoking PHP via the `php-cgi` binary, populating all the super globals with * the information from the request objects returned from the exercise. The exercise can return multiple * requests so the solution will be invoked for however many requests there are. * * Running only runs the student's solution, the reference solution is not run and no verification is performed, * the output of the student's solution is written directly to the output. * * Events dispatched (for each request): * * * cgi.run.student-execute.pre * * cgi.run.student.executing * * @param Input $input The command line arguments passed to the command. * @param OutputInterface $output A wrapper around STDOUT. * @return bool If the solution was successfully executed, eg. exit code was 0. */ public function run(Input $input, OutputInterface $output) { $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.start', $this->exercise, $input)); $success = true; foreach ($this->exercise->getRequests() as $i => $request) { $event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student-execute.pre', $request)); $process = $this->getProcess($input->getArgument('program'), $event->getRequest()); $output->writeTitle("Request"); $output->emptyLine(); $output->write($this->requestRenderer->renderRequest($request)); $output->writeTitle("Output"); $output->emptyLine(); $process->start(); $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student.executing', $request, ['output' => $output])); $process->wait(function ($outputType, $outputBuffer) use($output) { $output->write($outputBuffer); }); $output->emptyLine(); if (!$process->isSuccessful()) { $success = false; } $output->lineBreak(); } $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cgi.run.finish', $this->exercise, $input)); return $success; }
/** * Runs a student's solution by invoking PHP from the CLI passing the arguments gathered from the exercise * as command line arguments to PHP. * * Running only runs the student's solution, the reference solution is not run and no verification is performed, * the output of the student's solution is written directly to the output. * * Events dispatched: * * * cli.run.student-execute.pre * * cli.run.student.executing * * @param Input $input The command line arguments passed to the command. * @param OutputInterface $output A wrapper around STDOUT. * @return bool If the solution was successfully executed, eg. exit code was 0. */ public function run(Input $input, OutputInterface $output) { $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.start', $this->exercise, $input)); $success = true; foreach ($this->preserveOldArgFormat($this->exercise->getArgs()) as $i => $args) { /** @var CliExecuteEvent $event */ $event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.student-execute.pre', new ArrayObject($args))); $args = $event->getArgs(); if (count($args)) { $glue = max(array_map('strlen', $args->getArrayCopy())) > 30 ? "\n" : ', '; $output->writeTitle('Arguments'); $output->write(implode($glue, $args->getArrayCopy())); $output->emptyLine(); } $output->writeTitle("Output"); $process = $this->getPhpProcess($input->getArgument('program'), $args); $process->start(); $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.student.executing', $args, ['output' => $output])); $process->wait(function ($outputType, $outputBuffer) use($output) { $output->write($outputBuffer); }); $output->emptyLine(); if (!$process->isSuccessful()) { $success = false; } $output->lineBreak(); } $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('cli.run.finish', $this->exercise, $input)); return $success; }
/** * @param ExerciseInterface $exercise * @param string $fileName * @param OutputInterface $output * @return bool */ public function run(ExerciseInterface $exercise, $fileName, OutputInterface $output) { $exercise->configure($this); $this->eventDispatcher->dispatch(new Event('run.start', compact('exercise', 'fileName'))); try { $exitStatus = $this->runnerFactory->create($exercise, $this->eventDispatcher)->run($fileName, $output); } finally { $this->eventDispatcher->dispatch(new Event('run.finish', compact('exercise', 'fileName'))); } return $exitStatus; }
/** * Run a student's solution against a specific exercise. Does not invoke checks. Invokes the * correct runner for the exercise based on the exercise type. Various events are triggered throughout the process. * The output of the solution is written directly to the `OutputInterface` instance. * * @param ExerciseInterface $exercise The exercise instance. * @param Input $input The command line arguments passed to the command. * @param OutputInterface $output An output instance capable of writing to stdout. * @return bool Whether the solution ran successfully or not. */ public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output) { $exercise->configure($this); $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $exercise, $input)); try { $exitStatus = $this->runnerManager->getRunner($exercise)->run($input, $output); } finally { $this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.finish', $exercise, $input)); } return $exitStatus; }
public function testListenersAndVerifiersAreCalledInOrderOfAttachment() { $e1 = new Event('first-event', ['arg1' => 1, 'arg2' => 2]); $counter = 0; $this->eventDispatcher->insertVerifier('first-event', function (Event $e) use(&$counter) { $this->assertEquals(0, $counter); $counter++; }); $this->eventDispatcher->listen('first-event', function (Event $e) use(&$counter) { $this->assertEquals(1, $counter); $counter++; }); $this->eventDispatcher->dispatch($e1); }
/** * @param string $fileName * @param OutputInterface $output * @return bool */ public function run($fileName, OutputInterface $output) { /** @var CliExecuteEvent $event */ $event = $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.user-execute.pre', new ArrayObject($this->exercise->getArgs()))); $args = $event->getArgs(); if (count($args)) { $glue = max(array_map('strlen', $args->getArrayCopy())) > 30 ? "\n" : ', '; $output->writeTitle('Arguments'); $output->write(implode($glue, $args->getArrayCopy())); $output->emptyLine(); } $output->writeTitle("Output"); $process = $this->getPhpProcess($fileName, $args); $process->start(); $this->eventDispatcher->dispatch(new CliExecuteEvent('cli.run.executing', $args, ['output' => $output])); $process->wait(function ($outputType, $outputBuffer) use($output) { $output->writeLine($outputBuffer); }); return $process->isSuccessful(); }
/** * Runs a student's solution by invoking PHP via the `php-cgi` binary, populating all the super globals with * the information from the request objects returned from the exercise. The exercise can return multiple * requests so the solution will be invoked for however many requests there are. * * Running only runs the student's solution, the reference solution is not run and no verification is performed, * the output of the student's solution is written directly to the output. * * Events dispatched (for each request): * * * cgi.run.student-execute.pre * * cgi.run.student.executing * * @param string $fileName The absolute path to the student's solution. * @param OutputInterface $output A wrapper around STDOUT. * @return bool If the solution was successfully executed, eg. exit code was 0. */ public function run($fileName, OutputInterface $output) { $success = true; foreach ($this->exercise->getRequests() as $i => $request) { $event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student-execute.pre', $request)); $process = $this->getProcess($fileName, $event->getRequest()); $output->writeTitle("Request"); $output->emptyLine(); $output->writeRequest($request); $output->writeTitle("Output"); $output->emptyLine(); $process->start(); $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.run.student.executing', $request, ['output' => $output])); $process->wait(function ($outputType, $outputBuffer) use($output) { $output->write($outputBuffer); }); $output->emptyLine(); if (!$process->isSuccessful()) { $success = false; } $output->lineBreak(); } return $success; }
/** * @param EventDispatcher $eventDispatcher * @param ExerciseInterface $exercise */ private function dispatchExerciseSelectedEvent(EventDispatcher $eventDispatcher, ExerciseInterface $exercise) { $eventDispatcher->dispatch(new Event(sprintf('exercise.selected.%s', AbstractExercise::normaliseName($exercise->getName())))); }
/** * @param CommandDefinition $command * @param callable $callable * @param Input $input * @return int */ private function callCommand(CommandDefinition $command, callable $callable, Input $input) { $this->eventDispatcher->dispatch(new Event\Event('route.pre.invoke')); $this->eventDispatcher->dispatch(new Event\Event(sprintf('route.pre.invoke.%s', $command->getName()))); return $callable($input); }