Skip to content

Commit

Permalink
Merge pull request #73 from ThaDafinser/feature/integration
Browse files Browse the repository at this point in the history
http integration test + fix hhvm
  • Loading branch information
ThaDafinser committed Mar 23, 2016
2 parents 8e566a5 + f9aac24 commit ef13d3a
Show file tree
Hide file tree
Showing 17 changed files with 975 additions and 4 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ Network Trash Folder
Temporary Items
.apdisk

/vendor/
vendor/
.settings
.buildpath
build
.project
.tmp
.php_cs.cache
composer.lock
composer.lock
tests/client.php
tests/credentials.php
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ So you can
- switch between different parsers, without changing your code
- compare the result of the different parsers
- get always the same result model, regardless of which parser you use currently
- be sure that it will work
- we got over 350 unit tests and over 70 integration tests
- plus we test the lowest and highest dependency


## Try it out
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Http/UserAgentApiCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected function getResult($userAgent, array $headers)
}

if (isset($content->error->code) && $content->error->code == 'useragent_invalid') {
throw new Exception\RequestException('User agent is invalid ' . $userAgent);
throw new Exception\RequestException('User agent is invalid "' . $userAgent . '"');
}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/client.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;

$handler = new CurlHandler();
$stack = HandlerStack::create($handler);

$client = new Client([
'handler' => $stack,

'timeout' => 10,

'curl' => [
CURLOPT_PROXY => 'YOUR_PROXY',
CURLOPT_PROXYUSERPWD => '...',
CURLOPT_PROXYAUTH => CURLAUTH_NTLM,
CURLOPT_PROXYTYPE => CURLPROXY_HTTP,

CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
]
]);

return $client;
13 changes: 13 additions & 0 deletions tests/credentials.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
define('CREDENTIALS_FILE_LOADED', true);

define('CREDENTIALS_DEVICE_ATLAS_COM_KEY', '...');

define('CREDENTIALS_NEUTRINO_API_COM_USER_ID', '...');
define('CREDENTIALS_NEUTRINO_API_COM_KEY', '...');

define('CREDENTIALS_UDGER_COM_KEY', '...');

define('CREDENTIALS_USER_AGENT_API_COM_KEY', '...');

define('CREDENTIALS_WHAT_IS_MY_BROWSER_COM_KEY', '...');
13 changes: 13 additions & 0 deletions tests/integration/Provider/BrowscapFullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,13 @@ public function testRealResultBot()
],
], $result->toArray());

/*
* Test the raw result
*/
$rawResult = $result->getProviderResultRaw();

$this->assertInstanceOf('stdClass', $rawResult);
$this->assertCount(50, (array) $rawResult);

$this->assertObjectHasAttribute('browser_name_regex', $rawResult);
$this->assertObjectHasAttribute('parent', $rawResult);
Expand Down Expand Up @@ -187,5 +192,13 @@ public function testRealResultDevice()
'type' => null,
],
], $result->toArray());

/*
* Test the raw result
*/
$rawResult = $result->getProviderResultRaw();

$this->assertInstanceOf('stdClass', $rawResult);
$this->assertCount(50, (array) $rawResult);
}
}
1 change: 1 addition & 0 deletions tests/integration/Provider/DonatjUAParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function testRealResult()
* Test the raw result
*/
$rawResult = $result->getProviderResultRaw();

$this->assertEquals([
'platform' => 'Chrome OS',
'browser' => 'Chrome',
Expand Down
57 changes: 57 additions & 0 deletions tests/integration/Provider/Http/AbstractHttpProviderTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace UserAgentParserTest\Integration\Provider\Http;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use UserAgentParserTest\Integration\Provider\AbstractProviderTestCase;

abstract class AbstractHttpProviderTestCase extends AbstractProviderTestCase
{
private $client;

public function setUp()
{
/*
* move tests/credentials.php.dist to tests/credentials.php
*/
if (! defined('CREDENTIALS_FILE_LOADED') && file_exists('tests/credentials.php')) {
include 'tests/credentials.php';
}

/*
* If you need an alternativ client to test the integration -> move test/client.php.dist to test/client.php and define your things!
*/
if (file_exists('tests/client.php')) {
$client = include 'tests/client.php';

if ($client instanceof Client) {
$this->client = $client;
}
}
}

/**
*
* @return Client
*/
protected function getClient()
{
if ($this->client === null) {
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);

$this->client = new Client([
'handler' => $stack,
'timeout' => 5,

'curl' => [
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
],
]);
}

return $this->client;
}
}
112 changes: 112 additions & 0 deletions tests/integration/Provider/Http/DeviceAtlasComTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
namespace UserAgentParserTest\Integration\Provider\Http;

use UserAgentParser\Provider\Http\DeviceAtlasCom;

/**
* @coversNothing
*/
class DeviceAtlasComTest extends AbstractHttpProviderTestCase
{
/**
* @expectedException \UserAgentParser\Exception\InvalidCredentialsException
* @expectedExceptionMessage Your API key "invalid_api_key" is not valid for DeviceAtlasCom
*/
public function testInvalidCredentials()
{
$provider = new DeviceAtlasCom($this->getClient(), 'invalid_api_key');

$result = $provider->parse('...');
}

/**
* @expectedException \UserAgentParser\Exception\NoResultFoundException
*/
public function testNoResultFound()
{
if (! defined('CREDENTIALS_DEVICE_ATLAS_COM_KEY')) {
$this->markTestSkipped('no credentials available. Please provide tests/credentials.php');
}

$provider = new DeviceAtlasCom($this->getClient(), CREDENTIALS_DEVICE_ATLAS_COM_KEY);

$result = $provider->parse('...');
}

public function testRealResultDevice()
{
if (! defined('CREDENTIALS_DEVICE_ATLAS_COM_KEY')) {
$this->markTestSkipped('no credentials available. Please provide tests/credentials.php');
}

$provider = new DeviceAtlasCom($this->getClient(), CREDENTIALS_DEVICE_ATLAS_COM_KEY);

$result = $provider->parse('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3');
$this->assertEquals([
'browser' => [
'name' => 'Safari',
'version' => [
'major' => 5,
'minor' => 1,
'patch' => null,

'alias' => null,

'complete' => '5.1',
],
],
'renderingEngine' => [
'name' => 'WebKit',
'version' => [
'major' => null,
'minor' => null,
'patch' => null,

'alias' => null,

'complete' => null,
],
],
'operatingSystem' => [
'name' => 'iOS',
'version' => [
'major' => 5,
'minor' => 0,
'patch' => null,

'alias' => '_',

'complete' => '5_0',
],
],
'device' => [
'model' => null,
'brand' => null,
'type' => 'Mobile Phone',

'isMobile' => null,
'isTouch' => null,
],
'bot' => [
'isBot' => null,
'name' => null,
'type' => null,
],
], $result->toArray());

/*
* Test the raw result
*/
$rawResult = $result->getProviderResultRaw();

$this->assertInstanceOf('stdClass', $rawResult);
$this->assertCount(6, (array) $rawResult);

$this->assertObjectHasAttribute('browserVersion', $rawResult);
$this->assertObjectHasAttribute('osVersion', $rawResult);
$this->assertObjectHasAttribute('browserName', $rawResult);
$this->assertObjectHasAttribute('primaryHardwareType', $rawResult);
$this->assertObjectHasAttribute('browserRenderingEngine', $rawResult);
$this->assertObjectHasAttribute('osName', $rawResult);
}
}
Loading

0 comments on commit ef13d3a

Please sign in to comment.