add account + fields endpoint tests
This commit is contained in:
parent
d392aa8ade
commit
6412ebbb35
11
tests/README.md
Normal file
11
tests/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Installation
|
||||
|
||||
```
|
||||
composer create-project -s dev --prefer-source [--repository '{"type": "vcs", "url": "repository url"}'] d3/klicktipp-php-client .
|
||||
```
|
||||
|
||||
# Run tests
|
||||
|
||||
```
|
||||
./vendor/bin/phpunit [--no-coverage] [--coverage-html=cov]
|
||||
```
|
75
tests/TestCase.php
Normal file
75
tests/TestCase.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\KlicktippPhpClient\tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
abstract class TestCase extends PhpUnitTestCase
|
||||
{
|
||||
/**
|
||||
* Calls a private or protected object method.
|
||||
*
|
||||
* @param object $object
|
||||
* @param string $methodName
|
||||
* @param array $arguments
|
||||
*
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function callMethod(object $object, string $methodName, array $arguments = [])
|
||||
{
|
||||
$class = new ReflectionClass($object);
|
||||
$method = $class->getMethod($methodName);
|
||||
$method->setAccessible(true);
|
||||
return $method->invokeArgs($object, $arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a private or protected property in defined class instance
|
||||
*
|
||||
* @param object $object
|
||||
* @param string $valueName
|
||||
* @param $value
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function setValue(object $object, string $valueName, $value): void
|
||||
{
|
||||
$reflection = new ReflectionClass($object);
|
||||
$property = $reflection->getProperty($valueName);
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($object, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* get a private or protected property from defined class instance
|
||||
*
|
||||
* @param object $object
|
||||
* @param string $valueName
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function getValue(object $object, string $valueName)
|
||||
{
|
||||
$reflection = new ReflectionClass($object);
|
||||
$property = $reflection->getProperty($valueName);
|
||||
$property->setAccessible(true);
|
||||
return $property->getValue($object);
|
||||
}
|
||||
}
|
42
tests/integration/IntegrationTestCase.php
Normal file
42
tests/integration/IntegrationTestCase.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?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\KlicktippPhpClient\tests\integration;
|
||||
|
||||
use D3\KlicktippPhpClient\Connection;
|
||||
use D3\KlicktippPhpClient\tests\TestCase;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* @coversNothing
|
||||
*/
|
||||
class IntegrationTestCase extends TestCase
|
||||
{
|
||||
public function getConnectionMock(ResponseInterface $response): Connection
|
||||
{
|
||||
$mock = new MockHandler([$response]);
|
||||
|
||||
$handlerStack = HandlerStack::create($mock);
|
||||
$client = new Client(['handler' => $handlerStack]);
|
||||
|
||||
$connection = new Connection('userName', 'password');
|
||||
$connection->setClient($client);
|
||||
|
||||
return $connection;
|
||||
}
|
||||
}
|
133
tests/integration/Resources/AccountTest.php
Normal file
133
tests/integration/Resources/AccountTest.php
Normal file
@ -0,0 +1,133 @@
|
||||
<?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\KlicktippPhpClient\tests\integration\Resources;
|
||||
|
||||
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
||||
use D3\KlicktippPhpClient\Resources\Account;
|
||||
use D3\KlicktippPhpClient\tests\integration\IntegrationTestCase;
|
||||
use Generator;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use ReflectionException;
|
||||
|
||||
/**
|
||||
* @coversNothing
|
||||
*/
|
||||
class AccountTest extends IntegrationTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\KlicktippPhpClient\Resources\Account::login
|
||||
* @dataProvider loginDataProvider
|
||||
*/
|
||||
public function testLogin(ResponseInterface $response, array $expected, bool $expectException = false): void
|
||||
{
|
||||
$sut = new Account($this->getConnectionMock($response));
|
||||
|
||||
if ($expectException) {
|
||||
$this->expectException(BaseException::class);
|
||||
}
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->callMethod(
|
||||
$sut,
|
||||
'login',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function loginDataProvider(): Generator
|
||||
{
|
||||
yield 'success' => [new Response(200, [], '{
|
||||
"sessid": "nFnp4uLZCnc3dIEl9PrXgjoUuaJSyJIKpLyU9HH5rxw",
|
||||
"session_name": "SSESS5bc3b84d3f2031474d7722e94851cff5",
|
||||
"account": {
|
||||
"uid": "12345",
|
||||
"status": "1",
|
||||
"usergroup": "16",
|
||||
"email": "info@mydomain.com",
|
||||
"username": "KTAPIdev",
|
||||
"firstname": "Developer",
|
||||
"lastname": "Klicktipper",
|
||||
"company": "Customer GmbH",
|
||||
"website": "www.mydomain.de",
|
||||
"street": "Am Hafen 6",
|
||||
"city": "Hamburg",
|
||||
"state": "",
|
||||
"zip": "20123",
|
||||
"country": "DE",
|
||||
"phone": "+49211123456789",
|
||||
"fax": "",
|
||||
"affid": "123456"
|
||||
}
|
||||
}'), [
|
||||
'sessid' => 'nFnp4uLZCnc3dIEl9PrXgjoUuaJSyJIKpLyU9HH5rxw',
|
||||
'session_name' => 'SSESS5bc3b84d3f2031474d7722e94851cff5',
|
||||
'account' => [
|
||||
'uid' => '12345',
|
||||
'status' => '1',
|
||||
'usergroup' => '16',
|
||||
'email' => 'info@mydomain.com',
|
||||
'username' => 'KTAPIdev',
|
||||
'firstname' => 'Developer',
|
||||
'lastname' => 'Klicktipper',
|
||||
'company' => 'Customer GmbH',
|
||||
'website' => 'www.mydomain.de',
|
||||
'street' => 'Am Hafen 6',
|
||||
'city' => 'Hamburg',
|
||||
'state' => '',
|
||||
'zip' => '20123',
|
||||
'country' => 'DE',
|
||||
'phone' => '+49211123456789',
|
||||
'fax' => '',
|
||||
'affid' => '123456',
|
||||
],
|
||||
]];
|
||||
yield 'wrong credentials' => [new Response(401, [], '["Falscher Benutzername oder Passwort."]'), [], true];
|
||||
yield 'already logged in' => [new Response(406, [], '["Sie sind bereits eingeloggt."]'), [], true];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\KlicktippPhpClient\Resources\Account::logout
|
||||
* @dataProvider logoutDataProvider
|
||||
*/
|
||||
public function testLogout(ResponseInterface $response, bool $expected, bool $expectException = false): void
|
||||
{
|
||||
$sut = new Account($this->getConnectionMock($response));
|
||||
|
||||
if ($expectException) {
|
||||
$this->expectException(BaseException::class);
|
||||
}
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->callMethod(
|
||||
$sut,
|
||||
'logout',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function logoutDataProvider(): Generator
|
||||
{
|
||||
yield 'success' => [new Response(200, [], '[true]'), true];
|
||||
yield 'already logged out' => [new Response(406, [], '["Benutzer nicht eingeloggt."]'), false, true];
|
||||
}
|
||||
}
|
95
tests/integration/Resources/FieldTest.php
Normal file
95
tests/integration/Resources/FieldTest.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?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\KlicktippPhpClient\tests\integration\Resources;
|
||||
|
||||
use D3\KlicktippPhpClient\Entities\FieldList;
|
||||
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
||||
use D3\KlicktippPhpClient\Resources\Field;
|
||||
use D3\KlicktippPhpClient\tests\integration\IntegrationTestCase;
|
||||
use Generator;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use ReflectionException;
|
||||
|
||||
/**
|
||||
* @coversNothing
|
||||
*/
|
||||
class FieldTest extends IntegrationTestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\KlicktippPhpClient\Resources\Field::index
|
||||
* @dataProvider indexDataProvider
|
||||
*/
|
||||
public function testIndex(ResponseInterface $response, ?FieldList $expected, bool $expectException = false)
|
||||
{
|
||||
$sut = new Field($this->getConnectionMock($response));
|
||||
|
||||
if ($expectException) {
|
||||
$this->expectException(BaseException::class);
|
||||
}
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->callMethod(
|
||||
$sut,
|
||||
'index',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function indexDataProvider(): Generator
|
||||
{
|
||||
yield 'success' => [new Response(200, [], '{
|
||||
"fieldFirstName": "Vorname",
|
||||
"fieldLastName": "Nachname",
|
||||
"fieldCompanyName": "Firma",
|
||||
"fieldStreet1": "Straße 1",
|
||||
"fieldStreet2": "Straße 2",
|
||||
"fieldCity": "Stadt",
|
||||
"fieldState": "Bundesland",
|
||||
"fieldZip": "Postleitzahl",
|
||||
"fieldCountry": "Land",
|
||||
"fieldPrivatePhone": "Telefon (Privat)",
|
||||
"fieldMobilePhone": "Telefon (Mobil)",
|
||||
"fieldPhone": "Telefon",
|
||||
"fieldFax": "Fax",
|
||||
"fieldWebsite": "Website",
|
||||
"fieldBirthday": "Geburtstag",
|
||||
"fieldLeadValue": "LeadValue"
|
||||
}'), new FieldList([
|
||||
"fieldFirstName" => "Vorname",
|
||||
"fieldLastName" => "Nachname",
|
||||
"fieldCompanyName" => "Firma",
|
||||
"fieldStreet1" => "Straße 1",
|
||||
"fieldStreet2" => "Straße 2",
|
||||
"fieldCity" => "Stadt",
|
||||
"fieldState" => "Bundesland",
|
||||
"fieldZip" => "Postleitzahl",
|
||||
"fieldCountry" => "Land",
|
||||
"fieldPrivatePhone" => "Telefon (Privat)",
|
||||
"fieldMobilePhone" => "Telefon (Mobil)",
|
||||
"fieldPhone" => "Telefon",
|
||||
"fieldFax" => "Fax",
|
||||
"fieldWebsite" => "Website",
|
||||
"fieldBirthday" => "Geburtstag",
|
||||
"fieldLeadValue" => "LeadValue",
|
||||
])];
|
||||
yield 'wrong request type' => [new Response(406, [], '["Bei der Erstellung des Objekt ist ein Fehler aufgetreten."]'), null, true];
|
||||
yield 'access denied' => [new Response(403, [], '["API Zugriff verweigert"]'), null, true];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user