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