Browscap\Generator\DiffGenerator::run PHP Method

run() public method

Entry point for generating builds for a specified version
public run ( string $leftFilename, string $rightFilename )
$leftFilename string
$rightFilename string
    public function run($leftFilename, $rightFilename)
    {
        $this->logger->debug('parsing left file ' . $leftFilename);
        $iniParserLeft = new IniParser($leftFilename);
        $leftFile = $iniParserLeft->setShouldSort(true)->parse();
        $this->logger->debug('parsing right file ' . $rightFilename);
        $iniParserRight = new IniParser($rightFilename);
        $rightFile = $iniParserRight->setShouldSort(true)->parse();
        $this->logger->debug('build diffs between files');
        $ltrDiff = $this->recursiveArrayDiff($leftFile, $rightFile);
        $rtlDiff = $this->recursiveArrayDiff($rightFile, $leftFile);
        $this->logger->debug('LTR');
        $this->logger->debug(var_export($ltrDiff, true));
        $this->logger->debug('RTL');
        $this->logger->debug(var_export($rtlDiff, true));
        $this->diffsFound = 0;
        if (count($ltrDiff) || count($rtlDiff)) {
            $this->logger->info('The following differences have been found:');
            $sectionsRead = [];
            $this->logger->debug('Pass 1 (LTR)');
            foreach ($ltrDiff as $section => $props) {
                if (isset($rightFile[$section]) && is_array($rightFile[$section])) {
                    $this->compareSectionProperties($section, $props, isset($rtlDiff[$section]) ? $rtlDiff[$section] : null, $rightFile[$section]);
                } else {
                    $this->logger->info('[' . $section . ']' . "\n" . 'Whole section only on LEFT');
                    ++$this->diffsFound;
                }
                $sectionsRead[] = $section;
            }
            $this->logger->debug('Pass 2 (RTL)');
            foreach ($rtlDiff as $section => $props) {
                if (in_array($section, $sectionsRead)) {
                    continue;
                }
                if (isset($leftFile[$section]) && is_array($leftFile[$section])) {
                    $this->compareSectionProperties($section, isset($ltrDiff[$section]) ? $ltrDiff[$section] : [], $props, $rightFile[$section]);
                } else {
                    $this->logger->info('[' . $section . ']' . "\n" . 'Whole section only on RIGHT');
                    ++$this->diffsFound;
                }
            }
            $msg = sprintf('%sThere %s %d difference%s found in the comparison.', "\n", $this->diffsFound === 1 ? 'was' : 'were', $this->diffsFound, $this->diffsFound === 1 ? '' : 's');
            $this->logger->info($msg);
        } else {
            $this->logger->info('No differences found, hooray!');
        }
    }

Usage Example

コード例 #1
0
    /**
     * tests running the generation of a diff
     *
     * @group generator
     * @group sourcetest
     */
    public function testRun()
    {
        $mock = $this->getMock('\\Monolog\\Logger', array(), array(), '', false);
        self::assertSame($this->object, $this->object->setLogger($mock));
        $tmpfile = tempnam(sys_get_temp_dir(), 'browscaptest');
        $in = <<<HERE
; comment

[test]
test=test
HERE;
        file_put_contents($tmpfile, $in);
        self::assertNull($this->object->run($tmpfile, $tmpfile));
        unlink($tmpfile);
    }