Skip to content

Commit 6affddf

Browse files
committed
#2 - increase test products and add highlights endpoint
1 parent 2d4ee9d commit 6affddf

File tree

11 files changed

+223
-42
lines changed

11 files changed

+223
-42
lines changed

examples/highlights/highlights.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use BatteryIncludedSdk\Client\ApiClient;
6+
use BatteryIncludedSdk\Client\CurlHttpClient;
7+
use BatteryIncludedSdk\Highlights\HighlightsService;
8+
9+
require_once __DIR__ . '/../../vendor/autoload.php';
10+
require_once __DIR__ . '/../credentials.php';
11+
12+
$apiClient = new ApiClient(
13+
new CurlHttpClient(),
14+
'https://api.batteryincluded.io/api/v1/collections/',
15+
$collection,
16+
$apiKey
17+
);
18+
19+
$service = new HighlightsService($apiClient);
20+
21+
echo '<pre>';
22+
print_r($service->getHighlights()->getAll());
23+
echo '</pre>';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use BatteryIncludedSdk\Client\ApiClient;
5+
use BatteryIncludedSdk\Client\CurlHttpClient;
6+
use BatteryIncludedSdk\Shop\BrowseSearchStruct;
7+
use BatteryIncludedSdk\Shop\BrowseService;
8+
9+
require_once __DIR__ . '/../../vendor/autoload.php';
10+
require_once __DIR__ . '/../credentials.php';
11+
12+
$apiClient = new ApiClient(
13+
new CurlHttpClient(),
14+
'https://api.batteryincluded.io/api/v1/collections/',
15+
$collection,
16+
$apiKey
17+
);
18+
19+
$syncService = new BrowseService($apiClient);
20+
$searchStruct = new BrowseSearchStruct();
21+
$searchStruct->setQuery('extension');
22+
$result = $syncService->browse($searchStruct);
23+
24+
echo '<pre>';
25+
print_r($result->getRawResponse());
26+
echo '</pre>';
27+
exit;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Highlights;
6+
7+
use BatteryIncludedSdk\Service\Response;
8+
9+
class HighlightsResponse extends Response
10+
{
11+
private $querySuggestions;
12+
private $searches;
13+
14+
public function __construct(string $responseRaw)
15+
{
16+
parent::__construct($responseRaw);
17+
$this->querySuggestions = $this->getBody()['querySuggestions'] ?? [];
18+
$this->searches = $this->getBody()['searches'] ?? [];
19+
}
20+
21+
public function getAll(): array
22+
{
23+
return [
24+
'searches' => $this->getSearches(),
25+
'querySuggestions' => $this->getQuerySuggestions(),
26+
];
27+
}
28+
29+
public function getSearches()
30+
{
31+
return $this->searches;
32+
}
33+
34+
public function getQuerySuggestions()
35+
{
36+
return $this->querySuggestions;
37+
}
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Highlights;
6+
7+
use BatteryIncludedSdk\Client\ApiClient;
8+
9+
class HighlightsService
10+
{
11+
public function __construct(private ApiClient $apiClient)
12+
{
13+
}
14+
15+
public function getHighlights(): HighlightsResponse
16+
{
17+
$response = $this->apiClient->getJson(
18+
'/documents/highlights',
19+
[]
20+
);
21+
22+
return new HighlightsResponse($response->getRawResponse());
23+
}
24+
}

tests/Helper.php

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,49 @@ class Helper
1818
*/
1919
public static function generateProducts(
2020
int $iterations,
21+
array $devices = ['iPhone', 'iPad', 'MacBook'],
2122
array $colours = ['Blau', 'Rosa', 'Gold', 'Schwarz'],
2223
array $storages = ['128GB', '256GB', '512GB'],
2324
): array {
2425
$products = [];
2526
$id = 0;
2627
for ($i = 1; $i <= $iterations; $i++) {
27-
foreach ($colours as $color) {
28-
foreach ($storages as $storage) {
29-
$id++;
30-
$product = new ProductBaseDto((string) $id);
31-
$product->setName('iPhone ' . $i . ' Pro ' . $color . ' - ' . $storage);
32-
$product->setDescription(
33-
'The latest iPhone with advanced features. Color: ' . $color . ', Storage: ' . $storage . '.'
34-
);
35-
$product->setId((string) $id);
36-
$product->setOrdernumber('AP-00' . $i . '-' . $color . '-' . $storage);
37-
$product->setPrice(1000 + $id);
38-
$product->setInstock(rand(0, 50));
39-
$product->setRating((float) (mt_rand(1, 10) / 2));
40-
$product->setManufacture('Apple');
41-
$product->setManufactureNumber('A' . $i . '-' . $color . '-' . $storage);
42-
$product->setEan('195950639292');
43-
$product->setImageUrl(
44-
'https://dummyimage.com/600x400/bbb/fff.png&text=' . $i . '-' . $color . '-' . $storage
45-
);
46-
$product->setShopUrl('https://www.apple.com/iphone-17-pro/');
47-
$product->setProperties(
48-
(new ProductPropertyDto())
49-
->addProperty('Farbe', $color)
50-
->addProperty('Speicherkapazität', $storage)
51-
->addProperty('Displaygröße', '6,1')
52-
);
53-
$product->addCategory(
54-
(new CategoryDto())->addCategoryNode('Apple')->addCategoryNode('iPhone')->addCategoryNode(
55-
'iPhone ' . $i . ' Pro'
56-
)
57-
);
58-
$product->addCategory(
59-
(new CategoryDto())->addCategoryNode('Apple')->addCategoryNode('iPhone Pro ' . $color)
60-
);
61-
$products[] = $product;
28+
foreach ($devices as $device) {
29+
foreach ($colours as $color) {
30+
foreach ($storages as $storage) {
31+
$id++;
32+
$product = new ProductBaseDto((string) $id);
33+
$product->setName($device . ' ' . $i . ' Pro ' . $color . ' - ' . $storage);
34+
$product->setDescription(
35+
'The latest ' . $device . ' with advanced features. Color: ' . $color . ', Storage: ' . $storage . '.'
36+
);
37+
$product->setId((string) $id);
38+
$product->setOrdernumber('AP-00' . $i . '-' . $color . '-' . $storage);
39+
$product->setPrice(1000 + $id);
40+
$product->setInstock(rand(0, 50));
41+
$product->setRating((float) (mt_rand(1, 10) / 2));
42+
$product->setManufacture('Apple');
43+
$product->setManufactureNumber('A' . $i . '-' . $color . '-' . $storage);
44+
$product->setEan('195950639292');
45+
$product->setImageUrl(
46+
'https://dummyimage.com/600x400/bbb/fff.png&text=' . $i . '-' . $color . '-' . $storage
47+
);
48+
$product->setShopUrl('https://www.apple.com/' . $device . '-' . $i . '-pro/');
49+
$product->setProperties(
50+
(new ProductPropertyDto())
51+
->addProperty('Gerät', $device)
52+
->addProperty('Farbe', $color)
53+
->addProperty('Speicherkapazität', $storage)
54+
->addProperty('Displaygröße', '6,1')
55+
);
56+
$product->addCategory(
57+
(new CategoryDto())->addCategoryNode('Apple')->addCategoryNode($device)->addCategoryNode($device . ' ' . $i . ' Pro')
58+
);
59+
$product->addCategory(
60+
(new CategoryDto())->addCategoryNode('Apple')->addCategoryNode($device . ' Pro ' . $color)
61+
);
62+
$products[] = $product;
63+
}
6264
}
6365
}
6466
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdkTests\Highlights;
6+
7+
use BatteryIncludedSdk\Highlights\HighlightsResponse;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
11+
#[CoversClass(HighlightsResponse::class)]
12+
class HighlightsResponseTest extends TestCase
13+
{
14+
public function testGetHighlightsResponse(): void
15+
{
16+
$data = [
17+
'searches' => [
18+
['q' => 'test1', 'count' => 5],
19+
['q' => 'test2', 'count' => 3],
20+
],
21+
'querySuggestions' => [
22+
['suggestion' => 'foo'],
23+
['suggestion' => 'bar'],
24+
],
25+
];
26+
$json = json_encode($data);
27+
28+
$response = new HighlightsResponse($json);
29+
30+
$this->assertSame($data['searches'], $response->getSearches());
31+
$this->assertSame($data['querySuggestions'], $response->getQuerySuggestions());
32+
$this->assertSame($data, $response->getAll());
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdkTests\Highlights;
6+
7+
use BatteryIncludedSdk\Client\ApiClient;
8+
use BatteryIncludedSdk\Client\CurlHttpClient;
9+
use BatteryIncludedSdk\Highlights\HighlightsResponse;
10+
use BatteryIncludedSdk\Highlights\HighlightsService;
11+
use BatteryIncludedSdk\Service\AbstractService;
12+
use BatteryIncludedSdk\Service\Response;
13+
use BatteryIncludedSdkTests\Helper;
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\UsesClass;
16+
use PHPUnit\Framework\TestCase;
17+
18+
#[CoversClass(HighlightsResponse::class)]
19+
#[CoversClass(HighlightsService::class)]
20+
#[UsesClass(ApiClient::class)]
21+
#[UsesClass(CurlHttpClient::class)]
22+
#[UsesClass(Response::class)]
23+
#[UsesClass(AbstractService::class)]
24+
class HighlightsServiceTest extends TestCase
25+
{
26+
public function testHighlightsMethodAgainstLiveApi()
27+
{
28+
$HighlightsService = new HighlightsService(Helper::getApiClient());
29+
$result = $HighlightsService->getHighlights();
30+
31+
$this->assertInstanceOf(HighlightsResponse::class, $result);
32+
}
33+
}

tests/Recommendations/RecommendationsServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testRecommendByIdentifierReturnsRecommendationsResponse()
3737
$mockApiClient->method('getJson')->willReturn($mockResponse);
3838

3939
$service = new RecommendationsService($mockApiClient);
40-
$result = $service->recommendByIdentifier('240');
40+
$result = $service->recommendByIdentifier('720');
4141

4242
$this->assertInstanceOf(RecommendationsResponse::class, $result);
4343
$this->assertSame($expectedData, $result->getRecommendations());

tests/Service/SyncServiceTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testSyncOneOrMany()
3232
$syncService = new SyncService($apiClient);
3333

3434
$result = $syncService->syncOneOrManyElements(...$products);
35-
$this->assertCount(240, $result->getBody());
35+
$this->assertCount(720, $result->getBody());
3636
}
3737

3838
public function testSyncFull()
@@ -42,7 +42,7 @@ public function testSyncFull()
4242
$syncService = new SyncService($apiClient);
4343

4444
$result = $syncService->syncFullElements(...$products);
45-
$this->assertCount(240, $result->getBody());
45+
$this->assertCount(720, $result->getBody());
4646
}
4747

4848
public function testSyncFullBatched()
@@ -82,7 +82,7 @@ public function testDeleteProduct()
8282
$apiClient = Helper::getApiClient();
8383
$syncService = new SyncService($apiClient);
8484

85-
$result = $syncService->deleteElementsByIds('PRODUCT-240', 'PRODUCT-2');
85+
$result = $syncService->deleteElementsByIds('PRODUCT-720', 'PRODUCT-2');
8686

8787
$this->assertEquals(2, $result->getBody()['num_deleted']);
8888
}

tests/Shop/BrowseServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testBrowseMethodAgainstLiveApi()
5252
$syncService = new SyncService($apiClient);
5353

5454
$result = $syncService->syncOneOrManyElements(...$products);
55-
$this->assertCount(240, $result->getBody());
55+
$this->assertCount(720, $result->getBody());
5656
$browseService = new BrowseService(Helper::getApiClient());
5757
$searchStruct = new BrowseSearchStruct();
5858
$searchStruct->addFilter('_PRODUCT.properties.Speicherkapazität', '512GB');
@@ -81,7 +81,7 @@ public function testBrowseMethodWithPresetAgainstLiveApi()
8181
$syncService = new SyncService($apiClient);
8282

8383
$result = $syncService->syncOneOrManyElements(...$products);
84-
$this->assertCount(240, $result->getBody());
84+
$this->assertCount(720, $result->getBody());
8585
$browseService = new BrowseService(Helper::getApiClient());
8686
$searchStruct = new BrowseSearchStruct();
8787
$searchStruct->addFilter('_PRODUCT.properties.Speicherkapazität', '512GB');

0 commit comments

Comments
 (0)