1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CrossKnowledge\DeviceDetectBundle\Tests\Services; |
4
|
|
|
|
5
|
|
|
use CrossKnowledge\DeviceDetectBundle\DependencyInjection\CrossKnowledgeDeviceDetectExtension; |
6
|
|
|
use CrossKnowledge\DeviceDetectBundle\Services\DeviceDetect; |
7
|
|
|
use CrossKnowledge\DeviceDetectBundle\Twig\DeviceDetectExtension; |
8
|
|
|
|
9
|
|
|
class DeviceDetectExtensionTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
public function getDetector() |
13
|
|
|
{ |
14
|
|
|
$detectorMock = $this->getMockBuilder(DeviceDetect::class) |
15
|
|
|
->disableOriginalConstructor() |
16
|
|
|
->getMock(); |
17
|
|
|
|
18
|
|
|
return $detectorMock; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function getFunctionByName(\Twig_Extension $extension, $name) |
22
|
|
|
{ |
23
|
|
|
foreach ($extension->getFunctions() as $function) { |
24
|
|
|
if ($function->getName()==$name) { |
25
|
|
|
return $function; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @dataProvider extensionFunctions |
32
|
|
|
*/ |
33
|
|
|
public function testIsTablet($function, $expected, $detectorMethod) |
34
|
|
|
{ |
35
|
|
|
$mock = $this->getDetector(); |
36
|
|
|
|
37
|
|
|
$ext = new DeviceDetectExtension($mock); |
38
|
|
|
|
39
|
|
|
$mock->expects($this->once())->method($detectorMethod) |
40
|
|
|
->will($this->returnValue($expected)); |
41
|
|
|
|
42
|
|
|
$function = $this->getFunctionByName($ext, $function); |
43
|
|
|
$this->assertNotNull($function); |
44
|
|
|
|
45
|
|
|
$this->assertEquals(call_user_func($function->getCallable()), $expected); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function extensionFunctions() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
['is_tablet', true, 'isTablet'], |
52
|
|
|
['is_tablet', false, 'isTablet'], |
53
|
|
|
['is_mobile', false, 'isMobile'], |
54
|
|
|
['is_desktop', false, 'isDesktop'], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
} |