Commit 0eb3b037 authored by Karamel's avatar Karamel
Browse files

Add a composition test and some fixes.

parent c8b556d8
......@@ -14,6 +14,7 @@ The semantic of version number is 'Level.Version'. Level is for compatibility be
### Fixed
- Sqlite support.
- Compositions from struct (http calls).
## [8.0-alpha11] - 2019-08-26
......
......@@ -119,9 +119,14 @@ class CompositionGroup extends DoctrineModel
public function clearCompositionProducts() {
$this->getCompositionProducts()->clear();
}
public function addCompositionProducts($compositionProduct) {
public function addCompositionProduct($compositionProduct) {
$this->compositionProducts->add($compositionProduct);
$compositionProduct->setCompositionGroup($this);
}
public function removeCompositionProduct($prd) {
$this->compositionProducts->removeElement($prd);
$prd->setCompositionGroup(null);
}
}
......@@ -51,7 +51,7 @@ class CompositionProduct extends DoctrineModel
public function getId() {
if ($this->getCompositionGroup() === null) {
return ['compositionGroup' => $this->getCompositionGroup()->getId(),
return ['compositionGroup' => null,
'product' => $this->getProduct()->getId()];
} else {
return ['compositionGroup' => $this->getCompositionGroup()->getId(),
......
......@@ -299,6 +299,10 @@ class Product extends DoctrineModel
$this->compositionGroups->add($compositionGroup);
$compositionGroup->setProduct($this);
}
public function removeCompositionGroup($compositionGroup) {
$this->compositionGroups->removeElement($compositionGroup);
$compositionGroup->setProduct(null);
}
/** Get sell price with tax.
* Virtual field passed along toStruct at current time.*/
......
......@@ -22,6 +22,8 @@
namespace Pasteque\Server;
use \Pasteque\Server\Model\Category;
use \Pasteque\Server\Model\CompositionGroup;
use \Pasteque\Server\Model\CompositionProduct;
use \Pasteque\Server\Model\Product;
use \Pasteque\Server\Model\Tax;
use \Pasteque\Server\System\Login;
......@@ -99,6 +101,49 @@ class HttpProductTest extends TestCase
$this->assertEquals(1, count($dbPrd));
}
/** @depends testPutOk */
public function testPutCompo() {
$prd = new Product();
$prd->setCategory($this->cat);
$prd->setTax($this->tax);
$prd->setReference('ref');
$prd->setLabel('label');
$prd->setPriceSell(10);
$this->dao->write($prd);
$this->dao->commit();
$compo = new Product();
$compo->setCategory($this->cat);
$compo->setTax($this->tax);
$compo->setReference('compo');
$compo->setLabel('Compo');
$compo->setPriceSell(15);
$compo->setComposition(true);
$grp = new CompositionGroup();
$grp->setLabel('Group');
$grpPrd = new CompositionProduct();
$grpPrd->setProduct($prd);
$grp->addCompositionProduct($grpPrd);
$compo->addCompositionGroup($grp);
$struct = $compo->toStruct();
unset($struct['id']);
unset($struct['compositionGroups'][0]['id']);
unset($struct['compositionGroups'][0]['compositionProducts'][0]['id']);
var_dump($struct);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/product/%s', urlencode($compo->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
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));
$dbPrd = $this->dao->search(Product::class,
new DAOCondition('reference', '=', $compo->getReference()));
$this->assertEquals(1, count($dbPrd));
}
public function testPutId() {
$prd = new Product();
$prd->setCategory($this->cat);
......
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