Page MenuHomeWickedGov Phorge

DeviceDetectorServiceTest.php
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

DeviceDetectorServiceTest.php

<?php
namespace Tests\MobileFrontend\Devices;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Request\WebRequest;
use MobileFrontend\Devices\DeviceDetector;
use MobileFrontend\Devices\DeviceDetectorService;
use MobileFrontend\Devices\DeviceProperties;
/**
* @group MobileFrontend
*/
class DeviceDetectorServiceTest extends \MediaWikiUnitTestCase {
private FauxRequest $request;
protected function setUp(): void {
parent::setUp();
$this->request = new FauxRequest();
}
/**
* Creates a list of child device detectors from a list of results, which are
* then used to create an instance of `CompositeDeviceDetector`.
*
* @param array $results
* @return CompositeDeviceDetector
*/
private function createDetector( array $results ) {
$childFactory = static function ( $result ) {
return new StubDeviceDetector( $result );
};
return new DeviceDetectorService( array_map( $childFactory, $results ) );
}
/**
* @covers \MobileFrontend\Devices\DeviceDetectorService::detectDeviceProperties
* @covers \MobileFrontend\Devices\DeviceDetectorService::__construct
* @covers \MobileFrontend\Devices\DeviceProperties::__construct
*/
public function testItShouldHandleOneChild() {
$expectedProperties = new DeviceProperties( true, false );
$detector = $this->createDetector( [ $expectedProperties ] );
$properties = $detector->detectDeviceProperties( $this->request, [] );
$this->assertSame( $expectedProperties, $properties );
}
/**
* @covers \MobileFrontend\Devices\DeviceDetectorService::detectDeviceProperties
* @covers \MobileFrontend\Devices\DeviceDetectorService::__construct
* @covers \MobileFrontend\Devices\DeviceProperties::__construct
*/
public function testItShouldHandleManyChildren() {
$expectedProperties = new DeviceProperties( true, false );
$detector = $this->createDetector( [
null,
null,
$expectedProperties
] );
$properties = $detector->detectDeviceProperties( $this->request, [] );
$this->assertSame( $expectedProperties, $properties );
}
/**
* @covers \MobileFrontend\Devices\DeviceDetectorService::detectDeviceProperties
* @covers \MobileFrontend\Devices\DeviceDetectorService::__construct
*/
public function testItShouldHandleZeroChildren() {
$detector = $this->createDetector( [] );
$properties = $detector->detectDeviceProperties( $this->request, [] );
$this->assertNull( $properties );
}
}
class StubDeviceDetector implements DeviceDetector {
/** @var DeviceProperties|null */
private $result;
public function __construct( $result ) {
$this->result = $result;
}
public function detectDeviceProperties( WebRequest $request, array $server ) {
return $this->result;
}
}

File Metadata

Mime Type
text/x-php
Expires
Sun, Nov 22, 21:09 (2 d, 1 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
b0/e5/7c35e2ef844f483e5f5c14481882
Default Alt Text
DeviceDetectorServiceTest.php (2 KB)

Event Timeline