Skip to content

Commit

Permalink
update to php 8.1, use enums, add tests, remove category level reques…
Browse files Browse the repository at this point in the history
…t class, adjust alibaba exception
  • Loading branch information
Dominik-Czulak committed Feb 16, 2024
1 parent 892d33b commit c69f344
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": "^8",
"php": "^8.1",
"ext-json": "*",
"ext-mbstring": "*",
"symfony/http-client": "^5.4 || ^6"
Expand Down
7 changes: 5 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ private function throwOnError(array $data): void
$errorResponse = $data['error_response'] ?? null;

if ($errorResponse !== null) {
$subMessage = $errorResponse['sub_msg'] ?? null;
$subCode = $errorResponse['sub_code'] ?? null;

throw new AlibabaException(
$errorResponse['msg'],
(int) $errorResponse['code'],
$errorResponse['sub_msg'],
$errorResponse['sub_code'],
$subMessage,
$subCode,
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Endpoint/CategoryEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Kyto\Alibaba\Model\Category;
use Kyto\Alibaba\Model\CategoryAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttributeRequest;

class CategoryEndpoint
{
Expand Down Expand Up @@ -82,10 +81,11 @@ public function getLevelAttribute(
string $attributeId,
?string $valueId = null
): CategoryLevelAttribute {
$attributeValueRequest = new CategoryLevelAttributeRequest();
$attributeValueRequest->categoryId = $categoryId;
$attributeValueRequest->attributeId = $attributeId;
$attributeValueRequest->valueId = $valueId ?? '0';
$attributeValueRequest = [
'cat_id' => $categoryId,
'attr_id' => $attributeId,
'value_id' => $valueId ?? '0'
];

$data = $this->client->request([
'method' => 'alibaba.icbu.category.level.attr.get',
Expand Down
12 changes: 12 additions & 0 deletions src/Enum/InputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Enum;

enum InputType: string
{
case INPUT = 'input';
case MULTI_SELECT = 'multi_select';
case SINGLE_SELECT = 'single_select';
}
13 changes: 13 additions & 0 deletions src/Enum/ShowType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Enum;

enum ShowType: string
{
case CHECK_BOX = 'check_box'; // multi_select
case GROUP_TABLE = 'group_table'; // single_select
case INPUT = 'input'; // input (text)
case LIST_BOX = 'list_box'; // single_select
}
11 changes: 11 additions & 0 deletions src/Enum/ValueType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Kyto\Alibaba\Enum;

enum ValueType: string
{
case NUMBER = 'number';
case STRING = 'string';
}
5 changes: 4 additions & 1 deletion src/Exception/AlibabaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public function __construct(
private ?string $subCode,
?\Throwable $previous = null
) {
$message = sprintf('%s. Sub-code: "%s". Sub-message: "%s".', $message, $this->subCode, $this->subMessage);
$subCodePart = $this->subCode !== null ? sprintf(' Sub-code: "%s".', $this->subCode) : null;
$subMessagePart = $this->subMessage !== null ? sprintf(' Sub-message: "%s".', $this->subMessage) : null;
$message = sprintf('%s.%s%s', $message, $subCodePart, $subMessagePart);

parent::__construct($message, $code, $previous);
}

Expand Down
9 changes: 6 additions & 3 deletions src/Factory/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Kyto\Alibaba\Factory;

use Kyto\Alibaba\Enum\InputType;
use Kyto\Alibaba\Enum\ShowType;
use Kyto\Alibaba\Enum\ValueType;
use Kyto\Alibaba\Model\CategoryLevelAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttributeValue;
use Kyto\Alibaba\Util\Formatter;
Expand Down Expand Up @@ -46,9 +49,9 @@ public function createAttribute(array $data): CategoryAttribute
$model->name = (string) $data['en_name'];
$model->isRequired = (bool) $data['required'];

$model->inputType = (string) $data['input_type'];
$model->showType = (string) $data['show_type'];
$model->valueType = (string) $data['value_type'];
$model->inputType = InputType::from($data['input_type']);
$model->showType = ShowType::from($data['show_type']);
$model->valueType = ValueType::from($data['value_type']);

$model->isSku = (bool) $data['sku_attribute'];
$model->hasCustomizeImage = (bool) $data['customize_image'];
Expand Down
13 changes: 7 additions & 6 deletions src/Model/CategoryAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

namespace Kyto\Alibaba\Model;

use Kyto\Alibaba\Enum\InputType;
use Kyto\Alibaba\Enum\ShowType;
use Kyto\Alibaba\Enum\ValueType;

class CategoryAttribute
{
public string $id;
public string $name;
public bool $isRequired;

// TODO: change to enums once all values would be known
public string $inputType; // Known values: single_select, multi_select, input

// Known values: list_box (single_select), check_box (multi_select), input (input), group_table (single_select)
public string $showType;
public string $valueType; // Known values: string, number
public InputType $inputType;
public ShowType $showType;
public ValueType $valueType;

public bool $isSku;
public bool $hasCustomizeImage;
Expand Down
27 changes: 0 additions & 27 deletions src/Model/CategoryLevelAttributeRequest.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/Endpoint/CategoryEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Kyto\Alibaba\Model\Category;
use Kyto\Alibaba\Model\CategoryAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttributeRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down
104 changes: 98 additions & 6 deletions tests/Factory/CategoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

namespace Kyto\Alibaba\Tests\Factory;

use Kyto\Alibaba\Enum\InputType;
use Kyto\Alibaba\Enum\ShowType;
use Kyto\Alibaba\Enum\ValueType;
use Kyto\Alibaba\Factory\CategoryFactory;
use Kyto\Alibaba\Model\Category;
use Kyto\Alibaba\Model\CategoryAttribute;
use Kyto\Alibaba\Model\CategoryAttributeValue;
use Kyto\Alibaba\Model\CategoryLevelAttribute;
use Kyto\Alibaba\Model\CategoryLevelAttributeValue;
use PHPUnit\Framework\TestCase;

class CategoryFactoryTest extends TestCase
Expand Down Expand Up @@ -145,9 +150,9 @@ public function createAttributeDataProvider(): array
$model->id = '1';
$model->name = 'Example';
$model->isRequired = true;
$model->inputType = 'single_select';
$model->showType = 'list_box';
$model->valueType = 'string';
$model->inputType = InputType::SINGLE_SELECT;
$model->showType = ShowType::LIST_BOX;
$model->valueType = ValueType::STRING;
$model->isSku = false;
$model->hasCustomizeImage = false;
$model->hasCustomizeValue = false;
Expand Down Expand Up @@ -188,9 +193,9 @@ public function createAttributeDataProvider(): array
$model->id = '1';
$model->name = 'Example';
$model->isRequired = true;
$model->inputType = 'input';
$model->showType = 'input';
$model->valueType = 'number';
$model->inputType = InputType::INPUT;
$model->showType = ShowType::INPUT;
$model->valueType = ValueType::NUMBER;
$model->isSku = false;
$model->hasCustomizeImage = false;
$model->hasCustomizeValue = false;
Expand Down Expand Up @@ -253,4 +258,91 @@ public function createAttributeValueDataProvider(): array

return $cases;
}

/**
* @dataProvider createLevelAttributeDataProvider
* @param mixed[] $data
*/
public function testCreateLevelAttribute(array $data, CategoryLevelAttribute $expected): void
{
$actual = $this->categoryFactory->createLevelAttribute($data);
self::assertEquals($expected, $actual);
}

public function createLevelAttributeDataProvider(): \Generator
{
$data = [
'property_id' => '123',
'property_en_name' => 'someName',
'values' => '{}'
];

$expected = new CategoryLevelAttribute();
$expected->id = '123';
$expected->name = 'someName';
$expected->values = [];

yield ['no values' => $data, $expected];

$data = [
'property_id' => '123',
'property_en_name' => 'someName',
'values' => '[{"id":"1","name":"valueNoLeaf"},{"id":2,"name":"valueIsLeaf","leaf":true}]'
];

$levelValueNoLeaf = new CategoryLevelAttributeValue();
$levelValueNoLeaf->id = '1';
$levelValueNoLeaf->name = 'valueNoLeaf';
$levelValueNoLeaf->isLeaf = false;

$levelValueIsLeaf = new CategoryLevelAttributeValue();
$levelValueIsLeaf->id = '2';
$levelValueIsLeaf->name = 'valueIsLeaf';
$levelValueIsLeaf->isLeaf = true;

$expected = new CategoryLevelAttribute();
$expected->id = '123';
$expected->name = 'someName';
$expected->values = [$levelValueNoLeaf, $levelValueIsLeaf];

yield ['with values' => $data, $expected];
}

/**
* @dataProvider createLevelAttributeValueDataProvider
* @param mixed[] $data
*/
public function testCreateLevelAttributeValue(array $data, CategoryLevelAttributeValue $expected): void
{
$actual = $this->categoryFactory->createLevelAttributeValue($data);
self::assertEquals($expected, $actual);
}

public function createLevelAttributeValueDataProvider(): \Generator
{
$data = [
"id" => "1",
"name" => "valueNoLeaf"
];

$expected = new CategoryLevelAttributeValue();
$expected->name = 'valueNoLeaf';
$expected->id = '1';
$expected->isLeaf = false;

yield ['no leaf' => $data, $expected];

$data = [
"id" => "1",
"name" => "valueIsLeaf",
"leaf" => true
];

$expected = new CategoryLevelAttributeValue();
$expected->name = 'valueIsLeaf';
$expected->id = '1';
$expected->isLeaf = true;

yield ['is leaf' => $data, $expected];
}
}

0 comments on commit c69f344

Please sign in to comment.