Commit bbaca06e authored by Karamel's avatar Karamel
Browse files

Fix crashing toStruct when deleting an element of an array which is not the last one.

Add some TariffArea test for it.
parent 93ab1162
......@@ -77,11 +77,11 @@ abstract class DoctrineModel
if (!empty($field['array'])) {
// Value is a ArrayCollection
$struct = array();
for ($i = 0; $i < $value->count(); $i++) {
foreach ($value as $v) {
if (empty($field['embedded'])) {
$struct[] = $value->get($i)->getId();
$struct[] = $v->getId();
} else {
$struct[] = $value->get($i)->toStruct();
$struct[] = $v->toStruct();
}
}
return $struct;
......
......@@ -102,6 +102,29 @@ class TariffAreaAPITest extends TestCase
$this->assertEquals($this->product->getId(), $id['product']);
}
/** @depends testPrice */
public function testRemovePrice() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Area');
$price = new TariffAreaPrice();
$price->setProduct($this->product);
$price->setPrice(0.5);
$ta->addPrice($price);
$price2 = new TariffAreaPrice();
$price2->setProduct($this->product2);
$price2->setPrice(0.2);
$ta->addPrice($price2);
$this->api->write($ta);
$ta->removePrice($price);
$this->api->write($ta);
$read = $this->dao->readSnapshot(TariffArea::class, $ta->getId());
$this->assertEquals($ta->getReference(), $read->getReference());
$this->assertEquals($ta->getLabel(), $read->getLabel());
$this->assertEquals(1, count($read->getPrices()));
$this->assertEquals(0.2, $read->getPrices()->get(0)->getPrice());
}
/** @depends testPrice */
public function testDeleteCascade() {
$ta = new TariffArea();
......
......@@ -41,6 +41,7 @@ class HttpTariffAreaTest extends TestCase
private $cat;
private $tax;
private $prd;
private $prd2;
public static function setUpBeforeClass() {
static::$token = obtainToken();
......@@ -70,7 +71,14 @@ class HttpTariffAreaTest extends TestCase
$this->prd->setCategory($this->cat);
$this->prd->setTax($this->tax);
$this->prd->setPriceSell(1.0);
$this->prd2 = new Product();
$this->prd2->setReference('product2');
$this->prd2->setLabel('Product2');
$this->prd2->setCategory($this->cat);
$this->prd2->setTax($this->tax);
$this->prd2->setPriceSell(0.5);
$this->dao->write($this->prd);
$this->dao->write($this->prd2);
$this->dao->commit();
}
......@@ -185,6 +193,36 @@ class HttpTariffAreaTest extends TestCase
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testUpdatePrices() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$taPrice2 = new TariffAreaPrice();
$taPrice2->setPrice(0.2);
$taPrice2->setProduct($this->prd2);
$ta->addPrice($taPrice2);
$this->dao->write($ta);
$this->dao->commit();
$struct = $ta->toStruct();
array_splice($struct['prices'], 0, 1);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/tariffarea'));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$read = $this->dao->readSnapshot(TariffArea::class, $ta->getId());
$this->assertEquals(1, count($read->getPrices()));
$this->assertEquals(0.2, $read->getPrices()->get(0)->getPrice());
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testDelete() {
$ta = new TariffArea();
$ta->setReference('ta');
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment