Added tests
This commit is contained in:
parent
947770715b
commit
7f74b0b144
|
@ -5,7 +5,9 @@
|
|||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
colors="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of sikofitt/user-agent
|
||||
*
|
||||
* Copyleft (C) 2017 http://sikofitt.com sikofitt@gmail.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Sikofitt\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Sikofitt\UserAgent\UserAgent;
|
||||
|
||||
class UserAgentTest extends TestCase
|
||||
{
|
||||
private $zts;
|
||||
private $arch;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->zts = (bool)PHP_ZTS ? '-zts' : '';
|
||||
$this->arch = 9223372036854775807 === PHP_INT_MAX ? 'x86_64' : 'i386';
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testExtendUserAgent(): void
|
||||
{
|
||||
$userAgent = new class extends UserAgent {
|
||||
protected static $format = 'PHP/%d.%d.%d (%d)';
|
||||
protected static $params = [
|
||||
PHP_MAJOR_VERSION,
|
||||
PHP_MINOR_VERSION,
|
||||
PHP_RELEASE_VERSION,
|
||||
PHP_VERSION_ID,
|
||||
];
|
||||
};
|
||||
|
||||
$expected = sprintf(
|
||||
'PHP/%d.%d.%d (%d)',
|
||||
PHP_MAJOR_VERSION,
|
||||
PHP_MINOR_VERSION,
|
||||
PHP_RELEASE_VERSION,
|
||||
PHP_VERSION_ID
|
||||
);
|
||||
$actual = $userAgent::create();
|
||||
$this->assertSame($expected, $actual);
|
||||
}
|
||||
|
||||
public function testFailure(): void
|
||||
{
|
||||
$userAgent = new class extends UserAgent {
|
||||
protected static $format = '';
|
||||
protected static $params = [];
|
||||
};
|
||||
|
||||
$this->assertNotSame(UserAgent::create(), $userAgent::create());
|
||||
$this->assertNotSame(UserAgent::create(), '');
|
||||
}
|
||||
|
||||
public function testDefaultUserAgent(): void
|
||||
{
|
||||
$format = 'PHP/%d.%d%s-%d (%s; %s %s) PHP/%s%s; %s (%s)';
|
||||
$params = [
|
||||
PHP_MAJOR_VERSION,
|
||||
PHP_MINOR_VERSION,
|
||||
$this->zts,
|
||||
PHP_VERSION_ID,
|
||||
'sikofitt/user-agent',
|
||||
PHP_OS_FAMILY,
|
||||
$this->arch,
|
||||
PHP_VERSION,
|
||||
$this->zts,
|
||||
$this->getGuzzleVersionString(),
|
||||
'https://packagist.org/sikofitt/user-agent',
|
||||
];
|
||||
$expected = sprintf($format, ...$params);
|
||||
|
||||
$userAgent = UserAgent::create();
|
||||
$this->assertSame($expected, $userAgent);
|
||||
}
|
||||
|
||||
private function getGuzzleVersionString(): string
|
||||
{
|
||||
/** @noinspection ClassConstantCanBeUsedInspection */
|
||||
if (class_exists('GuzzleHttp\Client')) {
|
||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||
$guzzleClient = new \GuzzleHttp\Client();
|
||||
$guzzleHeaders = $guzzleClient->getConfig('headers');
|
||||
$guzzleUserAgent = $guzzleHeaders['User-Agent'];
|
||||
return trim(explode('php', strtolower($guzzleUserAgent))[0]);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue