-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to set version truncation (#29)
- Loading branch information
Showing
7 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Acsiomatic\DeviceDetectorBundle\Tests; | ||
|
||
use Acsiomatic\DeviceDetectorBundle\AcsiomaticDeviceDetectorBundle; | ||
use Acsiomatic\DeviceDetectorBundle\Contracts\DeviceDetectorFactoryInterface; | ||
use Acsiomatic\DeviceDetectorBundle\Tests\Util\Compiler\CompilerPassFactory; | ||
use Acsiomatic\DeviceDetectorBundle\Tests\Util\HttpKernel\Kernel; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
|
||
final class VersionTruncationTest extends TestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const USER_AGENT = 'Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36'; | ||
|
||
public function testDefaultsTargetsToMinor(): void | ||
{ | ||
$kernel = new Kernel('test', true); | ||
$kernel->appendBundle(new FrameworkBundle()); | ||
$kernel->appendBundle(new AcsiomaticDeviceDetectorBundle()); | ||
$kernel->appendExtensionConfiguration('framework', ['test' => true, 'secret' => '53CR37']); | ||
|
||
$kernel->appendCompilerPass( | ||
CompilerPassFactory::createPublicAlias( | ||
'device_detector_factory.public', | ||
DeviceDetectorFactoryInterface::class | ||
) | ||
); | ||
|
||
$kernel->boot(); | ||
|
||
/** @var DeviceDetectorFactoryInterface $deviceDetectorFactory */ | ||
$deviceDetectorFactory = $kernel->getContainer()->get('device_detector_factory.public'); | ||
$deviceDetector = $deviceDetectorFactory->createDeviceDetector(); | ||
|
||
$deviceDetector->setUserAgent(self::USER_AGENT); | ||
$deviceDetector->parse(); | ||
|
||
static::assertSame('4.2', $deviceDetector->getOs('version')); | ||
static::assertSame('34.0', $deviceDetector->getClient('version')); | ||
} | ||
|
||
/** | ||
* @dataProvider getVersionTruncationFixtures | ||
*/ | ||
public function testSetVersionTruncation( | ||
string $userAgent, | ||
string $truncation, | ||
string $expectedOsVersion, | ||
string $expectedClientVersion | ||
): void { | ||
$kernel = new Kernel('test', true); | ||
$kernel->appendBundle(new FrameworkBundle()); | ||
$kernel->appendBundle(new AcsiomaticDeviceDetectorBundle()); | ||
$kernel->appendExtensionConfiguration('framework', ['test' => true, 'secret' => '53CR37']); | ||
$kernel->appendExtensionConfiguration('acsiomatic_device_detector', ['version_truncation' => $truncation]); | ||
|
||
$kernel->appendCompilerPass( | ||
CompilerPassFactory::createPublicAlias( | ||
'device_detector_factory.public', | ||
DeviceDetectorFactoryInterface::class | ||
) | ||
); | ||
|
||
$kernel->boot(); | ||
|
||
/** @var DeviceDetectorFactoryInterface $deviceDetectorFactory */ | ||
$deviceDetectorFactory = $kernel->getContainer()->get('device_detector_factory.public'); | ||
$deviceDetector = $deviceDetectorFactory->createDeviceDetector(); | ||
|
||
$deviceDetector->setUserAgent($userAgent); | ||
$deviceDetector->parse(); | ||
|
||
static::assertSame($expectedOsVersion, $deviceDetector->getOs('version')); | ||
static::assertSame($expectedClientVersion, $deviceDetector->getClient('version')); | ||
} | ||
|
||
/** | ||
* @return iterable<array{user_agent: string, truncation: string, os_version: string, client_version: string}> | ||
*/ | ||
public function getVersionTruncationFixtures(): iterable | ||
{ | ||
yield 'none' => [ | ||
'user_agent' => self::USER_AGENT, | ||
'truncation' => 'none', | ||
'os_version' => '4.2.2', | ||
'client_version' => '34.0.1847.114', | ||
]; | ||
|
||
yield 'build' => [ | ||
'user_agent' => self::USER_AGENT, | ||
'truncation' => 'build', | ||
'os_version' => '4.2.2', | ||
'client_version' => '34.0.1847.114', | ||
]; | ||
|
||
yield 'patch' => [ | ||
'user_agent' => self::USER_AGENT, | ||
'truncation' => 'patch', | ||
'os_version' => '4.2.2', | ||
'client_version' => '34.0.1847', | ||
]; | ||
|
||
yield 'minor' => [ | ||
'user_agent' => self::USER_AGENT, | ||
'truncation' => 'minor', | ||
'os_version' => '4.2', | ||
'client_version' => '34.0', | ||
]; | ||
|
||
yield 'major' => [ | ||
'user_agent' => self::USER_AGENT, | ||
'truncation' => 'major', | ||
'os_version' => '4', | ||
'client_version' => '34', | ||
]; | ||
} | ||
} |