prestashop-module/tests/lib/RetailcrmInventoriesTest.php

130 lines
3.5 KiB
PHP
Raw Normal View History

2019-01-18 15:56:22 +03:00
<?php
class RetailcrmInventoriesTest extends RetailcrmTestCase
{
private $apiMock;
2019-01-21 14:42:19 +03:00
private $product1;
private $product2;
2021-11-03 16:19:39 +07:00
2019-01-21 14:42:19 +03:00
const PRODUCT1_QUANTITY = 10;
const PRODUCT2_QUANTITY = 15;
2021-11-03 16:19:39 +07:00
protected function setUp()
2019-01-18 15:56:22 +03:00
{
parent::setUp();
$this->apiMock = $this->getMockBuilder('RetailcrmProxy')
->disableOriginalConstructor()
->setMethods(
2021-11-03 16:19:39 +07:00
[
'storeInventories',
]
2019-01-18 15:56:22 +03:00
)
2021-11-03 16:19:39 +07:00
->getMock()
;
2019-01-21 14:42:19 +03:00
2019-01-18 15:56:22 +03:00
$catalog = new RetailcrmCatalog();
$data = $catalog->getData();
$this->product1 = $data[1]->current();
$data[1]->next();
$this->product2 = $data[1]->current();
2019-01-18 15:56:22 +03:00
}
/**
* @param $response
*
* @dataProvider dataProviderLoadStocks
*/
public function testLoadStocks($response)
2019-01-18 15:56:22 +03:00
{
2021-11-03 16:19:39 +07:00
if (true == $response['success']) {
2019-01-18 16:24:10 +03:00
$this->apiMock->expects($this->any())
2019-01-18 15:56:22 +03:00
->method('storeInventories')
2019-01-21 14:42:19 +03:00
->willReturn(
new RetailcrmApiResponse(
'200',
json_encode(
$this->getApiInventories()
)
)
2021-11-03 16:19:39 +07:00
)
;
2019-01-21 14:42:19 +03:00
} else {
2021-11-03 16:19:39 +07:00
$this->apiMock->expects($this->any())
->method('storeInventories')
->willReturn($response)
;
2019-01-21 14:42:19 +03:00
}
2019-01-18 15:56:22 +03:00
RetailcrmInventories::$api = $this->apiMock;
2019-01-21 14:42:19 +03:00
RetailcrmInventories::loadStocks();
$product1Id = explode('#', $this->product1['id']);
$product2Id = explode('#', $this->product2['id']);
2021-11-03 16:19:39 +07:00
if (isset($product1Id[1])) {
2019-01-21 16:50:46 +03:00
$prod1Quantity = StockAvailable::getQuantityAvailableByProduct($product1Id[0], $product1Id[1]);
} else {
$prod1Quantity = StockAvailable::getQuantityAvailableByProduct($product1Id[0], 0);
}
2021-11-03 16:19:39 +07:00
if (isset($product2Id[1])) {
2019-01-21 16:50:46 +03:00
$prod2Quantity = StockAvailable::getQuantityAvailableByProduct($product2Id[0], $product2Id[1]);
} else {
$prod2Quantity = StockAvailable::getQuantityAvailableByProduct($product2Id[0], 0);
}
2019-01-21 14:42:19 +03:00
$this->assertEquals(self::PRODUCT1_QUANTITY, $prod1Quantity);
$this->assertEquals(self::PRODUCT2_QUANTITY, $prod2Quantity);
2019-01-18 15:56:22 +03:00
}
public function dataProviderLoadStocks()
{
$response = $this->getResponseData();
2021-11-03 16:19:39 +07:00
return [
[
'response' => $response['true'],
],
[
'response' => $response['false'],
],
];
2019-01-18 15:56:22 +03:00
}
2019-01-21 14:42:19 +03:00
private function getResponseData()
{
2021-11-03 16:19:39 +07:00
return [
2019-01-21 14:42:19 +03:00
'true' => $this->getApiInventories(),
2021-11-03 16:19:39 +07:00
'false' => false,
];
2019-01-21 14:42:19 +03:00
}
private function getApiInventories()
{
2021-11-03 16:19:39 +07:00
return [
'success' => true,
'pagination' => [
'limit' => 250,
'totalCount' => 1,
'currentPage' => 1,
'totalPageCount' => 1,
],
'offers' => [
[
2019-01-21 14:42:19 +03:00
'externalId' => $this->product1['id'],
'xmlId' => 'xmlId',
'quantity' => self::PRODUCT1_QUANTITY,
2021-11-03 16:19:39 +07:00
],
[
2019-01-21 14:42:19 +03:00
'externalId' => $this->product2['id'],
'xmlId' => 'xmlId',
'quantity' => self::PRODUCT2_QUANTITY,
2021-11-03 16:19:39 +07:00
],
],
];
2019-01-21 14:42:19 +03:00
}
}