/** * tests parsing an not empty data collection with children * * @group data * @group sourcetest */ public function testParseOnNotEmptyDatacollectionWithChildren() { $mockCollection = $this->getMock('\\Browscap\\Data\\DataCollection', array('getDivisions', 'getDefaultProperties'), array(), '', false); $mockCollection->expects(self::never())->method('getDivisions')->will(self::returnValue(array())); $mockDivision = $this->getMock('\\Browscap\\Data\\Division', array('getUserAgents'), array(), '', false); $mockDivision->expects(self::once())->method('getUserAgents')->will(self::returnValue(array(0 => array('properties' => array('avd' => 'xyz'))))); $mockCollection->expects(self::once())->method('getDefaultProperties')->will(self::returnValue($mockDivision)); $uaData = array(0 => array('userAgent' => 'abc', 'properties' => array('Parent' => 'Defaultproperties', 'Version' => '1.0', 'MajorVer' => 1, 'Browser' => 'xyz'), 'children' => array(0 => array('match' => 'abc*', 'properties' => array('Browser' => 'xyza'))))); $mockDivision = $this->getMock('\\Browscap\\Data\\Division', array('getUserAgents'), array(), '', false); $mockDivision->expects(self::once())->method('getUserAgents')->will(self::returnValue($uaData)); $this->object->setLogger($this->logger); self::assertSame($this->object, $this->object->setDataCollection($mockCollection)); $result = $this->object->expand($mockDivision, 'TestDivision'); self::assertInternalType('array', $result); self::assertCount(2, $result); }
/** * tests parsing an not empty data collection with children * * @group data * @group sourcetest */ public function testParseOnNotEmptyDatacollectionWithChildren() { $collection = $this->getMockBuilder(\Browscap\Data\DataCollection::class)->disableOriginalConstructor()->setMethods(['getDivisions', 'getDefaultProperties'])->getMock(); $collection->expects(self::never())->method('getDivisions')->will(self::returnValue([])); $division = $this->getMockBuilder(\Browscap\Data\Division::class)->disableOriginalConstructor()->setMethods(['getUserAgents'])->getMock(); $division->expects(self::once())->method('getUserAgents')->will(self::returnValue([0 => ['properties' => ['avd' => 'xyz']]])); $collection->expects(self::once())->method('getDefaultProperties')->will(self::returnValue($division)); $uaData = [0 => ['userAgent' => 'abc', 'properties' => ['Parent' => 'Defaultproperties', 'Version' => '1.0', 'MajorVer' => 1, 'Browser' => 'xyz'], 'children' => [0 => ['match' => 'abc*', 'properties' => ['Browser' => 'xyza']]]]]; $division = $this->getMockBuilder(\Browscap\Data\Division::class)->disableOriginalConstructor()->setMethods(['getUserAgents'])->getMock(); $division->expects(self::once())->method('getUserAgents')->will(self::returnValue($uaData)); $this->object->setLogger($this->logger); self::assertSame($this->object, $this->object->setDataCollection($collection)); $result = $this->object->expand($division, 'TestDivision'); self::assertInternalType('array', $result); self::assertCount(2, $result); }
/** * renders all found useragents into a string * * @param string[] $section * @param \Browscap\Data\DataCollection $collection * @param array[] $sections * @param string $sectionName * * @throws \InvalidArgumentException * @return JsonWriter */ public function renderSectionBody(array $section, DataCollection $collection, array $sections = [], $sectionName = '') { if ($this->isSilent()) { return $this; } $division = $collection->getDefaultProperties(); $ua = $division->getUserAgents(); $defaultproperties = $ua[0]['properties']; $properties = array_merge(['Parent'], array_keys($defaultproperties)); foreach ($defaultproperties as $propertyName => $propertyValue) { $defaultproperties[$propertyName] = $this->expander->trimProperty($propertyValue); } $propertiesToOutput = []; foreach ($properties as $property) { if (!isset($section[$property])) { continue; } if (!isset($this->outputProperties[$property])) { $this->outputProperties[$property] = $this->getFilter()->isOutputProperty($property, $this); } if (!$this->outputProperties[$property]) { continue; } if (isset($section['Parent']) && 'Parent' !== $property) { if ('DefaultProperties' === $section['Parent'] || !isset($sections[$section['Parent']])) { if (isset($defaultproperties[$property]) && $defaultproperties[$property] === $section[$property]) { continue; } } else { $parentProperties = $sections[$section['Parent']]; if (isset($parentProperties[$property]) && $parentProperties[$property] === $section[$property]) { continue; } } } $propertiesToOutput[$property] = $section[$property]; } fputs($this->file, $this->getFormatter()->formatPropertyValue(json_encode($propertiesToOutput), 'Comment')); return $this; }
/** * Entry point for generating builds for a specified version * * @param string $version * @param string $resourceFolder * @param \Psr\Log\LoggerInterface $logger * @param \Browscap\Writer\WriterCollection $writerCollection * @param \Browscap\Helper\CollectionCreator $collectionCreator * * @throws \Exception */ public static function run($version, $resourceFolder, LoggerInterface $logger, WriterCollection $writerCollection, CollectionCreator $collectionCreator) { $logger->info('started creating a data collection'); $dataCollection = new DataCollection($version); $dataCollection->setLogger($logger); $collectionCreator->setLogger($logger)->setDataCollection($dataCollection); $collection = $collectionCreator->createDataCollection($resourceFolder); $logger->info('finished creating a data collection'); $logger->info('started initialisation of writers'); $expander = new Expander(); $expander->setDataCollection($collection)->setLogger($logger); $logger->info('finished initialisation of writers'); $logger->info('started output of header and version'); $comments = ['Provided courtesy of http://browscap.org/', 'Created on ' . $collection->getGenerationDate()->format('l, F j, Y \\a\\t h:i A T'), 'Keep up with the latest goings-on with the project:', 'Follow us on Twitter <https://twitter.com/browscap>, or...', 'Like us on Facebook <https://facebook.com/browscap>, or...', 'Collaborate on GitHub <https://github.com/browscap>, or...', 'Discuss on Google Groups <https://groups.google.com/forum/#!forum/browscap>.']; $writerCollection->setExpander($expander)->fileStart()->renderHeader($comments)->renderVersion($version, $collection); $logger->info('finished output of header and version'); $output = []; $logger->info('started output of divisions'); $division = $collection->getDefaultProperties(); $logger->info('handle division ' . $division->getName()); $writerCollection->renderAllDivisionsHeader($collection)->renderDivisionHeader($division->getName()); $ua = $division->getUserAgents(); $sections = [$ua[0]['userAgent'] => $ua[0]['properties']]; foreach (array_keys($sections) as $sectionName) { $section = $sections[$sectionName]; $writerCollection->setSilent($division)->renderSectionHeader($sectionName)->renderSectionBody($section, $collection, $sections, $sectionName)->renderSectionFooter($sectionName); } $writerCollection->renderDivisionFooter(); foreach ($collection->getDivisions() as $division) { /** @var \Browscap\Data\Division $division */ // run checks on division before expanding versions because the checked properties do not change between // versions $sections = $expander->expand($division, $division->getName()); $logger->info('checking division ' . $division->getName()); foreach (array_keys($sections) as $sectionName) { $section = $sections[$sectionName]; $collection->checkProperty($sectionName, $section); } $writerCollection->setSilent($division); $versions = $division->getVersions(); foreach ($versions as $version) { list($majorVer, $minorVer) = $expander->getVersionParts($version); $divisionName = $expander->parseProperty($division->getName(), $majorVer, $minorVer); $logger->info('handle division ' . $divisionName); $encodedSections = json_encode($sections); $encodedSections = $expander->parseProperty($encodedSections, $majorVer, $minorVer); $sectionsWithVersion = json_decode($encodedSections, true); $firstElement = current($sectionsWithVersion); $writerCollection->renderDivisionHeader($divisionName, $firstElement['Parent']); foreach (array_keys($sectionsWithVersion) as $sectionName) { if (array_key_exists($sectionName, $output)) { $logger->error('tried to add section "' . $sectionName . '" more than once -> skipped'); continue; } $section = $sectionsWithVersion[$sectionName]; $writerCollection->setSilentSection($section); $writerCollection->renderSectionHeader($sectionName)->renderSectionBody($section, $collection, $sectionsWithVersion, $sectionName)->renderSectionFooter($sectionName); $output[$sectionName] = $sectionName; } $writerCollection->renderDivisionFooter(); unset($divisionName, $majorVer, $minorVer); } } $division = $collection->getDefaultBrowser(); $logger->info('handle division ' . $division->getName()); $writerCollection->renderDivisionHeader($division->getName()); $ua = $division->getUserAgents(); $sections = [$ua[0]['userAgent'] => array_merge(['Parent' => 'DefaultProperties'], $ua[0]['properties'])]; foreach (array_keys($sections) as $sectionName) { $section = $sections[$sectionName]; $writerCollection->setSilent($division)->renderSectionHeader($sectionName)->renderSectionBody($section, $collection, $sections, $sectionName)->renderSectionFooter($sectionName); } $writerCollection->renderDivisionFooter()->renderAllDivisionsFooter(); $logger->info('finished output of divisions'); $logger->info('started closing writers'); $writerCollection->fileEnd()->close(); $logger->info('finished closing writers'); }