mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-08-04 23:51:46 +00:00
Use V2 api to fetch data from TME
This commit is contained in:
parent
a5cde64550
commit
d3020698a3
3 changed files with 309 additions and 207 deletions
|
|
@ -46,83 +46,111 @@ final class TMEProviderTest extends TestCase
|
|||
{
|
||||
$this->httpClient = new MockHttpClient();
|
||||
$this->settings = SettingsTestHelper::createSettingsDummy(TMESettings::class);
|
||||
// Use a short (anonymous-style) token so grossPrices is read from settings
|
||||
$this->settings->apiToken = 'test_token_000000000000000000000000000000000000000';
|
||||
$this->settings->apiSecret = 'test_secret';
|
||||
$this->settings->apiToken = 'test_token_000000000000000000000000000000000000000000000000';
|
||||
$this->settings->apiSecret = 'test_secret_00000000';
|
||||
$this->settings->currency = 'EUR';
|
||||
$this->settings->language = 'en';
|
||||
$this->settings->country = 'DE';
|
||||
$this->settings->grossPrices = false;
|
||||
$this->provider = new TMEProvider(new TMEClient($this->httpClient, $this->settings), $this->settings);
|
||||
}
|
||||
|
||||
// --- Mock response helpers ---
|
||||
// Only fields actually read by TMEProvider are included.
|
||||
|
||||
private function mockProductList(array $products): MockResponse
|
||||
/** OAuth2 token response – always the first reply in a sequence that triggers an API call */
|
||||
private function mockTokenResponse(): MockResponse
|
||||
{
|
||||
return new MockResponse(json_encode([
|
||||
'Status' => 'OK',
|
||||
'Data' => ['ProductList' => $products],
|
||||
'access_token' => 'mock_access_token',
|
||||
'token_type' => 'Bearer',
|
||||
'expires_in' => 300,
|
||||
'refresh_token' => 'mock_refresh_token',
|
||||
]));
|
||||
}
|
||||
|
||||
private function mockSearchResults(array $products): MockResponse
|
||||
{
|
||||
return new MockResponse(json_encode([
|
||||
'status' => 'OK',
|
||||
'data' => [
|
||||
'products' => ['elements' => $products],
|
||||
'parameters' => [],
|
||||
'counters' => [],
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
private function mockProductsList(array $products): MockResponse
|
||||
{
|
||||
return new MockResponse(json_encode([
|
||||
'status' => 'OK',
|
||||
'data' => ['elements' => $products],
|
||||
]));
|
||||
}
|
||||
|
||||
private function mockFilesList(array $products): MockResponse
|
||||
{
|
||||
return new MockResponse(json_encode([
|
||||
'Status' => 'OK',
|
||||
'Data' => ['ProductList' => $products],
|
||||
'status' => 'OK',
|
||||
'data' => ['elements' => $products],
|
||||
]));
|
||||
}
|
||||
|
||||
private function mockParametersList(array $products): MockResponse
|
||||
{
|
||||
return new MockResponse(json_encode([
|
||||
'Status' => 'OK',
|
||||
'Data' => ['ProductList' => $products],
|
||||
'status' => 'OK',
|
||||
'data' => ['elements' => $products],
|
||||
]));
|
||||
}
|
||||
|
||||
private function mockPrices(string $currency, string $priceType, array $products): MockResponse
|
||||
private function mockPricesData(string $currency, string $priceType, array $products): MockResponse
|
||||
{
|
||||
return new MockResponse(json_encode([
|
||||
'Status' => 'OK',
|
||||
'Data' => [
|
||||
'Currency' => $currency,
|
||||
'PriceType' => $priceType,
|
||||
'ProductList' => $products,
|
||||
'status' => 'OK',
|
||||
'data' => [
|
||||
'currency' => $currency,
|
||||
'price_type' => $priceType,
|
||||
'elements' => $products,
|
||||
],
|
||||
]));
|
||||
}
|
||||
|
||||
// --- Mock data ---
|
||||
// --- Mock data (v2 field names) ---
|
||||
|
||||
private function smd0603Product(): array
|
||||
{
|
||||
return [
|
||||
'symbol' => 'SMD0603-5K1-1%',
|
||||
'original_symbol' => '0603SAF5101T5E',
|
||||
'producer' => 'ROYALOHM',
|
||||
'description' => 'Resistor: thick film; SMD; 0603; 5.1kΩ; 0.1W; ±1%; 50V; -55÷155°C',
|
||||
'category' => 'SMD resistors',
|
||||
'photo' => '//ce8dc832c.cloudimg.io/v7/_cdn_/E9/C2/B0/00/0/732318_1.jpg',
|
||||
'statuses' => [],
|
||||
'product_information_page' => '//www.tme.eu/en/details/smd0603-5k1-1%/smd-resistors/royalohm/0603saf5101t5e/',
|
||||
'weight' => 0.021,
|
||||
'weight_unit' => 'g',
|
||||
];
|
||||
}
|
||||
|
||||
private function smd0603SearchResults(): MockResponse
|
||||
{
|
||||
return $this->mockSearchResults([$this->smd0603Product()]);
|
||||
}
|
||||
|
||||
private function smd0603Products(): MockResponse
|
||||
{
|
||||
return $this->mockProductList([[
|
||||
'Symbol' => 'SMD0603-5K1-1%',
|
||||
'OriginalSymbol' => '0603SAF5101T5E',
|
||||
'Producer' => 'ROYALOHM',
|
||||
'Description' => 'Resistor: thick film; SMD; 0603; 5.1kΩ; 0.1W; ±1%; 50V; -55÷155°C',
|
||||
'Category' => 'SMD resistors',
|
||||
'Photo' => '//ce8dc832c.cloudimg.io/v7/_cdn_/E9/C2/B0/00/0/732318_1.jpg',
|
||||
'ProductStatusList' => [],
|
||||
'ProductInformationPage' => '//www.tme.eu/en/details/smd0603-5k1-1%/smd-resistors/royalohm/0603saf5101t5e/',
|
||||
'Weight' => 0.021,
|
||||
'WeightUnit' => 'g',
|
||||
]]);
|
||||
return $this->mockProductsList([$this->smd0603Product()]);
|
||||
}
|
||||
|
||||
private function smd0603Files(): MockResponse
|
||||
{
|
||||
return $this->mockFilesList([[
|
||||
'Symbol' => 'SMD0603-5K1-1%',
|
||||
'Files' => [
|
||||
'AdditionalPhotoList' => [],
|
||||
'DocumentList' => [
|
||||
['DocumentUrl' => '//www.tme.eu/Document/b315665a56acbc42df513c99b390ad98/ROYALOHM-THICKFILM.pdf'],
|
||||
['DocumentUrl' => '//www.tme.eu/Document/c283990e907c122bb808207d1578ac7f/POWER_RATING-DTE.pdf'],
|
||||
],
|
||||
'symbol' => 'SMD0603-5K1-1%',
|
||||
'photos' => [],
|
||||
'documents' => [
|
||||
['url' => '//www.tme.eu/Document/b315665a56acbc42df513c99b390ad98/ROYALOHM-THICKFILM.pdf'],
|
||||
['url' => '//www.tme.eu/Document/c283990e907c122bb808207d1578ac7f/POWER_RATING-DTE.pdf'],
|
||||
],
|
||||
]]);
|
||||
}
|
||||
|
|
@ -130,55 +158,58 @@ final class TMEProviderTest extends TestCase
|
|||
private function smd0603Parameters(): MockResponse
|
||||
{
|
||||
return $this->mockParametersList([[
|
||||
'Symbol' => 'SMD0603-5K1-1%',
|
||||
'ParameterList' => [
|
||||
['ParameterId' => 34, 'ParameterName' => 'Type of resistor', 'ParameterValue' => 'thick film'],
|
||||
['ParameterId' => 35, 'ParameterName' => 'Case - mm', 'ParameterValue' => '1608'],
|
||||
['ParameterId' => 38, 'ParameterName' => 'Resistance', 'ParameterValue' => '5.1kΩ'],
|
||||
['ParameterId' => 39, 'ParameterName' => 'Tolerance', 'ParameterValue' => '±1%'],
|
||||
['ParameterId' => 120, 'ParameterName' => 'Operating voltage', 'ParameterValue' => '50V'],
|
||||
'symbol' => 'SMD0603-5K1-1%',
|
||||
'parameters' => [
|
||||
['id' => 34, 'name' => 'Type of resistor', 'value' => 'thick film'],
|
||||
['id' => 35, 'name' => 'Case - mm', 'value' => '1608'],
|
||||
['id' => 38, 'name' => 'Resistance', 'value' => '5.1kΩ'],
|
||||
['id' => 39, 'name' => 'Tolerance', 'value' => '±1%'],
|
||||
['id' => 120, 'name' => 'Operating voltage', 'value' => '50V'],
|
||||
],
|
||||
]]);
|
||||
}
|
||||
|
||||
private function smd0603Prices(): MockResponse
|
||||
{
|
||||
return $this->mockPrices('EUR', 'NET', [[
|
||||
'Symbol' => 'SMD0603-5K1-1%',
|
||||
'PriceList' => [
|
||||
['Amount' => 100, 'PriceValue' => 0.01077],
|
||||
['Amount' => 1000, 'PriceValue' => 0.00291],
|
||||
['Amount' => 5000, 'PriceValue' => 0.00150],
|
||||
return $this->mockPricesData('EUR', 'NET', [[
|
||||
'symbol' => 'SMD0603-5K1-1%',
|
||||
'price_list' => [
|
||||
['amount' => 100, 'price_value' => 0.01077],
|
||||
['amount' => 1000, 'price_value' => 0.00291],
|
||||
['amount' => 5000, 'price_value' => 0.00150],
|
||||
],
|
||||
]]);
|
||||
}
|
||||
|
||||
private function etqp3mProduct(): array
|
||||
{
|
||||
return [
|
||||
'symbol' => 'ETQP3M6R8KVP',
|
||||
'original_symbol' => 'ETQP3M6R8KVP',
|
||||
'producer' => 'PANASONIC',
|
||||
'description' => 'Inductor: wire; SMD; 6.8uH; 2.9A; R: 65.7mΩ; ±20%; ETQP3M; 5.5x5x3mm',
|
||||
'category' => 'Inductors',
|
||||
'photo' => '//ce8dc832c.cloudimg.io/v7/_cdn_/9E/27/A0/00/0/684777_1.jpg',
|
||||
'statuses' => [],
|
||||
'product_information_page' => '//www.tme.eu/en/details/etqp3m6r8kvp/inductors/panasonic/',
|
||||
'weight' => 0.44,
|
||||
'weight_unit' => 'g',
|
||||
];
|
||||
}
|
||||
|
||||
private function etqp3mProducts(): MockResponse
|
||||
{
|
||||
return $this->mockProductList([[
|
||||
'Symbol' => 'ETQP3M6R8KVP',
|
||||
'OriginalSymbol' => 'ETQP3M6R8KVP',
|
||||
'Producer' => 'PANASONIC',
|
||||
'Description' => 'Inductor: wire; SMD; 6.8uH; 2.9A; R: 65.7mΩ; ±20%; ETQP3M; 5.5x5x3mm',
|
||||
'Category' => 'Inductors',
|
||||
'Photo' => '//ce8dc832c.cloudimg.io/v7/_cdn_/9E/27/A0/00/0/684777_1.jpg',
|
||||
'ProductStatusList' => [],
|
||||
'ProductInformationPage' => '//www.tme.eu/en/details/etqp3m6r8kvp/inductors/panasonic/',
|
||||
'Weight' => 0.44,
|
||||
'WeightUnit' => 'g',
|
||||
]]);
|
||||
return $this->mockProductsList([$this->etqp3mProduct()]);
|
||||
}
|
||||
|
||||
private function etqp3mFiles(): MockResponse
|
||||
{
|
||||
return $this->mockFilesList([[
|
||||
'Symbol' => 'ETQP3M6R8KVP',
|
||||
'Files' => [
|
||||
'AdditionalPhotoList' => [],
|
||||
'DocumentList' => [
|
||||
['DocumentUrl' => '//www.tme.eu/Document/50a845881f09d8a2248350946e11df38/AGL0000C63.pdf'],
|
||||
['DocumentUrl' => '//www.tme.eu/Document/8480690a42fa577214e35e33d3fc8d77/ETQP3M100KVN-LNK.txt'],
|
||||
],
|
||||
'symbol' => 'ETQP3M6R8KVP',
|
||||
'photos' => [],
|
||||
'documents' => [
|
||||
['url' => '//www.tme.eu/Document/50a845881f09d8a2248350946e11df38/AGL0000C63.pdf'],
|
||||
['url' => '//www.tme.eu/Document/8480690a42fa577214e35e33d3fc8d77/ETQP3M100KVN-LNK.txt'],
|
||||
],
|
||||
]]);
|
||||
}
|
||||
|
|
@ -186,23 +217,23 @@ final class TMEProviderTest extends TestCase
|
|||
private function etqp3mParameters(): MockResponse
|
||||
{
|
||||
return $this->mockParametersList([[
|
||||
'Symbol' => 'ETQP3M6R8KVP',
|
||||
'ParameterList' => [
|
||||
['ParameterId' => 566, 'ParameterName' => 'Inductance', 'ParameterValue' => '6.8µH'],
|
||||
['ParameterId' => 370, 'ParameterName' => 'Operating current', 'ParameterValue' => '2.9A'],
|
||||
['ParameterId' => 39, 'ParameterName' => 'Tolerance', 'ParameterValue' => '±20%'],
|
||||
'symbol' => 'ETQP3M6R8KVP',
|
||||
'parameters' => [
|
||||
['id' => 566, 'name' => 'Inductance', 'value' => '6.8µH'],
|
||||
['id' => 370, 'name' => 'Operating current', 'value' => '2.9A'],
|
||||
['id' => 39, 'name' => 'Tolerance', 'value' => '±20%'],
|
||||
],
|
||||
]]);
|
||||
}
|
||||
|
||||
private function etqp3mPrices(): MockResponse
|
||||
{
|
||||
return $this->mockPrices('EUR', 'NET', [[
|
||||
'Symbol' => 'ETQP3M6R8KVP',
|
||||
'PriceList' => [
|
||||
['Amount' => 1, 'PriceValue' => 0.589],
|
||||
['Amount' => 5, 'PriceValue' => 0.429],
|
||||
['Amount' => 10, 'PriceValue' => 0.399],
|
||||
return $this->mockPricesData('EUR', 'NET', [[
|
||||
'symbol' => 'ETQP3M6R8KVP',
|
||||
'price_list' => [
|
||||
['amount' => 1, 'price_value' => 0.589],
|
||||
['amount' => 5, 'price_value' => 0.429],
|
||||
['amount' => 10, 'price_value' => 0.399],
|
||||
],
|
||||
]]);
|
||||
}
|
||||
|
|
@ -257,7 +288,11 @@ final class TMEProviderTest extends TestCase
|
|||
|
||||
public function testSearchByKeyword(): void
|
||||
{
|
||||
$this->httpClient->setResponseFactory([$this->smd0603Products()]);
|
||||
// Request order: POST /auth/token, GET /products/search
|
||||
$this->httpClient->setResponseFactory([
|
||||
$this->mockTokenResponse(),
|
||||
$this->smd0603SearchResults(),
|
||||
]);
|
||||
|
||||
$results = $this->provider->searchByKeyword('SMD0603-5K1-1%');
|
||||
|
||||
|
|
@ -277,7 +312,10 @@ final class TMEProviderTest extends TestCase
|
|||
|
||||
public function testGetDetailsWithPercentInPartNumber(): void
|
||||
{
|
||||
// Request order: POST /auth/token, GET /products, GET /products/files,
|
||||
// GET /products/parameters, GET /products/data
|
||||
$this->httpClient->setResponseFactory([
|
||||
$this->mockTokenResponse(),
|
||||
$this->smd0603Products(),
|
||||
$this->smd0603Files(),
|
||||
$this->smd0603Parameters(),
|
||||
|
|
@ -325,7 +363,10 @@ final class TMEProviderTest extends TestCase
|
|||
|
||||
public function testGetDetailsForEtqp3m6r8kvp(): void
|
||||
{
|
||||
// Request order: POST /auth/token, GET /products, GET /products/files,
|
||||
// GET /products/parameters, GET /products/data
|
||||
$this->httpClient->setResponseFactory([
|
||||
$this->mockTokenResponse(),
|
||||
$this->etqp3mProducts(),
|
||||
$this->etqp3mFiles(),
|
||||
$this->etqp3mParameters(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue