@@ -230,7 +230,7 @@ public function testParse(array $fixtureData): void
230230 }
231231
232232 $ errorMessage = \sprintf (
233- "UserAgent: %s \nHeaders: %s " ,
233+ "UserAgent: %s \nHeaders: %s \n Version truncation: none " ,
234234 $ ua ,
235235 \print_r ($ fixtureData ['headers ' ] ?? null , true )
236236 );
@@ -240,6 +240,48 @@ public function testParse(array $fixtureData): void
240240 $ this ->assertEquals ($ fixtureData , $ uaInfo , $ errorMessage );
241241 }
242242
243+ /**
244+ * @dataProvider getFixtures
245+ */
246+ public function testParseWithVersionTruncationMajor (array $ fixtureData ): void
247+ {
248+ $ ua = $ fixtureData ['user_agent ' ];
249+ $ clientHints = !empty ($ fixtureData ['headers ' ]) ? ClientHints::factory ($ fixtureData ['headers ' ]) : null ;
250+
251+ AbstractDeviceParser::setVersionTruncation (AbstractDeviceParser::VERSION_TRUNCATION_MAJOR );
252+
253+ try {
254+ $ uaInfo = DeviceDetector::getInfoFromUserAgent ($ ua , $ clientHints );
255+ } catch (\Exception $ exception ) {
256+ throw new \Exception (
257+ \sprintf ('Error: %s from useragent %s ' , $ exception ->getMessage (), $ ua ),
258+ $ exception ->getCode (),
259+ $ exception
260+ );
261+ }
262+
263+ $ errorMessage = \sprintf (
264+ "UserAgent: %s \nHeaders: %s \nVersion truncation: major " ,
265+ $ ua ,
266+ \print_r ($ fixtureData ['headers ' ] ?? null , true )
267+ );
268+
269+ unset($ fixtureData ['headers ' ]); // ignore headers in result
270+
271+ // truncate versions
272+ if (\array_key_exists ('version ' , $ fixtureData ['os ' ] ?? [])) {
273+ $ fixtureData ['os ' ]['version ' ] = self ::truncateVersion ($ fixtureData ['os ' ]['version ' ]);
274+ }
275+
276+ if (\array_key_exists ('version ' , $ fixtureData ['client ' ] ?? [])) {
277+ $ fixtureData ['client ' ]['version ' ] = self ::truncateVersion ($ fixtureData ['client ' ]['version ' ]);
278+ }
279+
280+ $ this ->assertEquals ($ fixtureData , $ uaInfo , $ errorMessage );
281+
282+ AbstractDeviceParser::setVersionTruncation (AbstractDeviceParser::VERSION_TRUNCATION_NONE );
283+ }
284+
243285 public function getFixtures (): array
244286 {
245287 $ fixtures = [];
@@ -613,7 +655,7 @@ public function testSetYamlParserInvalid(): void
613655 public function testSetYamlParser (): void
614656 {
615657 $ reader = function & ($ object , $ property ) {
616- $ value = & Closure::bind (function & () use ($ property ) {
658+ $ value = &Closure::bind (function & () use ($ property ) {
617659 return $ this ->$ property ;
618660 }, $ object , $ object )->__invoke ();
619661
@@ -630,15 +672,15 @@ public function testSetYamlParser(): void
630672
631673 foreach ($ dd ->getClientParsers () as $ parser ) {
632674 if ($ parser instanceof MobileApp) {
633- $ appHints = & $ reader ($ parser , 'appHints ' );
675+ $ appHints = &$ reader ($ parser , 'appHints ' );
634676 $ this ->assertInstanceOf (Symfony::class, $ appHints ->getYamlParser ());
635677 }
636678
637679 if (!($ parser instanceof Browser)) {
638680 continue ;
639681 }
640682
641- $ browserHints = & $ reader ($ parser , 'browserHints ' );
683+ $ browserHints = &$ reader ($ parser , 'browserHints ' );
642684 $ this ->assertInstanceOf (Symfony::class, $ browserHints ->getYamlParser ());
643685 }
644686 }
@@ -741,4 +783,19 @@ private function getDeviceDetector(): DeviceDetector
741783
742784 return $ dd ;
743785 }
786+
787+ private static function truncateVersion (?string $ versionString ): ?string
788+ {
789+ if (null === $ versionString ) {
790+ return null ;
791+ }
792+
793+ if (\substr_count ($ versionString , '. ' ) > 0 ) {
794+ $ versionParts = \explode ('. ' , $ versionString );
795+ $ versionParts = \array_slice ($ versionParts , 0 , 1 );
796+ $ versionString = \implode ('. ' , $ versionParts );
797+ }
798+
799+ return \trim ($ versionString , ' . ' );
800+ }
744801}
0 commit comments