Skip to content

Commit

Permalink
Introduce PropertyTypeProviderTest
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam committed Jun 29, 2019
1 parent 0e0ceff commit b0ab0b9
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Contract/About/IPropertyTypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

interface IPropertyTypeProvider
{
public function getType(object $contract, string $property) : string;
public function getTypes(object $contract, string $property) : array;
}
18 changes: 14 additions & 4 deletions src/Contract/About/PropertyTypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
final class PropertyTypeProvider implements IPropertyTypeProvider
{
/**
* @var string[][]
* @var string[][][]
*/
private $cache = [];

public function getType(object $contract, string $property) : string
public function getTypes(object $contract, string $property) : array
{
$cacheKey = get_class($contract);
if (!array_key_exists($cacheKey, $this->cache)) {
Expand All @@ -24,10 +24,20 @@ public function getType(object $contract, string $property) : string
$type = $propertyReflection->getType();

if ($type === null) {
throw new \RuntimeException(); // TODO own exception
$types = [];
foreach ($propertyReflection->getDocBlockTypes() as $_type) {
$types[] = $_type->__toString();
}
}
else {
$types = [$type->__toString()];
}

if (count($types) === 0) {
throw new \RuntimeException; // TODO own exception
}

$this->cache[$cacheKey][$property] = $type->__toString();
$this->cache[$cacheKey][$property] = $types;
}

return $this->cache[$cacheKey][$property];
Expand Down
12 changes: 7 additions & 5 deletions src/Encoder/EncoderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function encode(object $contract) : array

$encodedValues = [];
foreach ($properties as $property => $propertyGetter) {
$propertyType = $this->propertyTypeProvider->getType($contract, $property);
$encodedValue = $this->encodeValue($contract, $propertyGetter, $propertyType);
$propertyTypes = $this->propertyTypeProvider->getTypes($contract, $property);
$encodedValue = $this->encodeValue($contract, $propertyGetter, $propertyTypes);

if ($encodedValue !== null) {
$encodedValues[$property] = $encodedValue;
Expand All @@ -44,7 +44,7 @@ public function encode(object $contract) : array
return $encodedValues;
}

private function encodeValue(object $contract, string $propertyGetter, string $propertyType) : ?string
private function encodeValue(object $contract, string $propertyGetter, array $propertyTypes) : ?string
{
$value = call_user_method($propertyGetter, $contract);

Expand All @@ -53,8 +53,10 @@ private function encodeValue(object $contract, string $propertyGetter, string $p
}

foreach ($this->propertyEncoders as $propertyEncoder) {
if ($propertyEncoder->isApplicable($propertyType)) {
return $propertyEncoder->encode($value);
foreach ($propertyTypes as $propertyType) {
if ($propertyEncoder->isApplicable($propertyType)) {
return $propertyEncoder->encode($value);
}
}
}
}
Expand Down
116 changes: 116 additions & 0 deletions tests/cases/Contract/About/PropertyTypeProviderTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);

namespace Trejjam\ComgateApi\Tests\Contract\About;

use Composer;
use Tester;
use Tester\Assert;
use Trejjam\ComgateApi\Configuration\Integration;
use Trejjam\ComgateApi\Contract\About\PropertyTypeProvider;
use Trejjam\ComgateApi\Contract\Create;
use Trejjam\ComgateApi\Contract\Method;
use Trejjam\ComgateApi\Property\Country;
use Trejjam\ComgateApi\Property\Language;
use Trejjam\ComgateApi\Property\ResponseType;

require __DIR__ . '/../../../bootstrap.php';

class PropertyTypeProviderTest extends Tester\TestCase
{
/**
* @var PropertyTypeProvider
*/
private $propertyTypeProvider;

protected function setUp()
{
$this->propertyTypeProvider = new PropertyTypeProvider;
}

public function testCreateContract()
{
$createContract = $this->createCreateContract();

Assert::same(['string'], $this->propertyTypeProvider->getTypes($createContract, 'merchant'));
Assert::same(['bool', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'test'));
Assert::same(
['\\' . Country::class, 'null'],
$this->propertyTypeProvider->getTypes($createContract, 'country')
);
Assert::same(['int'], $this->propertyTypeProvider->getTypes($createContract, 'price'));
Assert::same(['string'], $this->propertyTypeProvider->getTypes($createContract, 'curr'));
Assert::same(['string'], $this->propertyTypeProvider->getTypes($createContract, 'label'));
Assert::same(['string'], $this->propertyTypeProvider->getTypes($createContract, 'refId'));
Assert::same(['string', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'payerId'));
Assert::same(['string'], $this->propertyTypeProvider->getTypes($createContract, 'method'));
Assert::same(['string', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'account'));
Assert::same(['string'], $this->propertyTypeProvider->getTypes($createContract, 'email'));
Assert::same(['string', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'phone'));
Assert::same(['string', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'name'));
Assert::same(['string', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'lang'));
Assert::same(['bool', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'prepareOnly'));
Assert::same(['string', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'secret'));
Assert::same(['bool', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'preauth'));
Assert::same(['bool', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'initRecurring'));
Assert::same(['bool', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'verification'));
Assert::same(['bool', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'embedded'));
Assert::same(['bool', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'eetReport'));
Assert::same(['object', 'null'], $this->propertyTypeProvider->getTypes($createContract, 'eetData'));
}

public function testMethodContract()
{
$methodContract = $this->createMethodContract();

Assert::same(['string'], $this->propertyTypeProvider->getTypes($methodContract, 'merchant'));
Assert::same(['string'], $this->propertyTypeProvider->getTypes($methodContract, 'secret'));
Assert::same(
['\\' . ResponseType::class, 'null'],
$this->propertyTypeProvider->getTypes($methodContract, 'type')
);
Assert::same(
['\\' . Language::class, 'null'],
$this->propertyTypeProvider->getTypes($methodContract, 'lang')
);
Assert::same(['string', 'null'], $this->propertyTypeProvider->getTypes($methodContract, 'curr'));
Assert::same(
['\\' . Country::class, 'null'],
$this->propertyTypeProvider->getTypes($methodContract, 'country')
);
}

private function createCreateContract() : Create
{
return new Create(
$this->createIntegration(),
0,
'CZK',
'',
'',
'',
''
);
}

private function createMethodContract() : Method
{
return new Method(
$this->createIntegration()
);
}

private function createIntegration() : Integration
{
return new Integration(
'https://payments.comgate.cz',
'v1.0',
'<merchant>',
'<secret>',
true
);
}
}

$test = new PropertyTypeProviderTest;
$test->run();

0 comments on commit b0ab0b9

Please sign in to comment.