-
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.
- Loading branch information
Showing
11 changed files
with
171 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Acsiomatic\DeviceDetectorBundle\Contracts; | ||
|
||
use DeviceDetector\ClientHints; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
interface ClientHintsFactoryInterface | ||
{ | ||
public function createClientHintsFromRequest(Request $request): ClientHints; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Acsiomatic\DeviceDetectorBundle\Factory; | ||
|
||
use Acsiomatic\DeviceDetectorBundle\Contracts\ClientHintsFactoryInterface; | ||
use DeviceDetector\ClientHints; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ClientHintsFactory implements ClientHintsFactoryInterface | ||
{ | ||
public function createClientHintsFromRequest(Request $request): ClientHints | ||
{ | ||
$headers = []; | ||
foreach ($request->headers->keys() as $key) { | ||
$headers[$key] = $request->headers->get($key); | ||
} | ||
|
||
return ClientHints::factory($headers); | ||
} | ||
} |
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,80 @@ | ||
<?php | ||
|
||
namespace Acsiomatic\DeviceDetectorBundle\Tests; | ||
|
||
use Acsiomatic\DeviceDetectorBundle\AcsiomaticDeviceDetectorBundle; | ||
use Acsiomatic\DeviceDetectorBundle\Contracts\ClientHintsFactoryInterface; | ||
use Acsiomatic\DeviceDetectorBundle\Tests\Util\Compiler\CompilerPassFactory; | ||
use Acsiomatic\DeviceDetectorBundle\Tests\Util\HttpKernel\Kernel; | ||
use DeviceDetector\ClientHints; | ||
use DeviceDetector\DeviceDetector; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
final class ClientHintsTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!method_exists(DeviceDetector::class, 'getClientHints')) { | ||
static::markTestSkipped('The MySQLi extension is not available.'); | ||
} | ||
} | ||
|
||
public function testDeviceDetectorReceivesClientHints(): 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.public', DeviceDetector::class)); | ||
$kernel->appendCompilerPass(CompilerPassFactory::createPublicAlias('request_stack.public', RequestStack::class)); | ||
|
||
$kernel->boot(); | ||
|
||
$request = new Request(); | ||
$request->headers->set('Sec-CH-UA-Platform', 'Windows'); | ||
$request->headers->set('Sec-CH-UA-Platform-Version', '10.0.0'); | ||
|
||
/** @var RequestStack $requestStack */ | ||
$requestStack = $kernel->getContainer()->get('request_stack.public'); | ||
$requestStack->push($request); | ||
|
||
/** @var DeviceDetector $deviceDetector */ | ||
$deviceDetector = $kernel->getContainer()->get('device_detector.public'); | ||
$clientHints = $deviceDetector->getClientHints(); | ||
|
||
static::assertInstanceOf(ClientHints::class, $clientHints); | ||
static::assertSame('Windows', $clientHints->getOperatingSystem()); | ||
static::assertSame('10.0.0', $clientHints->getOperatingSystemVersion()); | ||
} | ||
|
||
public function testFactoryCreatesClientHintsFromRequest(): 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( | ||
'client_hints_factory.public', | ||
ClientHintsFactoryInterface::class | ||
) | ||
); | ||
|
||
$kernel->boot(); | ||
|
||
/** @var ClientHintsFactoryInterface $clientHintsFactory */ | ||
$clientHintsFactory = $kernel->getContainer()->get('client_hints_factory.public'); | ||
|
||
$request = new Request(); | ||
$request->headers->set('Sec-CH-UA-Platform', 'Windows'); | ||
$request->headers->set('Sec-CH-UA-Platform-Version', '10.0.0'); | ||
|
||
$clientHints = $clientHintsFactory->createClientHintsFromRequest($request); | ||
|
||
static::assertSame('Windows', $clientHints->getOperatingSystem()); | ||
static::assertSame('10.0.0', $clientHints->getOperatingSystemVersion()); | ||
} | ||
} |