Skip to content

Commit

Permalink
Fixed typings (phpstan) and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Serban Ghita <[email protected]>
  • Loading branch information
serbanghita committed Dec 11, 2024
1 parent 5dfc1cc commit 514c2e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 46 deletions.
3 changes: 1 addition & 2 deletions src/MobileDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,6 @@ public function checkHttpHeadersForMobile(): bool
/**
* Magic overloading method.
*
* @method boolean is[...]()
* @param string $name
* @param array $arguments
* @return bool
Expand Down Expand Up @@ -1711,7 +1710,7 @@ public function version(string $propertyName, string $type = self::VERSION_TYPE_
return false;
}

public function getCache(): Cache
public function getCache(): CacheInterface
{
return $this->cache;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/MobileDetectGeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testBadMethodCall()
{
$this->expectException(\BadMethodCallException::class);
$md = new MobileDetect();
/** @phpstan-ignore-next-line */
$md->badmethodthatdoesntexistatall();
}

Expand Down Expand Up @@ -153,9 +154,12 @@ public function testBasicMethods()
$this->assertTrue($detect->isMobile());
$this->assertFalse($detect->isTablet());

/** @phpstan-ignore-next-line */
$this->assertTrue($detect->isIphone());
/** @phpstan-ignore-next-line */
$this->assertTrue($detect->isiphone());
$this->assertTrue($detect->isiOS());
/** @phpstan-ignore-next-line */
$this->assertTrue($detect->isios());
$this->assertTrue($detect->is('iphone'));
$this->assertTrue($detect->is('ios'));
Expand Down
57 changes: 13 additions & 44 deletions tests/MobileDetectWithCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Detection\Exception\MobileDetectException;
use Detection\MobileDetect;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\InvalidArgumentException;

final class MobileDetectWithCacheTest extends TestCase
{
Expand All @@ -33,6 +34,7 @@ public function testFlattenHeaders()

/**
* @throws MobileDetectException
* @throws InvalidArgumentException
*/
public function testDefaultCacheClassCreatesACacheRecord()
{
Expand All @@ -41,41 +43,17 @@ public function testDefaultCacheClassCreatesACacheRecord()
$isMobile = $detect->isMobile();

$this->assertTrue($isMobile);
$this->assertEquals(1, $detect->getCache()->count());
$this->assertSame(
sha1("mobile:Some iPhone user agent:"),
$detect->getCache()->getKeys()[0]
);
}

/**
* @throws MobileDetectException
*/
public function testDefaultCacheClassCreatesASingleCacheRecordOnMultipleIsMobileCalls()
{
$detect = new MobileDetect();
$detect->setUserAgent('Some iPhone user agent');
$isMobile = $detect->isMobile();
$this->assertTrue($isMobile);
$this->assertEquals(1, $detect->getCache()->count());

$isMobile = $detect->isMobile();
$this->assertTrue($isMobile);
$this->assertEquals(1, $detect->getCache()->count());

$detect->isMobile();
$detect->isMobile();
$detect->isMobile();
$this->assertEquals(1, $detect->getCache()->count());
$this->assertTrue($detect->getCache()->has(sha1("mobile:Some iPhone user agent:")));
}

/**
* @throws MobileDetectException
*/
public function testDefaultCacheClassCreatesMultipleCacheRecordsForAllCalls()
{
$userAgent = 'iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5';
$detect = new MobileDetect();
$detect->setUserAgent('iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5');
$detect->setUserAgent($userAgent);

$isMobile = $detect->isMobile();
$isTablet = $detect->isTablet();
Expand All @@ -96,23 +74,14 @@ public function testDefaultCacheClassCreatesMultipleCacheRecordsForAllCalls()
$this->assertTrue($isiOS);
$this->assertTrue($isiOS2);

$this->assertEquals(4, $detect->getCache()->count());
$this->assertSame(
sha1("mobile:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[0]
);
$this->assertSame(
sha1("tablet:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[1]
);
$this->assertSame(
sha1("iPad:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[2]
);
$this->assertSame(
sha1("iOS:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"),
$detect->getCache()->getKeys()[3]
);
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("mobile:$userAgent:")));
$this->assertTrue($detect->getCache()->get(sha1("mobile:$userAgent:"))->get());
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("tablet:$userAgent:")));
$this->assertTrue($detect->getCache()->get(sha1("tablet:$userAgent:"))->get());
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("iPad:$userAgent:")));
$this->assertTrue($detect->getCache()->get(sha1("iPad:$userAgent:"))->get());
$this->assertInstanceOf(CacheItem::class, $detect->getCache()->get(sha1("iOS:$userAgent:")));
$this->assertTrue($detect->getCache()->get(sha1("iOS:$userAgent:"))->get());
}

/**
Expand Down

0 comments on commit 514c2e9

Please sign in to comment.