/**
  * @dataProvider getLibraries
  */
 public function testLibrary($zipFile)
 {
     $libName = basename($zipFile, '.zip');
     $config = array();
     if (is_file($configFile = __DIR__ . '/libraries/' . $libName . '-config.json')) {
         $config = json_decode(file_get_contents($configFile), true);
     }
     $files = FileCollection::createFromZipFile($zipFile, null, isset($config['filter']) ? $config['filter'] : array());
     $this->analyzer->analyze($files);
     $actualComments = $this->dumpComments($files);
     if (!is_file($commentFile = __DIR__ . '/libraries/' . $libName . '-comments.txt')) {
         file_put_contents($commentFile . '.tmp', $actualComments);
         $this->fail(sprintf('The comment file "%s" was not found.', $commentFile));
     }
     $this->assertEquals(file_get_contents($commentFile), $actualComments);
     $nonExistentFiles = array();
     foreach ($files as $file) {
         $fixedFilePath = __DIR__ . '/libraries/' . $libName . '-' . $file->getName();
         if (!$file->hasFixedFileWithChanges()) {
             $this->assertFileNotExists($fixedFilePath . '.diff');
             continue;
         }
         $diff = \Scrutinizer\Util\DiffUtils::generate($file->getContent(), $file->getFixedFile()->getContent());
         if (!is_file($fixedFilePath . '.diff')) {
             if (!is_dir($dir = dirname($fixedFilePath))) {
                 mkdir($dir, 0777, true);
             }
             file_put_contents($fixedFilePath . '.tmp.diff', $diff);
             $nonExistentFiles[] = $fixedFilePath . '.tmp.diff';
             continue;
         }
         $this->assertEquals(file_get_contents($fixedFilePath . '.diff'), $diff);
     }
     $this->assertCount(0, $nonExistentFiles, "Some files are missing:\n" . implode("\n", $nonExistentFiles));
 }
Ejemplo n.º 2
0
 public function testDiff()
 {
     $diffs = DiffUtils::parseDiffs(file_get_contents(__DIR__ . '/Fixture/one_modified_one_removed.diff'));
     $this->assertCount(3, $diffs);
     $this->assertEquals('src/CheckAccessControlPass.php', $diffs[0]['a_path']);
     $this->assertEquals('src/CheckAccessControlPass.php', $diffs[0]['b_path']);
     $this->assertEquals('a5c01ca', $diffs[0]['a_sha']);
     $this->assertEquals('f781f8f', $diffs[0]['b_sha']);
     $this->assertNull($diffs[0]['a_mode']);
     $this->assertEquals('100644', $diffs[0]['b_mode']);
     $this->assertFalse($diffs[0]['is_new']);
     $this->assertFalse($diffs[0]['is_deleted']);
     $this->assertFalse($diffs[0]['is_renamed']);
     $this->assertNull($diffs[0]['sim_index']);
     $this->assertStringStartsWith('@@', $diffs[0]['diff']);
     $this->assertEquals('src/PhpParser/Scope/Scope.php', $diffs[1]['a_path']);
     $this->assertEquals('src/PhpParser/Scope/Scope.php', $diffs[1]['b_path']);
     $this->assertEquals('0110ef0', $diffs[1]['a_sha']);
     $this->assertEquals('facb0c0', $diffs[1]['b_sha']);
     $this->assertNull($diffs[1]['a_mode']);
     $this->assertEquals('100644', $diffs[1]['b_mode']);
     $this->assertFalse($diffs[1]['is_new']);
     $this->assertFalse($diffs[1]['is_deleted']);
     $this->assertFalse($diffs[1]['is_renamed']);
     $this->assertStringStartsWith('@@', $diffs[0]['diff']);
     $this->assertNull($diffs[1]['sim_index']);
     $this->assertEquals('tests/Fixture/AccessControl/call_from_non_object_context.test', $diffs[2]['a_path']);
     $this->assertEquals('tests/Fixture/AccessControl/call_from_non_object_context.test', $diffs[2]['b_path']);
     $this->assertEquals('5c7ab73', $diffs[2]['a_sha']);
     $this->assertEquals('0000000', $diffs[2]['b_sha']);
     $this->assertEquals('100644', $diffs[2]['a_mode']);
     $this->assertNull($diffs[2]['b_mode']);
     $this->assertFalse($diffs[2]['is_new']);
     $this->assertTrue($diffs[2]['is_deleted']);
     $this->assertFalse($diffs[2]['is_renamed']);
     $this->assertStringStartsWith('@@', $diffs[2]['diff']);
     $this->assertNull($diffs[2]['sim_index']);
 }
