add tests
This commit is contained in:
bovenliggende
d3af1b7054
commit
ff342fe11d
@ -18,12 +18,18 @@ declare(strict_types=1);
|
||||
namespace D3\GuzzleFactory\tests;
|
||||
|
||||
use D3\GuzzleFactory\GuzzleFactory;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use Monolog\Logger;
|
||||
use ReflectionException;
|
||||
|
||||
/**
|
||||
* @coversNothing
|
||||
*/
|
||||
class GuzzleFactoryTest extends ApiTestCase
|
||||
{
|
||||
use HeaderTestTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @return void
|
||||
@ -35,4 +41,56 @@ class GuzzleFactoryTest extends ApiTestCase
|
||||
|
||||
$this->assertInstanceOf(GuzzleFactory::class, $instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getGuzzle
|
||||
*/
|
||||
public function testGetGuzzle(): void
|
||||
{
|
||||
$sutMock = $this->getMockBuilder(GuzzleFactory::class)
|
||||
->onlyMethods(['getStack', 'getContentType', 'getAccept', 'getUserAgent'])
|
||||
->getMock();
|
||||
$sutMock->expects($this->once())->method('getStack');
|
||||
$sutMock->expects($this->once())->method('getContentType');
|
||||
$sutMock->expects($this->once())->method('getAccept');
|
||||
$sutMock->expects($this->once())->method('getUserAgent');
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Client::class,
|
||||
$this->callMethod(
|
||||
$sutMock,
|
||||
'getGuzzle',
|
||||
['https://google.com']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getStack
|
||||
*/
|
||||
public function testGetStack()
|
||||
{
|
||||
$sutMock = $this->getMockBuilder(GuzzleFactory::class)
|
||||
->onlyMethods(['getLoggers', 'getMessageLevel', 'getMessageFormatter'])
|
||||
->getMock();
|
||||
$sutMock->expects($this->once())->method('getLoggers')->willReturn([
|
||||
new Logger('logger1'), new Logger('logger2'),
|
||||
]);
|
||||
$sutMock->expects($this->exactly(2))->method('getMessageLevel')->willReturn(Logger::INFO);
|
||||
$sutMock->expects($this->exactly(2))->method('getMessageFormatter');
|
||||
|
||||
$this->assertInstanceOf(
|
||||
HandlerStack::class,
|
||||
$this->callMethod(
|
||||
$sutMock,
|
||||
'getStack'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
167
tests/HeaderTestTrait.php
Normal file
167
tests/HeaderTestTrait.php
Normal file
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*
|
||||
* https://www.d3data.de
|
||||
*
|
||||
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
namespace D3\GuzzleFactory\tests;
|
||||
|
||||
use D3\GuzzleFactory\GuzzleFactory;
|
||||
use Generator;
|
||||
use ReflectionException;
|
||||
|
||||
trait HeaderTestTrait
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getAccept
|
||||
* @dataProvider getAcceptDataProvider
|
||||
*/
|
||||
public function testGetAccept(string $expected, bool $viaClassProperty): void
|
||||
{
|
||||
$sut = GuzzleFactory::create();
|
||||
|
||||
if ($viaClassProperty) {
|
||||
$this->setValue($sut, 'accept', $expected);
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->callMethod(
|
||||
$sut,
|
||||
'getAccept',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getAcceptDataProvider(): Generator
|
||||
{
|
||||
yield 'default' => ['application/json', false];
|
||||
yield 'via property' => ['application/xml', true];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @param string $expected
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::setAccept
|
||||
* @dataProvider setAcceptDataProvider
|
||||
*/
|
||||
public function testSetAccept(string $expected): void
|
||||
{
|
||||
$sut = GuzzleFactory::create();
|
||||
$sut->setAccept($expected);
|
||||
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getValue($sut, 'accept')
|
||||
);
|
||||
}
|
||||
|
||||
public static function setAcceptDataProvider(): Generator
|
||||
{
|
||||
yield 'default' => ['application/json'];
|
||||
yield 'via property' => ['application/xml'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getContentType
|
||||
* @dataProvider getAcceptDataProvider
|
||||
*/
|
||||
public function testGetContentType(string $expected, bool $viaClassProperty): void
|
||||
{
|
||||
$sut = GuzzleFactory::create();
|
||||
|
||||
if ($viaClassProperty) {
|
||||
$this->setValue($sut, 'contentType', $expected);
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->callMethod(
|
||||
$sut,
|
||||
'getContentType',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @param string $expected
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::setContentType
|
||||
* @dataProvider setAcceptDataProvider
|
||||
*/
|
||||
public function testSetContentType(string $expected): void
|
||||
{
|
||||
$sut = GuzzleFactory::create();
|
||||
$sut->setContentType($expected);
|
||||
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getValue($sut, 'contentType')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getUserAgent
|
||||
* @dataProvider getUserAgentDataProvider
|
||||
*/
|
||||
public function testGetUserAgent(string $expected, bool $viaClassProperty): void
|
||||
{
|
||||
$sut = GuzzleFactory::create();
|
||||
|
||||
if ($viaClassProperty) {
|
||||
$this->setValue($sut, 'userAgent', $expected);
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->callMethod(
|
||||
$sut,
|
||||
'getUserAgent',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function getUserAgentDataProvider(): Generator
|
||||
{
|
||||
yield 'default' => ['GuzzleHttp/7', false];
|
||||
yield 'via property' => ['myApp/1.0.0', true];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @param string $expected
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::setUserAgent
|
||||
* @dataProvider setAcceptDataProvider
|
||||
*/
|
||||
public function testSetUserAgent(string $expected): void
|
||||
{
|
||||
$sut = GuzzleFactory::create();
|
||||
$sut->setUserAgent($expected);
|
||||
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getValue($sut, 'userAgent')
|
||||
);
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
namespace D3\OxidGuzzleFactory\tests\Helpers;
|
||||
namespace D3\GuzzleFactory\tests\Helpers;
|
||||
|
||||
use Monolog\Logger;
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
use D3\OxidGuzzleFactory\tests\Helpers\OxidRegistryStub;
|
||||
use D3\GuzzleFactory\tests\Helpers\OxidRegistryStub;
|
||||
|
||||
const OX_BASE_PATH = __DIR__;
|
||||
|
||||
|
Laden…
x
Verwijs in nieuw issue
Block a user