public function testErrorOnParseException() { $file = m::mock('\\Psecio\\Parse\\File'); $file->shouldReceive('isPathMatch')->once()->with('/\\.phps$/i')->andReturn(false); $file->shouldReceive('getContents')->once()->andReturn(''); $dispatcher = $this->createErrorDispatcherMock(); $scanner = new Scanner($dispatcher, m::mock('\\Psecio\\Parse\\CallbackVisitor')->shouldReceive('onNodeFailure', 'setFile')->mock(), m::mock('\\PhpParser\\Parser')->shouldReceive('parse')->andThrow(new \PhpParser\Error(''))->mock(), m::mock('\\PhpParser\\NodeTraverser')->shouldReceive('addVisitor')->mock()); $scanner->scan(m::mock('\\Psecio\\Parse\\FileIterator')->shouldReceive('getIterator')->andReturn(new \ArrayIterator([$file]))->mock()); }
/** * Execute the "scan" command * * @param InputInterface $input Input object * @param OutputInterface $output Output object * @throws RuntimeException If output format is not valid * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { $dispatcher = new EventDispatcher(); $exitCode = new ExitCodeCatcher(); $dispatcher->addSubscriber($exitCode); $fileIterator = new FileIterator($input->getArgument('path'), $this->parseCsv($input->getOption('ignore-paths')), $this->parseCsv($input->getOption('extensions'))); $format = strtolower($input->getOption('format')); switch ($format) { case 'dots': case 'progress': $output->writeln("<info>Parse: A PHP Security Scanner</info>\n"); if ($output->isVeryVerbose()) { $dispatcher->addSubscriber(new ConsoleDebug($output)); } elseif ($output->isVerbose()) { $dispatcher->addSubscriber(new ConsoleLines($output)); } elseif ('progress' == $format && $output->isDecorated()) { $dispatcher->addSubscriber(new ConsoleProgressBar(new ProgressBar($output, count($fileIterator)))); } else { $dispatcher->addSubscriber(new ConsoleDots($output)); } $dispatcher->addSubscriber(new ConsoleReport($output)); break; case 'xml': $dispatcher->addSubscriber(new Xml($output)); break; default: throw new RuntimeException("Unknown output format '{$input->getOption('format')}'"); } $ruleFactory = new RuleFactory($this->parseCsv($input->getOption('whitelist-rules')), $this->parseCsv($input->getOption('blacklist-rules'))); $ruleCollection = $ruleFactory->createRuleCollection(); $ruleNames = implode(',', array_map(function (RuleInterface $rule) { return $rule->getName(); }, $ruleCollection->toArray())); $dispatcher->dispatch(Events::DEBUG, new MessageEvent("Using ruleset {$ruleNames}")); $docCommentFactory = new DocCommentFactory(); $scanner = new Scanner($dispatcher, new CallbackVisitor($ruleCollection, $docCommentFactory, !$input->getOption('disable-annotations'))); $scanner->scan($fileIterator); return $exitCode->getExitCode(); }