Add KrogerToGrocyConverter class with tests

Kroger grocery stores allow manual access to past purchases in json form
through a browser's developer tools.  This class converts the Kroger
json data into something Grocy can understand while doing minor error
checking.  Also added appropriate unit tests.

In order to get this json, open developer tools and navigate to
https://www.qfc.com/mypurchases (or another Kroger grocer, aka
https://www.fredmeyer.com/mypurchases) and look for a call to
/mypurchases/api/v1/receipt/details.  The entire response will contain
all recent purchases.
This commit is contained in:
Zeb Fross 2020-03-28 12:32:00 -07:00
parent 375865d80e
commit ac53c5abf7
5 changed files with 1785 additions and 3 deletions

View File

@ -23,5 +23,8 @@
"files": [
"helpers/extensions.php"
]
},
"require-dev": {
"phpunit/phpunit": "9"
}
}

1603
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,46 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Grocy\Helpers\KrogerToGrocyConverter;
final class KrogerToGrocyConverterTest extends TestCase
{
public function testConvertUpcToBarcode(): void
{
$this->assertEquals(
"0722252601421",
KrogerToGrocyConverter::ConvertUpcToBarcode("0072225260142")
);
$this->assertEquals(
"0072220005165",
KrogerToGrocyConverter::ConvertUpcToBarcode("07222000516")
);
$this->assertEquals(
"0072220005165",
KrogerToGrocyConverter::ConvertUpcToBarcode("0000007222000516")
);
}
public function testConvertJson(): void
{
$testjson = file_get_contents(dirname(__FILE__) . "/testdata/receipts.json");
$default_quantity_units = 3;
$default_location_id = 2;
$products = KrogerToGrocyConverter::ConvertJson(json_decode($testjson, true), $default_quantity_units, $default_location_id);
$expectedjson = file_get_contents(dirname(__FILE__) . "/testdata/receipts_expected.json");
$expected = json_decode($expectedjson, true);
$this->assertEquals(count($expected), count($products));
foreach ($expected as $index => $product)
{
foreach ($product as $key => $value)
{
$this->assertEquals($value, $products[$index][$key]);
}
}
}
}
?>

118
tests/testdata/receipts.json vendored Normal file
View File

@ -0,0 +1,118 @@
{
"data": [{
"address": {
"address1": "8867 161st Ave NE",
"address2": "Redmond, WA 98052"
},
"items": [{
"itemIdentifier": "0004000052550",
"baseUpc": "0004000052550",
"department": "4",
"unitOfMeasure": "EA",
"detail": {
"upc": "0004000052550",
"customerFacingSize": "9.7 oz",
"description": "Twix Minis Size Chocolate Cookie Bars",
"familyTreeCommodityCode": "202",
"familyTreeDepartmentCode": "04",
"familyTreeSubCommodityCode": "3227",
"mainImage": "https://www.kroger.com/product/images/small/front/0004000052550",
"soldInStore": true,
"categories": [{
"category": "Candy",
"categoryCode": "77"
}
]
},
"quantity": 1,
"totalSavings": 1.79,
"extendedPrice": 4.29,
"pricePaid": 2.50,
"priceModifiers": [{
"type": "1",
"amount": 1.79,
"action": "Subtract",
"promotionId": "10000019468",
"couponType": "73",
"reportingCode": "0"
}
],
"unitPrice": 4.29,
"itemType": "NORMAL",
"entryMethod": "KEYED_ITEM_CODE",
"imageUrl": "https://www.kroger.com/product/images/small/front/0004000052550",
"detailUrl": "/p/item/0004000052550",
"sellableUpc": "0004000052550",
"fuel": false,
"giftCard": false,
"managerOverrideIgnored": false,
"containerDeposit": false,
"pharmacy": false,
"managerOverride": false,
"weighted": false
}, {
"itemIdentifier": "0007222000516",
"baseUpc": "0007222000516",
"department": "75",
"unitOfMeasure": "EA",
"detail": {
"upc": "0007222000516",
"customerFacingSize": "6 ct / 13 oz",
"description": "Franz Extra Crisp English Muffins",
"familyTreeCommodityCode": "050",
"familyTreeDepartmentCode": "75",
"familyTreeSubCommodityCode": "2407",
"mainImage": "https://www.kroger.com/product/images/small/front/0007222000516",
"soldInStore": true,
"categories": [{
"category": "Breakfast",
"categoryCode": "10"
}
]
},
"quantity": 3,
"totalSavings": 6.9,
"extendedPrice": 12.87,
"pricePaid": 5.97,
"priceModifiers": [{
"type": "1",
"amount": 2.3,
"action": "Subtract",
"promotionId": "10000013849",
"couponType": "73",
"reportingCode": "0"
}, {
"type": "1",
"amount": 2.3,
"action": "Subtract",
"promotionId": "10000013849",
"couponType": "73",
"reportingCode": "0"
}, {
"type": "1",
"amount": 2.3,
"action": "Subtract",
"promotionId": "10000013849",
"couponType": "73",
"reportingCode": "0"
}
],
"unitPrice": 4.29,
"itemType": "NORMAL",
"entryMethod": "KEYED_ITEM_CODE",
"imageUrl": "https://www.kroger.com/product/images/small/front/0007222000516",
"detailUrl": "/p/item/0007222000516",
"sellableUpc": "0007222000516",
"fuel": false,
"giftCard": false,
"managerOverrideIgnored": false,
"containerDeposit": false,
"pharmacy": false,
"managerOverride": false,
"weighted": false
}
],
"transactionTime": "2020-03-21T09:14:00"
}
]
}

18
tests/testdata/receipts_expected.json vendored Normal file
View File

@ -0,0 +1,18 @@
[{
"name": "Twix Minis Size Chocolate Cookie Bars",
"location_id": 2,
"qu_id_purchase": 3,
"qu_id_stock": 3,
"barcode": "0040000525509",
"quantity": 1,
"transaction_date": "2020-03-21"
}, {
"name": "Franz Extra Crisp English Muffins",
"location_id": 2,
"qu_id_purchase": 3,
"qu_id_stock": 3,
"barcode": "0072220005165",
"quantity": 3,
"transaction_date": "2020-03-21"
}
]