From 4921892cc88df5a6e862dc5c2c3fa10a48e2ac9d Mon Sep 17 00:00:00 2001 From: Serban Ghita Date: Sat, 28 Oct 2023 18:49:13 +0300 Subject: [PATCH] [bug] No user-agent has been set #946 --- Dockerfile | 18 ------------ docker-compose.yml | 43 +++++++++++++++++++++++++++++ src/MobileDetect.php | 33 ++++++++++++++++------ tests/MobileDetectGeneralTest.php | 14 ++++++++-- tests/MobileDetectWithCacheTest.php | 9 +++--- 5 files changed, 84 insertions(+), 33 deletions(-) delete mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 7b15f138..00000000 --- a/Dockerfile +++ /dev/null @@ -1,18 +0,0 @@ -FROM composer:latest as setup - -WORKDIR app -COPY . . - -RUN set -xe && composer install -RUN composer -v -RUN ls -al - -FROM php:8.2-fpm - -WORKDIR . -COPY --from=setup app . - -RUN php -v -RUN ls -al app - -RUN cd app && ./vendor/bin/phpunit -v -c tests/phpunit.xml --coverage-text --strict-coverage --stop-on-risky \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..07de81df --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,43 @@ +services: + setup: + image: composer:latest + command: > + /bin/sh -c " + rm -rf vendor && + rm -f composer.lock composer.phar && + set -xe && composer install && + composer -v && + ls -al" + working_dir: /app + volumes: + - .:/app + + runUnitTests: + image: php:8.0.3-fpm-alpine3.13 + working_dir: /app + command: > + /bin/sh -c "vendor/bin/phpunit -v -c tests/phpunit.xml --coverage-text --strict-coverage --stop-on-risky" + ports: + - "8000:8000" + volumes: + - .:/app + + runLinting: + image: php:8.0.3-fpm-alpine3.13 + working_dir: /app + command: > + /bin/sh -c "vendor/bin/phpcs; vendor/bin/phpcbf" + ports: + - "8000:8000" + volumes: + - .:/app + + generateModel: + image: php:8.0.3-fpm-alpine3.13 + working_dir: /app + command: > + /bin/sh -c "php ./scripts/export_to_json.php" + ports: + - "8000:8000" + volumes: + - .:/app diff --git a/src/MobileDetect.php b/src/MobileDetect.php index 4245808d..c74a9e39 100644 --- a/src/MobileDetect.php +++ b/src/MobileDetect.php @@ -19,7 +19,7 @@ * @author Nick Ilyin * @author: Victor Stanciu (original author) * - * @version 4.8.03 + * @version 4.8.04 */ declare(strict_types=1); @@ -236,7 +236,7 @@ class MobileDetect /** * Stores the version number of the current release. */ - protected string $VERSION = '4.8.03'; + protected string $VERSION = '4.8.04'; protected array $config = [ // Auto-initialization on HTTP headers from $_SERVER['HTTP...'] @@ -1210,12 +1210,12 @@ private function prepareUserAgent(string $userAgent): string * Set the User-Agent to be used. * * @param string $userAgent The User-Agent string. - * @return string|null + * @return string */ - public function setUserAgent(string $userAgent): string|null + public function setUserAgent(string $userAgent): string { $preparedUserAgent = $this->prepareUserAgent($userAgent); - return $this->userAgent = !empty($preparedUserAgent) ? $preparedUserAgent : null; + return $this->userAgent = $preparedUserAgent; } /** @@ -1230,7 +1230,12 @@ public function getUserAgent(): ?string public function hasUserAgent(): bool { - return \is_string($this->userAgent) && !empty($this->userAgent); + return is_string($this->userAgent); + } + + public function isUserAgentEmpty(): bool + { + return $this->hasUserAgent() && $this->userAgent === ''; } public function getMatchingRegex(): ?string @@ -1366,7 +1371,11 @@ public function __call(string $name, array $arguments) public function isMobile(): bool { if (!$this->hasUserAgent()) { - throw new MobileDetectException('No user-agent has been set.'); + throw new MobileDetectException('No valid user-agent has been set.'); + } + + if ($this->isUserAgentEmpty()) { + return false; } // Cache check. @@ -1411,6 +1420,10 @@ public function isTablet(): bool throw new MobileDetectException('No user-agent has been set.'); } + if ($this->isUserAgentEmpty()) { + return false; + } + // Cache check. try { $cacheKey = $this->createCacheKey("tablet"); @@ -1476,6 +1489,10 @@ public function is(string $ruleName): bool throw new MobileDetectException('No user-agent has been set.'); } + if ($this->isUserAgentEmpty()) { + return false; + } + // Cache check. try { $cacheKey = $this->createCacheKey($ruleName); @@ -1671,7 +1688,7 @@ public static function flattenHeaders(array $httpHeaders): string { $key = ''; foreach ($httpHeaders as $name => $value) { - $key .= "$name: $value" . "\n"; + $key .= "$name: $value" . PHP_EOL; } return trim($key); } diff --git a/tests/MobileDetectGeneralTest.php b/tests/MobileDetectGeneralTest.php index 7dcfd5fa..001b846e 100644 --- a/tests/MobileDetectGeneralTest.php +++ b/tests/MobileDetectGeneralTest.php @@ -30,12 +30,22 @@ public function testBadMethodCall() public function testNoUserAgentSet() { $this->expectException(\Exception::class); - $this->expectExceptionMessage('No user-agent has been set.'); + $this->expectExceptionMessage('No valid user-agent has been set.'); $detect = new MobileDetect(); $detect->isMobile(); } + /** + * @throws MobileDetectException + */ + public function testEmptyStringAsAUserAgent() + { + $detect = new MobileDetect(); + $detect->setUserAgent(''); + $this->assertFalse($detect->isMobile()); + } + /** * @throws MobileDetectException */ @@ -55,7 +65,7 @@ public function testAutoInitPicksUpKnownHttpHeaders() public function testValidHeadersThatDoNotContainHttpUserAgentHeaderButNoUserAgentIsManuallySet() { $this->expectException(MobileDetectException::class); - $this->expectExceptionMessage('No user-agent has been set.'); + $this->expectExceptionMessage('No valid user-agent has been set.'); $detect = new MobileDetect(); $detect->setHttpHeaders([ diff --git a/tests/MobileDetectWithCacheTest.php b/tests/MobileDetectWithCacheTest.php index a76108bf..e8e95980 100644 --- a/tests/MobileDetectWithCacheTest.php +++ b/tests/MobileDetectWithCacheTest.php @@ -22,11 +22,10 @@ public function testFlattenHeaders() 'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5', ]); - $expectedString = <<assertEquals($expectedString, $cacheKey); }