Пример #1
0
 public function testCreateConvertsParserErrorToComment()
 {
     $file = File::create('missing_semicolon.php', '<?php echo "foo"');
     $this->assertInstanceOf('PHPParser_Node', $file->getAst());
     $comments = $file->getComments(1);
     $this->assertSame(1, count($comments));
     $this->assertContains('This code did not parse for me. Apparently, there is an error somewhere around this line', (string) $comments[0]);
 }
 /**
  * @dataProvider getTests
  */
 public function testFixingFiles($testFile)
 {
     $testData = $this->parseTestCase(file_get_contents($testFile));
     $this->file = \Scrutinizer\PhpAnalyzer\Model\File::create('test.php', $testData['before']);
     $this->analyzer->setConfigurationValues($testData['config']);
     $this->analyzer->analyze(new \Scrutinizer\PhpAnalyzer\Model\FileCollection(array($this->file)));
     $this->assertTrue($this->file->hasFixedFile(), 'File has no fixed file.');
     $this->assertEquals($testData['after'], $this->file->getFixedFile()->getContent());
 }
 /**
  * @dataProvider getTests
  */
 public function testAnalyze($filename)
 {
     $test = $this->parseTestCase($filename);
     $testName = basename($filename);
     $phpFile = File::create($test['filename'], $test['code']);
     if (!empty($test['diff'])) {
         $phpFile->setDiff($test['diff']);
     }
     $expectedComments = $test['comments'];
     $config = $test['config'];
     $analyzer = $this->getAnalyzer();
     $analyzer->setConfigurationValues($config);
     $analyzer->analyze(new FileCollection(array($phpFile)));
     $actualComments = $phpFile->getComments();
     foreach ($expectedComments as $line => $comments) {
         $this->assertTrue(isset($actualComments[$line]), sprintf('Expected comments for line "%d", but got none in test "%s". Comments: %s', $line, $testName, var_export($actualComments, true)));
         $this->assertSame(count($comments), count($actualComments[$line]), sprintf('Expected "%d" comments for line "%d", but got "%d" comments. Comments: %s', count($comments), $line, count($actualComments[$line]), var_export($actualComments, true)));
         foreach ($comments as $comment) {
             $found = false;
             foreach ($actualComments[$line] as $actualComment) {
                 if (false !== strpos((string) $actualComment, $comment)) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 $availableComments = "\n";
                 foreach ($actualComments[$line] as $aComment) {
                     $availableComments .= $aComment . "\n";
                 }
                 $this->fail(sprintf('Expected comment "%s" on line "%d", but did not find it. Available Comments: %s', $comment, $line, $availableComments));
             }
         }
     }
     foreach ($actualComments as $line => $actualComment) {
         $this->assertTrue(isset($expectedComments[$line]), sprintf('Found comment "%s" on line "%d", but did not expect it.', (string) $actualComment[0], $line));
     }
     if (null !== $test['after']) {
         $this->assertTrue($phpFile->hasFixedFile());
         $this->assertEquals($test['after'], $phpFile->getFixedFile()->getContent());
     }
 }
 public function getCodingStyleTests()
 {
     $testDir = realpath($this->getTestDir());
     try {
         $tests = array();
         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($testDir)) as $file) {
             $test = $this->parseTestCase(file_get_contents($file->getRealpath()));
             $phpFile = File::create($file->getFilename() . '.php', $test['code']);
             if (!empty($test['diff'])) {
                 $phpFile->setDiff($test['diff']);
             }
             $tests[] = array($file->getFilename(), $phpFile, $test['comments']);
         }
     } catch (\Exception $ex) {
         echo "Could not load test cases\n";
         echo $ex->getMessage();
         exit;
     }
     return $tests;
 }
 public static function createFromTraversable(\Traversable $traversable, $basePath = '')
 {
     $prefixLength = strlen($basePath);
     $col = new self();
     $i = 0;
     $memoryThreshold = self::getMemoryThreshold();
     foreach ($traversable as $file) {
         assert($file instanceof SplFileInfo);
         if ($file->isLink()) {
             continue;
         }
         $col->add(File::create(substr($file->getRealPath(), $prefixLength), $file->getContents()));
         if ($i % 50 === 0 && $memoryThreshold < ($memoryUsage = memory_get_usage())) {
             throw MaxMemoryExceededException::create($memoryUsage, $memoryThreshold);
         }
         $i += 1;
     }
     if ($memoryThreshold < ($memoryUsage = memory_get_usage())) {
         throw MaxMemoryExceededException::create($memoryUsage, $memoryThreshold);
     }
     return $col;
 }