Testing

James Titcumb edited this page May 8, 2014 · 3 revisions

Unit Test Suite

We have a unit test suite that is designed to verify that the output the tool generates is what we expect it to generate.

Generation Features

The most important tests are run by the BrowscapIniGeneratorTest->testGenerateFeatures method, which as the name implies, tests the features for the generate() method. This test is run for all the feature-*.json files in the tests/fixtures/ua folder, and it verifies that the generated INI file looks like the relative file in the tests/fixtures/ini folder.

Integration Testing

We also have an integration test which is intended to prevent any regressions in the files. To verify that a UA string gives the intended result, you can add a specification into the tests/fixtures/issues folder as an array. The format of the array is as such:

<?php

return [
    'descriptive-unique-name' => [
        'user-agent-to-test',
        [
            'Property1' => 'Value1',
            'Property2' => 'Value2',
        ],
    ],
];

Put into plain English, this test verifies that the UA definition matched by user-agent-to-test has Property1 and Property2 and that they have Value1 and Value2 as respective values.

For example, the following test verifies that the UA string below evaluates to Chrome 27.0 on Android 4.2 and that it is flagged as a mobile device. Note that the descriptive name is issue-7 as this bug was fixed for #7, so if this test were to break in the future, we can see exactly which one failed. This test can be seen here

<?php

return [
    'issue-7' => [
        'Mozilla/5.0 (Linux; Android 4.2.2; Nexus 4 Build/JDQ39E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Mobile Safari/537.36',
        [
            'Browser' => 'Chrome',
            'Version' => '27.0',
            'Platform' => 'Android',
            'Platform_Version' => '4.2',
            'isMobileDevice' => true,
        ],
    ],
];

The test itself is run in BrowscapTest\UserAgentsInfo. This test automatically runs a build and stores the generated INI files in the /tmp directory, and then uses the phpbrowscap library to load it and match the UAs specified in TestUserAgents.php.