Skip to content

Commit 514c2e9

Browse files
committed
Fixed typings (phpstan) and tests
Signed-off-by: Serban Ghita <[email protected]>
1 parent 5dfc1cc commit 514c2e9

File tree

3 files changed

+18
-46
lines changed

3 files changed

+18
-46
lines changed

src/MobileDetect.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,6 @@ public function checkHttpHeadersForMobile(): bool
13701370
/**
13711371
* Magic overloading method.
13721372
*
1373-
* @method boolean is[...]()
13741373
* @param string $name
13751374
* @param array $arguments
13761375
* @return bool
@@ -1711,7 +1710,7 @@ public function version(string $propertyName, string $type = self::VERSION_TYPE_
17111710
return false;
17121711
}
17131712

1714-
public function getCache(): Cache
1713+
public function getCache(): CacheInterface
17151714
{
17161715
return $this->cache;
17171716
}

tests/MobileDetectGeneralTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function testBadMethodCall()
2121
{
2222
$this->expectException(\BadMethodCallException::class);
2323
$md = new MobileDetect();
24+
/** @phpstan-ignore-next-line */
2425
$md->badmethodthatdoesntexistatall();
2526
}
2627

@@ -153,9 +154,12 @@ public function testBasicMethods()
153154
$this->assertTrue($detect->isMobile());
154155
$this->assertFalse($detect->isTablet());
155156

157+
/** @phpstan-ignore-next-line */
156158
$this->assertTrue($detect->isIphone());
159+
/** @phpstan-ignore-next-line */
157160
$this->assertTrue($detect->isiphone());
158161
$this->assertTrue($detect->isiOS());
162+
/** @phpstan-ignore-next-line */
159163
$this->assertTrue($detect->isios());
160164
$this->assertTrue($detect->is('iphone'));
161165
$this->assertTrue($detect->is('ios'));

tests/MobileDetectWithCacheTest.php

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Detection\Exception\MobileDetectException;
88
use Detection\MobileDetect;
99
use PHPUnit\Framework\TestCase;
10+
use Psr\SimpleCache\InvalidArgumentException;
1011

1112
final class MobileDetectWithCacheTest extends TestCase
1213
{
@@ -33,6 +34,7 @@ public function testFlattenHeaders()
3334

3435
/**
3536
* @throws MobileDetectException
37+
* @throws InvalidArgumentException
3638
*/
3739
public function testDefaultCacheClassCreatesACacheRecord()
3840
{
@@ -41,41 +43,17 @@ public function testDefaultCacheClassCreatesACacheRecord()
4143
$isMobile = $detect->isMobile();
4244

4345
$this->assertTrue($isMobile);
44-
$this->assertEquals(1, $detect->getCache()->count());
45-
$this->assertSame(
46-
sha1("mobile:Some iPhone user agent:"),
47-
$detect->getCache()->getKeys()[0]
48-
);
49-
}
50-
51-
/**
52-
* @throws MobileDetectException
53-
*/
54-
public function testDefaultCacheClassCreatesASingleCacheRecordOnMultipleIsMobileCalls()
55-
{
56-
$detect = new MobileDetect();
57-
$detect->setUserAgent('Some iPhone user agent');
58-
$isMobile = $detect->isMobile();
59-
$this->assertTrue($isMobile);
60-
$this->assertEquals(1, $detect->getCache()->count());
61-
62-
$isMobile = $detect->isMobile();
63-
$this->assertTrue($isMobile);
64-
$this->assertEquals(1, $detect->getCache()->count());
65-
66-
$detect->isMobile();
67-
$detect->isMobile();
68-
$detect->isMobile();
69-
$this->assertEquals(1, $detect->getCache()->count());
46+
$this->assertTrue($detect->getCache()->has(sha1("mobile:Some iPhone user agent:")));
7047
}
7148

7249
/**
7350
* @throws MobileDetectException
7451
*/
7552
public function testDefaultCacheClassCreatesMultipleCacheRecordsForAllCalls()
7653
{
54+
$userAgent = 'iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5';
7755
$detect = new MobileDetect();
78-
$detect->setUserAgent('iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5');
56+
$detect->setUserAgent($userAgent);
7957

8058
$isMobile = $detect->isMobile();
8159
$isTablet = $detect->isTablet();
@@ -96,23 +74,14 @@ public function testDefaultCacheClassCreatesMultipleCacheRecordsForAllCalls()
9674
$this->assertTrue($isiOS);
9775
$this->assertTrue($isiOS2);
9876

99-
$this->assertEquals(4, $detect->getCache()->count());
100-
$this->assertSame(
101-
sha1("mobile:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
102-
$detect->getCache()->getKeys()[0]
103-
);
104-
$this->assertSame(
105-
sha1("tablet:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
106-
$detect->getCache()->getKeys()[1]
107-
);
108-
$this->assertSame(
109-
sha1("iPad:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
110-
$detect->getCache()->getKeys()[2]
111-
);
112-
$this->assertSame(
113-
sha1("iOS:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
114-
$detect->getCache()->getKeys()[3]
115-
);
77+
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("mobile:$userAgent:")));
78+
$this->assertTrue($detect->getCache()->get(sha1("mobile:$userAgent:"))->get());
79+
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("tablet:$userAgent:")));
80+
$this->assertTrue($detect->getCache()->get(sha1("tablet:$userAgent:"))->get());
81+
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("iPad:$userAgent:")));
82+
$this->assertTrue($detect->getCache()->get(sha1("iPad:$userAgent:"))->get());
83+
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("iOS:$userAgent:")));
84+
$this->assertTrue($detect->getCache()->get(sha1("iOS:$userAgent:"))->get());
11685
}
11786

11887
/**

0 commit comments

Comments
 (0)