Skip to content

Commit

Permalink
mocking some testing and adding metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaDafinser committed Nov 15, 2015
1 parent 6223489 commit 678104f
Show file tree
Hide file tree
Showing 19 changed files with 554 additions and 142 deletions.
59 changes: 58 additions & 1 deletion src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,71 @@

abstract class AbstractProvider
{
private $version;

/**
* Return the name of the provider
*
*
* @return string
*/
abstract public function getName();

abstract public function getComposerPackageName();

/**
* Return the version of the provider
*
* @return string null
*/
public function getVersion()
{
if ($this->version !== null) {
return $this->version;
}

if ($this->getComposerPackageName() === null) {
return;
}

$packages = $this->getComposerPackages();

if ($packages === null) {
return;
}

foreach ($packages as $package) {
if ($package->name === $this->getComposerPackageName()) {
$this->version = $package->version;

break;
}
}

return $this->version;
}

/**
*
* @return \stdClass null
*/
private function getComposerPackages()
{
if (! file_exists('composer.lock')) {
return;
}

$content = file_get_contents('composer.lock');
if ($content === false) {
return;
}

$content = json_decode($content);

return $content->packages;
}

/**
*
* @param string $userAgent
* @param array $headers
*
Expand Down
11 changes: 11 additions & 0 deletions src/Provider/BrowscapPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function getName()
return 'BrowscapPhp';
}

public function getComposerPackageName()
{
return 'browscap/browscap-php';
}

public function getVersion()
{
return $this->getParser()->getCache()->getVersion();
}

/**
*
* @param AdapterInterface $cache
Expand All @@ -42,6 +52,7 @@ public function getCache()
}

/**
* Initial needed for uniTest mocking
*
* @param Browscap $parser
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Provider/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function getName()
return 'Chain';
}

public function getComposerPackageName()
{
return;
}

/**
*
* @return AbstractProvider[]
Expand Down
5 changes: 5 additions & 0 deletions src/Provider/DonatjUAParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function getName()
return 'DonatjUAParser';
}

public function getComposerPackageName()
{
return 'donatj/phpuseragentparser';
}

/**
* @param array $resultRaw
*
Expand Down
83 changes: 51 additions & 32 deletions src/Provider/PiwikDeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function getName()
return 'PiwikDeviceDetector';
}

public function getComposerPackageName()
{
return 'piwik/device-detector';
}

/**
*
* @param CacheProvider $cache
Expand All @@ -45,6 +50,16 @@ public function getCache()
return $this->cache;
}

/**
* Initial needed for uniTest mocking
*
* @param DeviceDetector $parser
*/
public function setParser(DeviceDetector $parser)
{
$this->parser = $parser;
}

/**
*
* @return DeviceDetector
Expand Down Expand Up @@ -78,18 +93,20 @@ private function hasResult(DeviceDetector $dd)
if ($dd->isBot() === true) {
$bot = $dd->getBot();

if ($bot['name'] === DeviceDetector::UNKNOWN) {
if ($bot['name'] === null || $bot['name'] === DeviceDetector::UNKNOWN) {
return false;
}

return true;
}

if ($dd->getClient('name') !== DeviceDetector::UNKNOWN) {
$client = $dd->getClient();
if (isset($client['name']) && $this->isRealResult($client['name'])) {
return true;
}

if ($dd->getOs('name') !== DeviceDetector::UNKNOWN) {
$os = $dd->getOs();
if (isset($os['name']) && $this->isRealResult($os['name'])) {
return true;
}

Expand All @@ -100,6 +117,25 @@ private function hasResult(DeviceDetector $dd)
return false;
}

/**
*
* @param mixed $value
*
* @return bool
*/
private function isRealResult($value)
{
if ($value === '' || $value === null) {
return false;
}

if ($value === DeviceDetector::UNKNOWN) {
return false;
}

return true;
}

/**
*
* @param DeviceDetector $dd
Expand Down Expand Up @@ -157,25 +193,6 @@ private function getResultRaw(DeviceDetector $dd)
return $raw;
}

/**
*
* @param mixed $value
*
* @return bool
*/
private function isRealResult($value)
{
if ($value === '') {
return false;
}

if ($value === DeviceDetector::UNKNOWN) {
return false;
}

return true;
}

public function parse($userAgent, array $headers = [])
{
$dd = $this->getParser();
Expand Down Expand Up @@ -219,34 +236,36 @@ public function parse($userAgent, array $headers = [])
*/
$browser = $result->getBrowser();

if ($this->isRealResult($dd->getClient('name')) === true) {
$browser->setName($dd->getClient('name'));
$ddClient = $dd->getClient();
if (isset($ddClient['name']) && $this->isRealResult($ddClient['name']) === true) {
$browser->setName($ddClient['name']);
}

if ($this->isRealResult($dd->getClient('version')) === true) {
$browser->getVersion()->setComplete($dd->getClient()['version']);
if (isset($ddClient['version']) && $this->isRealResult($ddClient['version']) === true) {
$browser->getVersion()->setComplete($ddClient['version']);
}

/*
* renderingEngine
*/
$renderingEngine = $result->getRenderingEngine();

if ($this->isRealResult($dd->getClient('engine')) === true) {
$renderingEngine->setName($dd->getClient('engine'));
if (isset($ddClient['engine']) && $this->isRealResult($ddClient['engine']) === true) {
$renderingEngine->setName($ddClient['engine']);
}

/*
* operatingSystem
*/
$operatingSystem = $result->getOperatingSystem();

if ($this->isRealResult($dd->getOs('name')) === true) {
$operatingSystem->setName($dd->getOs('name'));
$ddOs = $dd->getOs();
if (isset($ddOs['name']) && $this->isRealResult($ddOs['name']) === true) {
$operatingSystem->setName($ddOs['name']);
}

if ($this->isRealResult($dd->getOs('version')) === true) {
$operatingSystem->getVersion()->setComplete($dd->getOs()['version']);
if (isset($ddOs['version']) && $this->isRealResult($ddOs['version']) === true) {
$operatingSystem->getVersion()->setComplete($ddOs['version']);
}

/*
Expand Down
15 changes: 15 additions & 0 deletions src/Provider/UAParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ public function getName()
return 'UAParser';
}

public function getComposerPackageName()
{
return 'ua-parser/uap-php';
}

/**
* Initial needed for uniTest mocking
*
* @param Parser $parser
*/
public function setParser(Parser $parser)
{
$this->parser = $parser;
}

/**
* @return Parser
*/
Expand Down
11 changes: 8 additions & 3 deletions src/Provider/WhichBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public function getName()
return 'WhichBrowser';
}

public function getComposerPackageName()
{
return 'whichbrowser/whichbrowser';
}

/**
*
* @param array $resultRaw
Expand Down Expand Up @@ -101,7 +106,7 @@ private function isMobile(array $resultRaw)
*
* @return string
*/
private function getVersion($versionPart)
private function getVersionString($versionPart)
{
if (! is_array($versionPart)) {
return $versionPart;
Expand Down Expand Up @@ -171,7 +176,7 @@ public function parse($userAgent, array $headers = [])
}

if (isset($resultRaw['browser']['version'])) {
$browser->getVersion()->setComplete($this->getVersion($resultRaw['browser']['version']));
$browser->getVersion()->setComplete($this->getVersionString($resultRaw['browser']['version']));
}

/*
Expand All @@ -197,7 +202,7 @@ public function parse($userAgent, array $headers = [])
}

if (isset($resultRaw['os']['version'])) {
$operatingSystem->getVersion()->setComplete($this->getVersion($resultRaw['os']['version']));
$operatingSystem->getVersion()->setComplete($this->getVersionString($resultRaw['os']['version']));
}

/*
Expand Down
5 changes: 5 additions & 0 deletions src/Provider/Woothee.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public function getName()
return 'Woothee';
}

public function getComposerPackageName()
{
return 'woothee/woothee';
}

/**
*
* @return \Woothee\Classifier
Expand Down
5 changes: 5 additions & 0 deletions src/Provider/YzalisUAParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public function getName()
return 'YzalisUAParser';
}

public function getComposerPackageName()
{
return 'yzalis/ua-parser';
}

/**
*
* @return \UAParser\UAParser
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/Model/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function testComplete()
$version->setComplete(null);
$this->assertNull($version->getComplete());

// 0.0 gets filtered
$version->setComplete('0.0');
$this->assertNull($version->getComplete());

$version->setComplete('2.0.1');
$this->assertEquals('2.0.1', $version->getComplete());
$this->assertEquals(2, $version->getMajor());
Expand Down
Loading

0 comments on commit 678104f

Please sign in to comment.