diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f7d6396..6d59a1c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -31,7 +31,7 @@ jobs: - stable symfony-require: # Test latest stable version - - 7.* + - 6.4 steps: - name: Checkout code uses: actions/checkout@v3 diff --git a/Makefile b/Makefile index 4ea0654..617443d 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,8 @@ lint: php vendor/bin/phpcs --report=code php vendor/bin/phpstan analyse +refactor: + php vendor/bin/php-cs-fixer fix + php vendor/bin/rector + test: lint phpunit diff --git a/README.md b/README.md index cdbee8d..692cae0 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,12 @@ MobileDetectBundle ================== -Symfony 3.4.x-6.0.x bundle to detect mobile devices, manage mobile view and redirect to the mobile and tablet version. +Symfony 6.4.x / PHP 8.2 bundle to detect mobile devices, manage mobile view and redirect to the mobile and tablet version. -[![Github Actions Status](https://github.com/tattali/MobileDetectBundle/actions/workflows/main.yml/badge.svg?branch=main -)](https://github.com/tattali/MobileDetectBundle/actions/workflows/main.yml?query=branch%3Amain) [![Latest Stable Version](http://poser.pugx.org/tattali/mobile-detect-bundle/v)](https://packagist.org/packages/tattali/mobile-detect-bundle) [![Total Downloads](http://poser.pugx.org/tattali/mobile-detect-bundle/downloads)](https://packagist.org/packages/tattali/mobile-detect-bundle) [![codecov](https://codecov.io/gh/tattali/MobileDetectBundle/branch/main/graph/badge.svg?token=HWV1OYRSD9)](https://codecov.io/gh/tattali/MobileDetectBundle) [![License](http://poser.pugx.org/tattali/mobile-detect-bundle/license)](https://packagist.org/packages/tattali/mobile-detect-bundle) [![PHP Version Require](http://poser.pugx.org/tattali/mobile-detect-bundle/require/php)](https://packagist.org/packages/tattali/mobile-detect-bundle) +[![Github Actions Status](https://github.com/digifa/MobileDetectBundle/actions/workflows/main.yml/badge.svg?branch=main +)](https://github.com/digifa/MobileDetectBundle/actions/workflows/main.yml?query=branch%3Amain) [![Latest Stable Version](http://poser.pugx.org/digifa/mobile-detect-bundle/v)](https://packagist.org/packages/digifa/mobile-detect-bundle) [![Total Downloads](http://poser.pugx.org/digifa/mobile-detect-bundle/downloads)](https://packagist.org/packages/digifa/mobile-detect-bundle) [![codecov](https://codecov.io/gh/digifa/MobileDetectBundle/branch/main/graph/badge.svg?token=HWV1OYRSD9)](https://codecov.io/gh/digifa/MobileDetectBundle) [![License](http://poser.pugx.org/digifa/mobile-detect-bundle/license)](https://packagist.org/packages/digifa/mobile-detect-bundle) [![PHP Version Require](http://poser.pugx.org/digifa/mobile-detect-bundle/require/php)](https://packagist.org/packages/digifa/mobile-detect-bundle) -*This bundle is a fork of [suncat2000/MobileDetectBundle](https://github.com/suncat2000/MobileDetectBundle). As this project doesn't look maintained anymore, we decided to create & maintain a fork. For more information read our [manifest](https://github.com/tattali/MobileDetectBundle/issues/8).* +*This bundle is a fork of [tattali/MobileDetectBundle](https://github.com/tattali/MobileDetectBundle) that is a fork of [suncat2000/MobileDetectBundle](https://github.com/suncat2000/MobileDetectBundle). As this project doesn't look maintained anymore, we decided to create & maintain a fork. For more information read our [manifest](https://github.com/tattali/MobileDetectBundle/issues/8).* Introduction ------------ @@ -76,4 +76,4 @@ License and credits This bundle is under the MIT license. See the complete [license](src/Resources/meta/LICENSE) in the bundle -Original authors: [suncat2000](https://github.com/suncat2000), [HenriVesala](https://github.com/HenriVesala), [netmikey](https://github.com/netmikey) and [all contributors](https://github.com/suncat2000/MobileDetectBundle/graphs/contributors) +Original authors: [tattali](https://gihub.com/tattali), [suncat2000](https://github.com/suncat2000), [HenriVesala](https://github.com/HenriVesala), [netmikey](https://github.com/netmikey) and [all contributors](https://github.com/suncat2000/MobileDetectBundle/graphs/contributors) diff --git a/composer.json b/composer.json index 8180cfb..933c9f2 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "tattali/mobile-detect-bundle", - "description": "Symfony 5.x-7.x bundle to detect mobile devices, manage mobile view and redirect to the mobile and tablet version.", + "name": "digifa/mobile-detect-bundle", + "description": "Symfony 6.4 / PHP 8.2 bundle to detect mobile devices, manage mobile view and redirect to the mobile and tablet version.", "keywords": [ "mobile detect", "device detect", @@ -12,10 +12,14 @@ "symfony mobiledetect", "symfony" ], - "homepage": "https://github.com/tattali/MobileDetectBundle", + "homepage": "https://github.com/digifa/MobileDetectBundle", "type": "symfony-bundle", "license": "MIT", "authors": [ + { + "name": "Guido Faust", + "email": "gfaust@digifa.de" + }, { "name": "Théo Attali", "email": "theoattali@gmail.com" @@ -35,23 +39,24 @@ ], "require": { "php": ">=8.2", - "mobiledetect/mobiledetectlib": "^2.8.15", - "symfony/dependency-injection": "^7.0", - "symfony/event-dispatcher": "^7.0", - "symfony/framework-bundle": "^7.0", - "symfony/yaml": "^7.0", + "mobiledetect/mobiledetectlib": "^4.8", + "symfony/dependency-injection": "^6.4", + "symfony/event-dispatcher": "^6.4", + "symfony/framework-bundle": "^6.4", + "symfony/yaml": "^6.4", "twig/twig": "^2.0 || ^3.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.8", + "friendsofphp/php-cs-fixer": "^3.4 || ^3.8", "phpstan/extension-installer": "^1.0", + "phpstan/phpstan": "^1.0", "phpstan/phpstan-nette": "^1.0", "phpstan/phpstan-phpunit": "^1.0", - "phpstan/phpstan": "^1.0", "phpunit/phpunit": "^9.5", + "rector/rector": "^1.2", "squizlabs/php_codesniffer": "^3.6", - "symfony/dotenv": "^7.0", - "symfony/phpunit-bridge": "^7.0" + "symfony/dotenv": "^6.4", + "symfony/phpunit-bridge": "^6.4" }, "config": { "sort-packages": true, diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..374e31c --- /dev/null +++ b/rector.php @@ -0,0 +1,30 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withPhpSets(php82: true) + ->withAttributesSets(symfony: true, doctrine: true) + ->withSets([ + Rector\Symfony\Set\SymfonySetList::SYMFONY_50, + Rector\Symfony\Set\SymfonySetList::SYMFONY_50_TYPES, + Rector\Symfony\Set\SymfonySetList::SYMFONY_51, + Rector\Symfony\Set\SymfonySetList::SYMFONY_52, + Rector\Symfony\Set\SymfonySetList::SYMFONY_52_VALIDATOR_ATTRIBUTES, + Rector\Symfony\Set\SymfonySetList::SYMFONY_53, + Rector\Symfony\Set\SymfonySetList::SYMFONY_60, + Rector\Symfony\Set\SymfonySetList::SYMFONY_61, + Rector\Symfony\Set\SymfonySetList::SYMFONY_62, + Rector\Symfony\Set\SymfonySetList::SYMFONY_63, + Rector\Symfony\Set\SymfonySetList::SYMFONY_64, + Rector\Symfony\Set\SymfonySetList::SYMFONY_CODE_QUALITY, + Rector\Symfony\Set\SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION, + ]) + ->withDeadCodeLevel(10) + ->withTypeCoverageLevel(10); diff --git a/src/DataCollector/DeviceDataCollector.php b/src/DataCollector/DeviceDataCollector.php index 071b28d..8d0116d 100644 --- a/src/DataCollector/DeviceDataCollector.php +++ b/src/DataCollector/DeviceDataCollector.php @@ -45,7 +45,7 @@ public function __construct(DeviceView $deviceView) public function collect( Request $request, Response $response, - \Throwable $exception = null + ?\Throwable $exception = null, ): void { $this->data['currentView'] = $this->deviceView->getViewType(); $this->data['views'] = [ @@ -127,7 +127,7 @@ protected function canUseView(string $view, ?string $host): bool && !empty($this->redirectConfig[$view]['host']) && \in_array($this->redirectConfig[$view]['action'], [RequestResponseListener::REDIRECT, RequestResponseListener::REDIRECT_WITHOUT_PATH], true) ) { - $parseHost = parse_url($this->redirectConfig[$view]['host']); + $parseHost = parse_url((string) $this->redirectConfig[$view]['host']); $redirectHost = $parseHost['scheme'].'://'.$parseHost['host']; if (!empty($parseHost['port'])) { $redirectHost .= ':'.$parseHost['port']; @@ -143,7 +143,7 @@ protected function canUseView(string $view, ?string $host): bool private function generateSwitchLink( Request $request, - string $view + string $view, ): ?string { $requestSwitchView = $request->duplicate(); $requestSwitchView->query->set($this->deviceView->getSwitchParam(), $view); diff --git a/src/DependencyInjection/MobileDetectExtension.php b/src/DependencyInjection/MobileDetectExtension.php index 016565b..067903b 100644 --- a/src/DependencyInjection/MobileDetectExtension.php +++ b/src/DependencyInjection/MobileDetectExtension.php @@ -15,8 +15,8 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; class MobileDetectExtension extends Extension { diff --git a/src/DeviceDetector/MobileDetector.php b/src/DeviceDetector/MobileDetector.php index 4634b08..f554ce9 100644 --- a/src/DeviceDetector/MobileDetector.php +++ b/src/DeviceDetector/MobileDetector.php @@ -13,9 +13,24 @@ namespace MobileDetectBundle\DeviceDetector; +use Detection\MobileDetect; + /** * @author suncat2000 */ -class MobileDetector extends \Mobile_Detect implements MobileDetectorInterface +class MobileDetector extends MobileDetect implements MobileDetectorInterface { + public const VERSION_TYPE_STRING = 'text'; + + public const VERSION_TYPE_FLOAT = 'float'; + + public static function getUserAgents(): array + { + return self::getBrowsers(); + } + + public function getCfHeaders(): array + { + return $this->getCloudFrontHttpHeaders(); + } } diff --git a/src/DeviceDetector/MobileDetectorInterface.php b/src/DeviceDetector/MobileDetectorInterface.php index 24241e0..5c02a07 100644 --- a/src/DeviceDetector/MobileDetectorInterface.php +++ b/src/DeviceDetector/MobileDetectorInterface.php @@ -196,10 +196,6 @@ * @method bool isNetFront() * @method bool isGenericBrowser() * @method bool isPaleMoon() - * @method bool isBot() - * @method bool isMobileBot() - * @method bool isDesktopMode() - * @method bool isTV() * @method bool isWebKit() * @method bool isConsole() * @method bool isWatch() @@ -211,97 +207,61 @@ interface MobileDetectorInterface * * @method bool is[...]() * - * @param string $name - * @param array $arguments - * * @throws \BadMethodCallException when the method doesn't exist and doesn't start with 'is' */ - public function __call($name, $arguments); + public function __call(string $name, array $arguments); /** * Retrieve the list of known browsers. Specifically, the user agents. * * @return array list of browsers / user agents */ - public static function getBrowsers(); + public static function getBrowsers(): array; /** * Retrieve the list of mobile operating systems. * * @return array the list of mobile operating systems */ - public static function getOperatingSystems(); + public static function getOperatingSystems(): array; /** * Retrieve the list of known phone devices. * * @return array list of phone devices */ - public static function getPhoneDevices(); + public static function getPhoneDevices(): array; /** * Get the properties array. - * - * @return array */ - public static function getProperties(); - - /** - * Get the current script version. - * This is useful for the demo.php file, - * so people can check on what version they are testing - * for mobile devices. - * - * @return string the version number in semantic version format - */ - public static function getScriptVersion(); + public static function getProperties(): array; /** * Retrieve the list of known tablet devices. * * @return array list of tablet devices */ - public static function getTabletDevices(); + public static function getTabletDevices(): array; /** * Alias for getBrowsers() method. * * @return array list of user agents */ - public static function getUserAgents(); - - /** - * Retrieve the list of known utilities. - * - * @return array list of utilities - */ - public static function getUtilities(); + public static function getUserAgents(): array; /** * Check the HTTP headers for signs of mobile. * This is the fastest mobile check possible; it's used * inside isMobile() method. - * - * @return bool */ - public function checkHttpHeadersForMobile(); + public function checkHttpHeadersForMobile(): bool; /** * Retrieves the cloudfront headers. - * - * @return array */ - public function getCfHeaders(); - - /** - * Set CloudFront headers - * http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-device. - * - * @param array $cfHeaders List of HTTP headers - * - * @return bool If there were CloudFront headers to be set - */ - public function setCfHeaders($cfHeaders = null); + public function getCfHeaders(): array; /** * Retrieves a particular header. If it doesn't exist, no exception/error is caused. @@ -313,28 +273,25 @@ public function setCfHeaders($cfHeaders = null); * * @return string|null the value of the header */ - public function getHttpHeader($header); + public function getHttpHeader(string $header): ?string; /** * Retrieves the HTTP headers. - * - * @return array */ - public function getHttpHeaders(); + public function getHttpHeaders(): array; /** * Set the HTTP Headers. Must be PHP-flavored. This method will reset existing headers. * - * @param array $httpHeaders The headers to set. If null, then using PHP's _SERVER to extract - * the headers. The default null is left for backwards compatibility. + * @param array $httpHeaders the headers to set */ - public function setHttpHeaders($httpHeaders = null); + public function setHttpHeaders(array $httpHeaders = []): void; - public function getMatchesArray(); + public function getMatchesArray(): ?array; - public function getMatchingRegex(); + public function getMatchingRegex(): ?string; - public function getMobileHeaders(); + public function getMobileHeaders(): array; /** * Get all possible HTTP headers that @@ -342,49 +299,41 @@ public function getMobileHeaders(); * * @return array list of HTTP headers */ - public function getUaHttpHeaders(); + public function getUaHttpHeaders(): array; /** * Retrieve the User-Agent. * * @return string|null the user agent if it's set */ - public function getUserAgent(); + public function getUserAgent(): ?string; /** * Set the User-Agent to be used. * * @param string $userAgent the user agent string to set - * - * @return string|null */ - public function setUserAgent($userAgent = null); + public function setUserAgent(string $userAgent): string; /** * This method checks for a certain property in the * userAgent. * - * @param string $key - * * @return bool|int|null */ - public function is($key); + public function is(string $key); /** * Check if the device is mobile. * Returns true if any type of mobile device detected, including special ones. - * - * @return bool */ - public function isMobile(); + public function isMobile(): bool; /** * Check if the device is a tablet. * Return true if any type of tablet device is detected. - * - * @return bool */ - public function isTablet(); + public function isTablet(): bool; /** * Some detection rules are relative (not standard), @@ -394,21 +343,15 @@ public function isTablet(); * * This method will be used to check custom regexes against * the User-Agent string. - * - * @param string $userAgent - * - * @return bool */ - public function match($regex, $userAgent = null); + public function match(string $regex, string $userAgent): bool; /** * Prepare the version number. * * @param string $ver The string version, like "2.6.21.2152"; - * - * @return float */ - public function prepareVersionNo($ver); + public function prepareVersionNo(string $ver): float; /** * Check the version of the given property in the User-Agent. @@ -423,5 +366,5 @@ public function prepareVersionNo($ver); * * @return string|float the version of the property we are trying to extract */ - public function version($propertyName, $type = MobileDetector::VERSION_TYPE_STRING); + public function version(string $propertyName, string $type = MobileDetector::VERSION_TYPE_STRING): float|bool|string; } diff --git a/src/EventListener/RequestResponseListener.php b/src/EventListener/RequestResponseListener.php index 47ccf0e..7b78263 100644 --- a/src/EventListener/RequestResponseListener.php +++ b/src/EventListener/RequestResponseListener.php @@ -77,7 +77,7 @@ public function __construct( DeviceView $deviceView, RouterInterface $router, array $redirectConf, - bool $fullPath = true + bool $fullPath = true, ) { $this->mobileDetector = $mobileDetector; $this->deviceView = $deviceView; @@ -232,7 +232,7 @@ protected function getRedirectUrl(Request $request, string $view): ?string $queryParams = $request->query->all(); $queryParams[$this->deviceView->getSwitchParam()] = $view; - return rtrim($this->redirectConf[$view]['host'], '/').$request->getPathInfo().'?'.Request::normalizeQueryString(http_build_query($queryParams)); + return rtrim((string) $this->redirectConf[$view]['host'], '/').$request->getPathInfo().'?'.Request::normalizeQueryString(http_build_query($queryParams)); } if (self::REDIRECT_WITHOUT_PATH === $routingOption) { // Make sure to hint at the device override, otherwise infinite loop @@ -272,8 +272,6 @@ protected function getRedirectResponse(Request $request, string $view): ?Redirec */ protected function prepareResponseModification(string $view): void { - $this->modifyResponseClosure = static function (DeviceView $deviceView, ResponseEvent $event) use ($view) { - return $deviceView->modifyResponse($view, $event->getResponse()); - }; + $this->modifyResponseClosure = static fn (DeviceView $deviceView, ResponseEvent $event): \Symfony\Component\HttpFoundation\Response => $deviceView->modifyResponse($view, $event->getResponse()); } } diff --git a/src/Helper/DeviceView.php b/src/Helper/DeviceView.php index 198dffe..4bb7faf 100644 --- a/src/Helper/DeviceView.php +++ b/src/Helper/DeviceView.php @@ -103,7 +103,7 @@ class DeviceView */ protected $redirectConfig = []; - public function __construct(RequestStack $requestStack = null) + public function __construct(?RequestStack $requestStack = null) { if (!$requestStack || !$this->request = $requestStack->getMainRequest()) { $this->viewType = self::VIEW_NOT_MOBILE; @@ -362,11 +362,7 @@ public function setSwitchParam(string $switchParam): void protected function getStatusCode(string $view): int { - if (isset($this->getRedirectConfig()[$view]['status_code'])) { - return $this->getRedirectConfig()[$view]['status_code']; - } - - return Response::HTTP_FOUND; + return $this->getRedirectConfig()[$view]['status_code'] ?? Response::HTTP_FOUND; } /** @@ -376,7 +372,7 @@ protected function createCookie(string $value): Cookie { try { $expire = new \DateTime($this->getCookieExpireDatetimeModifier()); - } catch (\Exception $e) { + } catch (\Exception) { $expire = new \DateTime(self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT); } diff --git a/src/Resources/doc/legacy-versions.md b/src/Resources/doc/legacy-versions.md index 50ebc8c..8b0a20c 100644 --- a/src/Resources/doc/legacy-versions.md +++ b/src/Resources/doc/legacy-versions.md @@ -3,17 +3,16 @@ Installation Composer -------- - -### Latest Symfony >=7.0 +### For Symfony 6.4 & PHP 8.2 ```sh -composer require tattali/mobile-detect-bundle +composer require 'digifa/mobile-detect-bundle:2.3.*-lts' ``` -### Latest Symfony >= 5.1 || <= 6.4 +### Latest Symfony >= 5.1 || < 6.4 ```sh -composer require 'tattali/mobile-detect-bundle:2.2.*' +composer require tattali/mobile-detect-bundle ``` ### For Symfony >= 3.4 || <= 4.4 diff --git a/src/Twig/Extension/MobileDetectExtension.php b/src/Twig/Extension/MobileDetectExtension.php index ac24731..3cdb2a0 100644 --- a/src/Twig/Extension/MobileDetectExtension.php +++ b/src/Twig/Extension/MobileDetectExtension.php @@ -26,32 +26,14 @@ */ class MobileDetectExtension extends AbstractExtension { - /** - * @var MobileDetectorInterface - */ - private $mobileDetector; - - /** - * @var DeviceView - */ - private $deviceView; - - /** - * @var array - */ - private $redirectConf; - /** * @var Request */ private $request; - public function __construct(RequestStack $requestStack, MobileDetectorInterface $mobileDetector, DeviceView $deviceView, array $redirectConf) + public function __construct(RequestStack $requestStack, private readonly MobileDetectorInterface $mobileDetector, private readonly DeviceView $deviceView, private array $redirectConf) { $this->request = $requestStack->getMainRequest(); - $this->mobileDetector = $mobileDetector; - $this->deviceView = $deviceView; - $this->redirectConf = $redirectConf; } /** @@ -60,19 +42,19 @@ public function __construct(RequestStack $requestStack, MobileDetectorInterface public function getFunctions(): array { return [ - new TwigFunction('is_mobile', [$this, 'isMobile']), - new TwigFunction('is_tablet', [$this, 'isTablet']), - new TwigFunction('is_device', [$this, 'isDevice']), - new TwigFunction('is_full_view', [$this, 'isFullView']), - new TwigFunction('is_mobile_view', [$this, 'isMobileView']), - new TwigFunction('is_tablet_view', [$this, 'isTabletView']), - new TwigFunction('is_not_mobile_view', [$this, 'isNotMobileView']), - new TwigFunction('is_ios', [$this, 'isIOS']), - new TwigFunction('is_android_os', [$this, 'isAndroidOS']), - new TwigFunction('is_windows_os', [$this, 'isWindowsOS']), - new TwigFunction('full_view_url', [$this, 'fullViewUrl'], ['is_safe' => ['html']]), - new TwigFunction('device_version', [$this, 'deviceVersion']), - new TwigFunction('rules_list', [$this, 'getRules']), + new TwigFunction('is_mobile', $this->isMobile(...)), + new TwigFunction('is_tablet', $this->isTablet(...)), + new TwigFunction('is_device', $this->isDevice(...)), + new TwigFunction('is_full_view', $this->isFullView(...)), + new TwigFunction('is_mobile_view', $this->isMobileView(...)), + new TwigFunction('is_tablet_view', $this->isTabletView(...)), + new TwigFunction('is_not_mobile_view', $this->isNotMobileView(...)), + new TwigFunction('is_ios', $this->isIOS(...)), + new TwigFunction('is_android_os', $this->isAndroidOS(...)), + new TwigFunction('is_windows_os', $this->isWindowsOS(...)), + new TwigFunction('full_view_url', $this->fullViewUrl(...), ['is_safe' => ['html']]), + new TwigFunction('device_version', $this->deviceVersion(...)), + new TwigFunction('rules_list', $this->getRules(...)), ]; } @@ -82,8 +64,7 @@ public function getRules(): array $this->mobileDetector->getPhoneDevices(), $this->mobileDetector->getTabletDevices(), $this->mobileDetector->getOperatingSystems(), - $this->mobileDetector->getBrowsers(), - $this->mobileDetector->getUtilities() + $this->mobileDetector->getBrowsers() ); } @@ -134,7 +115,7 @@ public function fullViewUrl(bool $addCurrentPathAndQuery = true): ?string } // if fullHost ends with /, skip it since getPathInfo() also starts with / - $result = rtrim($fullHost, '/').$this->request->getPathInfo(); + $result = rtrim((string) $fullHost, '/').$this->request->getPathInfo(); $query = Request::normalizeQueryString(http_build_query($this->request->query->all(), '', '&')); if ($query) { @@ -159,7 +140,7 @@ public function isTablet(): bool */ public function isDevice(string $deviceName): bool { - $magicMethodName = 'is'.strtolower((string) $deviceName); + $magicMethodName = 'is'.strtolower($deviceName); return $this->mobileDetector->{$magicMethodName}(); } diff --git a/symfony.lock b/symfony.lock index 0cc13c1..e324312 100644 --- a/symfony.lock +++ b/symfony.lock @@ -1,6 +1,6 @@ { "php": { - "version": "7.2" + "version": "8.2" }, "symfony/flex": { "version": "1.0", diff --git a/tests/DataCollector/DeviceDataCollectorTest.php b/tests/DataCollector/DeviceDataCollectorTest.php index d2d841c..90d875a 100644 --- a/tests/DataCollector/DeviceDataCollectorTest.php +++ b/tests/DataCollector/DeviceDataCollectorTest.php @@ -36,17 +36,17 @@ final class DeviceDataCollectorTest extends TestCase /** * @var MockObject|RequestStack */ - private $requestStack; + private MockObject $requestStack; /** * @var MockObject|Request */ - private $request; + private MockObject $request; /** * @var MockObject|Response */ - private $response; + private MockObject $response; protected function setUp(): void { @@ -153,7 +153,7 @@ public function testCollectCurrentViewMobileCanUseTablet(): void self::assertFalse($view['isCurrent']); self::assertTrue($view['enabled']); self::assertSame( - sprintf( + \sprintf( 'http://t.testsite.com/base-url/path-info?%s=%s¶m1=value1', $deviceView->getSwitchParam(), DeviceView::VIEW_TABLET @@ -216,7 +216,7 @@ public function testCollectCurrentViewFullCanUseMobile(): void self::assertFalse($view['isCurrent']); self::assertTrue($view['enabled']); self::assertSame( - sprintf( + \sprintf( 'http://t.testsite.com/base-url/path-info?%s=%s¶m1=value1', $deviceView->getSwitchParam(), DeviceView::VIEW_MOBILE @@ -279,7 +279,7 @@ public function testCollectCurrentViewFullCantUseMobile(): void self::assertFalse($view['isCurrent']); self::assertFalse($view['enabled']); self::assertSame( - sprintf( + \sprintf( 'http://testsite.com/base-url/path-info?%s=%s¶m1=value1', $deviceView->getSwitchParam(), DeviceView::VIEW_MOBILE diff --git a/tests/EventListener/RequestResponseListenerTest.php b/tests/EventListener/RequestResponseListenerTest.php index b6d9ed6..ecd96eb 100644 --- a/tests/EventListener/RequestResponseListenerTest.php +++ b/tests/EventListener/RequestResponseListenerTest.php @@ -33,22 +33,22 @@ final class RequestResponseListenerTest extends TestCase /** * @var MockObject|MobileDetectorInterface */ - private $mobileDetector; + private MockObject $mobileDetector; /** * @var MockObject|RequestStack */ - private $requestStack; + private MockObject $requestStack; /** * @var MockObject|Request */ - private $request; + private MockObject $request; /** * @var MockObject|RouterInterface */ - private $router; + private MockObject $router; /** * @var array @@ -95,6 +95,7 @@ public function testHandleRequestHasSwitchParam(): void $deviceView->setRedirectConfig([DeviceView::VIEW_MOBILE => ['status_code' => Response::HTTP_FOUND]]); $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, []); $event = $this->createGetResponseEvent('some content'); + $event->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($event); self::assertFalse($listener->needsResponseModification()); @@ -120,6 +121,7 @@ public function testHandleRequestBis(): void $deviceView->setRedirectConfig([DeviceView::VIEW_MOBILE => ['status_code' => Response::HTTP_FOUND]]); $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, [], false); $event = $this->createGetResponseEvent('some content'); + $event->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($event); self::assertFalse($listener->needsResponseModification()); @@ -152,6 +154,7 @@ public function testHandleRequestHasSwitchParamAndQuery(): void $deviceView->setRedirectConfig([DeviceView::VIEW_MOBILE => ['status_code' => Response::HTTP_FOUND]]); $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $event = $this->createGetResponseEvent('some content'); + $event->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($event); self::assertFalse($listener->needsResponseModification()); @@ -159,7 +162,7 @@ public function testHandleRequestHasSwitchParamAndQuery(): void $response = $event->getResponse(); self::assertInstanceOf(RedirectResponseWithCookie::class, $response); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); - self::assertSame(sprintf( + self::assertSame(\sprintf( 'http://mobilehost.com/?%s=%s&myparam=myvalue', $this->switchParam, DeviceView::VIEW_MOBILE @@ -182,6 +185,7 @@ public function testHandleRequestIsFullView(): void self::assertFalse($deviceView->hasSwitchParam()); self::assertNull($deviceView->getRequestedViewType()); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertTrue($listener->needsResponseModification()); @@ -192,6 +196,7 @@ public function testHandleRequestIsFullView(): void $responseEventResponse = new Response('Full view', Response::HTTP_OK); $responseEvent = $this->createResponseEvent($responseEventResponse); + $responseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleResponse($responseEvent); $modifiedResponse = $responseEvent->getResponse(); @@ -217,6 +222,7 @@ public function testHandleRequestIsNotMobileView(): void self::assertNull($deviceView->getRequestedViewType()); self::assertSame(DeviceView::VIEW_NOT_MOBILE, $deviceView->getViewType()); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertFalse($listener->needsResponseModification()); @@ -226,6 +232,7 @@ public function testHandleRequestIsNotMobileView(): void $responseEventResponse = new Response('Not mobile view', Response::HTTP_OK); $responseEvent = $this->createResponseEvent($responseEventResponse); + $responseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleResponse($responseEvent); $modifiedResponse = $responseEvent->getResponse(); @@ -253,6 +260,7 @@ public function testHandleRequestHasTabletRedirect(): void $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertFalse($deviceView->hasSwitchParam()); @@ -262,7 +270,7 @@ public function testHandleRequestHasTabletRedirect(): void $response = $getResponseEvent->getResponse(); self::assertInstanceOf(RedirectResponseWithCookie::class, $response); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); - self::assertSame(sprintf( + self::assertSame(\sprintf( 'http://t.testsite.com/some/parameters?%s=%s&some=param', $this->switchParam, DeviceView::VIEW_TABLET @@ -296,6 +304,7 @@ public function testHandleRequestWithDifferentSwitchParamRedirect(): void $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertFalse($deviceView->hasSwitchParam()); @@ -305,7 +314,7 @@ public function testHandleRequestWithDifferentSwitchParamRedirect(): void $response = $getResponseEvent->getResponse(); self::assertInstanceOf(RedirectResponseWithCookie::class, $response); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); - self::assertSame(sprintf( + self::assertSame(\sprintf( 'http://testsite.com/some/parameters?%s=%s&some=param', $switchParam, DeviceView::VIEW_TABLET @@ -331,6 +340,7 @@ public function testHandleDeviceIsTabletAndTabletRedirectIsDisabledAndDetectTabl $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertTrue($listener->needsResponseModification()); @@ -342,6 +352,7 @@ public function testHandleDeviceIsTabletAndTabletRedirectIsDisabledAndDetectTabl $responseEventResponse = new Response('Tablet view', Response::HTTP_OK); $responseEvent = $this->createResponseEvent($responseEventResponse); + $responseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleResponse($responseEvent); $modifiedResponse = $responseEvent->getResponse(); @@ -374,6 +385,7 @@ public function testHandleDeviceIsTabletAsMobileAndTabletRedirectIsDisabledAndDe $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertFalse($listener->needsResponseModification()); @@ -384,7 +396,7 @@ public function testHandleDeviceIsTabletAsMobileAndTabletRedirectIsDisabledAndDe self::assertInstanceOf(RedirectResponseWithCookie::class, $response); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); - self::assertSame(sprintf( + self::assertSame(\sprintf( 'http://mobilehost.com/some/parameters?%s=%s', $this->switchParam, DeviceView::VIEW_MOBILE @@ -414,6 +426,7 @@ public function testHandleRequestHasTabletRedirectWithoutPath(): void $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertFalse($listener->needsResponseModification()); @@ -425,7 +438,7 @@ public function testHandleRequestHasTabletRedirectWithoutPath(): void self::assertInstanceOf(RedirectResponseWithCookie::class, $response); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); self::assertSame('http://testsite.com?device_view=tablet', $response->getTargetUrl()); - self::assertSame(sprintf( + self::assertSame(\sprintf( 'http://testsite.com?%s=%s', $this->switchParam, DeviceView::VIEW_TABLET @@ -455,6 +468,7 @@ public function testHandleRequestHasTabletNoRedirect(): void $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertTrue($listener->needsResponseModification()); @@ -466,6 +480,7 @@ public function testHandleRequestHasTabletNoRedirect(): void $responseEventResponse = new Response('Tablet view no redirect', Response::HTTP_OK); $responseEvent = $this->createResponseEvent($responseEventResponse); + $responseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleResponse($responseEvent); $modifiedResponse = $responseEvent->getResponse(); @@ -498,6 +513,7 @@ public function testHandleRequestHasMobileRedirect(): void $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertFalse($listener->needsResponseModification()); @@ -508,7 +524,7 @@ public function testHandleRequestHasMobileRedirect(): void self::assertInstanceOf(RedirectResponseWithCookie::class, $response); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); - self::assertSame(sprintf( + self::assertSame(\sprintf( 'http://testsite.com/some/parameters?%s=%s', $this->switchParam, DeviceView::VIEW_MOBILE @@ -539,6 +555,7 @@ public function testHandleRequestHasMobileRedirectWithoutPath(): void $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertFalse($listener->needsResponseModification()); @@ -549,7 +566,7 @@ public function testHandleRequestHasMobileRedirectWithoutPath(): void self::assertInstanceOf(RedirectResponseWithCookie::class, $response); self::assertSame(Response::HTTP_FOUND, $response->getStatusCode()); - self::assertSame(sprintf( + self::assertSame(\sprintf( 'http://testsite.com?%s=%s', $this->switchParam, DeviceView::VIEW_MOBILE @@ -580,6 +597,7 @@ public function testHandleRequestHasMobileNoRedirect(): void $listener = new RequestResponseListener($this->mobileDetector, $deviceView, $this->router, $this->config); $getResponseEvent = $this->createGetResponseEvent('some content'); + $getResponseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleRequest($getResponseEvent); self::assertTrue($listener->needsResponseModification()); @@ -591,6 +609,7 @@ public function testHandleRequestHasMobileNoRedirect(): void $responseEventResponse = new Response('Mobile view no redirect', Response::HTTP_OK); $responseEvent = $this->createResponseEvent($responseEventResponse); + $responseEvent->getRequest()->headers->set('user-agent', 'agent'); $listener->handleResponse($responseEvent); $modifiedResponse = $responseEvent->getResponse(); diff --git a/tests/Helper/DeviceViewTest.php b/tests/Helper/DeviceViewTest.php index f03c28b..43293c6 100644 --- a/tests/Helper/DeviceViewTest.php +++ b/tests/Helper/DeviceViewTest.php @@ -6,6 +6,7 @@ use MobileDetectBundle\Helper\DeviceView; use MobileDetectBundle\Helper\RedirectResponseWithCookie; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\InputBag; @@ -23,12 +24,12 @@ final class DeviceViewTest extends TestCase /** * @var MockObject|RequestStack */ - private $requestStack; + private MockObject $requestStack; /** * @var MockObject|Request */ - private $request; + private MockObject $request; private $cookieKey = DeviceView::COOKIE_KEY_DEFAULT; private $switchParam = DeviceView::SWITCH_PARAM_DEFAULT; diff --git a/tests/Twig/Extension/MobileDetectExtensionTest.php b/tests/Twig/Extension/MobileDetectExtensionTest.php index 41cdbb0..750860b 100644 --- a/tests/Twig/Extension/MobileDetectExtensionTest.php +++ b/tests/Twig/Extension/MobileDetectExtensionTest.php @@ -25,17 +25,17 @@ final class MobileDetectExtensionTest extends TestCase /** * @var MockObject|MobileDetector */ - private $mobileDetector; + private MockObject $mobileDetector; /** * @var MockObject|RequestStack */ - private $requestStack; + private MockObject $requestStack; /** * @var MockObject|Request */ - private $request; + private MockObject $request; /** * @var array @@ -68,38 +68,6 @@ protected function setUp(): void ]; } - public function testGetFunctionsArray(): void - { - $deviceView = new DeviceView($this->requestStack); - $extension = new MobileDetectExtension($this->requestStack, $this->mobileDetector, $deviceView, $this->config); - - $functions = $extension->getFunctions(); - self::assertCount(13, $functions); - $names = [ - 'is_mobile' => 'isMobile', - 'is_tablet' => 'isTablet', - 'is_device' => 'isDevice', - 'is_full_view' => 'isFullView', - 'is_mobile_view' => 'isMobileView', - 'is_tablet_view' => 'isTabletView', - 'is_not_mobile_view' => 'isNotMobileView', - 'is_ios' => 'isIOS', - 'is_android_os' => 'isAndroidOS', - 'is_windows_os' => 'isWindowsOS', - 'full_view_url' => 'fullViewUrl', - 'device_version' => 'deviceVersion', - 'rules_list' => 'getRules', - ]; - foreach ($functions as $function) { - self::assertInstanceOf(TwigFunction::class, $function); - $name = $function->getName(); - $callable = $function->getCallable(); - self::assertArrayHasKey($name, $names); - self::assertIsArray($callable); - self::assertSame($names[$name], $callable[1]); - } - } - public function testRulesList(): void { $deviceView = new DeviceView($this->requestStack); @@ -368,4 +336,25 @@ public function testIsWindowsOSFalse(): void $extension = new MobileDetectExtension($this->requestStack, $this->mobileDetector, $deviceView, $this->config); self::assertFalse($extension->isWindowsOS()); } + + public function testGetFunctions(): void + { + $deviceView = new DeviceView($this->requestStack); + $extension = new MobileDetectExtension($this->requestStack, $this->mobileDetector, $deviceView, $this->config); + $functions = $extension->getFunctions(); + + self::assertCount(13, $functions); + self::assertInstanceOf(TwigFunction::class, $functions[0]); + self::assertInstanceOf(TwigFunction::class, $functions[1]); + self::assertInstanceOf(TwigFunction::class, $functions[2]); + self::assertInstanceOf(TwigFunction::class, $functions[3]); + self::assertInstanceOf(TwigFunction::class, $functions[4]); + self::assertInstanceOf(TwigFunction::class, $functions[5]); + self::assertInstanceOf(TwigFunction::class, $functions[6]); + self::assertInstanceOf(TwigFunction::class, $functions[7]); + self::assertInstanceOf(TwigFunction::class, $functions[8]); + self::assertInstanceOf(TwigFunction::class, $functions[9]); + self::assertInstanceOf(TwigFunction::class, $functions[10]); + self::assertInstanceOf(TwigFunction::class, $functions[11]); + } }