Completed
Pull Request — master (#21)
by Thomas
09:06
created

BrowscapTest::setUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
namespace Browscap\BrowscapBundle\Tests;
4
5
use phpbrowscap\Browscap;
6
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class BrowscapTest extends WebTestCase
11
{
12
    /**
13
     * @var \phpbrowscap\Browscap
14
     */
15
    private static $browscap;
16
17
    protected static function createKernel(array $options = array())
18
    {
19
        $env = @$options['env'] ?: 'test';
20
21
        return new AppKernel($env, true);
22
    }
23
24
    /**
25
     * This method is called before the first test of this test class is run.
26
     *
27
     * @since Method available since Release 3.4.0
28
     */
29
    public static function setUpBeforeClass()
30
    {
31
        $cacheDir = sys_get_temp_dir() . '/BrowscapBundle/';
32
        $fs = new Filesystem();
33
        $fs->remove($cacheDir);
34
35
        /** @var ContainerInterface $container */
36
        $container = static::createClient()->getContainer();
37
38
        /* @var $bc \Browscap\BrowscapBundle\Browscap */
39
        $bc = $container->get('browscap');
40
        $bc->updateMethod = Browscap::UPDATE_LOCAL;
41
        $bc->localFile = 'resources/browscap.ini';
42
        $bc->cacheDir = $cacheDir;
43
        $bc->updateCache();
44
45
        // Now, load an INI file into phpbrowscap\Browscap for testing the UAs
46
        self::$browscap = $bc;
47
    }
48
49
    public function testService()
50
    {
51
        self::assertInstanceOf('Browscap\BrowscapBundle\Browscap', self::$browscap);
52
    }
53
54
    /**
55
     * @dataProvider getBrowsers
56
     *
57
     * @param string $userAgent
58
     * @param array  $expectedProperties
59
     */
60
    public function testBrowsers($userAgent, array $expectedProperties)
61
    {
62
        if (!is_array($expectedProperties) || !count($expectedProperties)) {
63
            self::markTestSkipped('Could not run test - no properties were defined to test');
64
        }
65
66
        $actualProps = (array) self::$browscap->getBrowser($userAgent);
67
68
        foreach ($expectedProperties as $propName => $propValue) {
69
            self::assertArrayHasKey(
70
                $propName,
71
                $actualProps,
72
                'Actual properties did not have "' . $propName . '" property'
73
            );
74
75
            self::assertSame(
76
                $propValue,
77
                $actualProps[$propName],
78
                'Expected actual "' . $propName . '" to be "' . $propValue . '" (was "' . $actualProps[$propName]
79
                . '"; used pattern: ' . $actualProps['browser_name_pattern'] . ')'
80
            );
81
        }
82
    }
83
84
    /**
85
     * Data Provider for testBrowsers
86
     */
87
    public function getBrowsers()
88
    {
89
        return array(
90
            array(
91
                'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/24.0',
92
                array(
93
                    'Parent' => 'Firefox 24.0',
94
                    'Platform' => 'Ubuntu',
95
                    'isMobileDevice' => false,
96
                ),
97
            ),
98
            array(
99
                'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)',
100
                array(
101
                    'Parent' => 'IE 10.0 for Desktop',
102
                    'Platform' => 'Win7',
103
                    'isMobileDevice' => false,
104
                ),
105
            ),
106
            array(
107
                'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
108
                array(
109
                    'Parent' => 'Android Browser 4.0',
110
                    'Platform' => 'Android',
111
                    'isMobileDevice' => true,
112
                ),
113
            ),
114
        );
115
    }
116
}
117