Ejemplo n.º 3
0
 /**
  * @Serializer\VirtualProperty
  */
 public function getProposedPatch()
 {
     if (empty($this->fixedFile)) {
         return null;
     }
     $after = $this->fixedFile->getContent();
     if ($this->content === $after) {
         return null;
     }
     return DiffUtils::generate($this->content, $after);
 }
 private function parseTestCase($filename)
 {
     $content = file_get_contents($filename);
     $test = array('filename' => 'foo.php', 'code' => '', 'diff' => '', 'after' => null, 'comments' => array(), 'config' => '', 'new_file' => null, 'original_file' => null);
     $block = null;
     foreach (explode("\n", $content) as $line) {
         if (0 === strpos($line, '-- DIFF --')) {
             $block = 'diff';
             continue;
         }
         if (0 === strpos($line, '-- AFTER --')) {
             $block = 'after';
             continue;
         }
         if (0 === strpos($line, '-- COMMENTS --')) {
             $block = 'comments';
             continue;
         }
         if (0 === strpos($line, '-- CONFIG --')) {
             $block = 'config';
             continue;
         }
         if (0 === strpos($line, '-- FILENAME --')) {
             $block = 'filename';
             continue;
         }
         if (0 === strpos($line, '-- ORIGINAL_FILE --')) {
             $block = 'original_file';
             continue;
         }
         if (0 === strpos($line, '-- NEW_FILE --')) {
             $block = 'new_file';
             continue;
         }
         switch ($block) {
             case 'config':
                 if ('' !== trim($line)) {
                     $test['config'] .= $line;
                 }
                 break;
             case 'new_file':
             case 'original_file':
             case 'filename':
                 if ('' !== trim($line)) {
                     $test[$block] = trim($line);
                 }
                 break;
             case 'comments':
                 if ('' === trim($line)) {
                     break;
                 }
                 if (!preg_match('/^Line ([0-9]+): (.*)/', $line, $match)) {
                     throw new \RuntimeException('Invalid Comment Line: ' . $line);
                 }
                 $test['comments'][(int) $match[1]][] = $match[2];
                 break;
             case 'diff':
                 if ('' !== trim($line)) {
                     $test['diff'] .= $line . "\n";
                 }
                 break;
             case 'after':
                 $test['after'] .= $line . "\n";
                 break;
             default:
                 $test['code'] .= $line . "\n";
         }
     }
     if (null !== $test['new_file'] && null !== $test['original_file']) {
         if (!is_file($newFile = dirname($filename) . '/' . $test['new_file'])) {
             throw new \InvalidArgumentException(sprintf('New file not found at "%s".', $newFile));
         }
         if (!is_file($originalFile = dirname($filename) . '/' . $test['original_file'])) {
             throw new \InvalidArgumentException(sprintf('Original file not found at "%s".', $originalFile));
         }
         $test['code'] = file_get_contents($newFile);
         $test['diff'] = DiffUtils::generate(file_get_contents($originalFile), $test['code']);
     }
     if ('' === $test['config']) {
         $test['config'] = array();
     } else {
         $test['config'] = eval($test['config']);
         if (!is_array($test['config'])) {
             throw new \RuntimeException('Config must be an array, but got ' . var_export($test['config'], true));
         }
     }
     return $test;
 }