From 529183a8211dc02f382d1b96d964c555dc52a1ec Mon Sep 17 00:00:00 2001 From: Thomas Peterson Date: Wed, 18 Mar 2020 23:10:50 +0100 Subject: [PATCH] Add Saved Values vom DB --- src/Engine.php | 8 ++++ src/Option/Type/Base.php | 17 ++++++++ src/Option/Type/PaperDbSelect.php | 32 ++++++++++++++- tests/Complex/PaperSavedTest.php | 56 ++++++++++++++++++++++++++ tests/Legacy/selectWithGrenzenTest.php | 2 + tests/TestFiles/General/complete1.xml | 2 +- 6 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 tests/Complex/PaperSavedTest.php diff --git a/src/Engine.php b/src/Engine.php index 531a4d5..dd9262e 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -55,6 +55,8 @@ class Engine /** @var \SimpleXMLElement $templates */ protected $templates; + protected $savedCalcValues = []; + /** * Load XML From String * @@ -217,6 +219,8 @@ class Engine // Prefill with defaults /** @var Base $option */ foreach($this->article->getOptions() as $option) { + $option->setSavedCalcValues($this->savedCalcValues); + if(!isset($this->variables[$option->getId()]) && $option->getDefault() !== null && !$option instanceof Text) { $this->variables[$option->getId()] = $option->getDefault(); } @@ -381,4 +385,8 @@ class Engine return $this->templates; } + public function setSavedCalcReferences($values) + { + $this->savedCalcValues = $values; + } } \ No newline at end of file diff --git a/src/Option/Type/Base.php b/src/Option/Type/Base.php index f28e872..0a082d6 100644 --- a/src/Option/Type/Base.php +++ b/src/Option/Type/Base.php @@ -38,6 +38,7 @@ class Base /** @var bool */ protected $isAjaxExport = false; + protected $savedCalcValues = []; public function __construct() { @@ -232,4 +233,20 @@ class Base { return $variables; } + + /** + * @return array + */ + public function getSavedCalcValues() + { + return $this->savedCalcValues; + } + + /** + * @param array $savedCalcValues + */ + public function setSavedCalcValues($savedCalcValues) + { + $this->savedCalcValues = $savedCalcValues; + } } \ No newline at end of file diff --git a/src/Option/Type/PaperDbSelect.php b/src/Option/Type/PaperDbSelect.php index cfb10a5..e5dc2d6 100644 --- a/src/Option/Type/PaperDbSelect.php +++ b/src/Option/Type/PaperDbSelect.php @@ -7,12 +7,42 @@ use PSC\Library\Calc\Tests\Mock\Paper; class PaperDbSelect extends Select { + public function getSelectedOption() + { + /** @var Opt $opt */ + foreach($this->getOptions() as $opt) { + if($opt->isSelected()) return $opt; + } + + if(isset($this->savedCalcValues[$this->getId()]) && $this->savedCalcValues[$this->getId()]['art_nr'] == $this->getRawValue()) { + $opt = new PaperOpt(); + $opt->setIsSelected(true); + $opt->setId($this->savedCalcValues[$this->getId()]['art_nr']); + $opt->setLabel($this->savedCalcValues[$this->getId()]['description_1']); + + $paper = new Paper(); + $paper->setId($this->savedCalcValues[$this->getId()]['id']); + $paper->setArtNr($this->savedCalcValues[$this->getId()]['art_nr']); + $paper->setDescription1($this->savedCalcValues[$this->getId()]['description_1']); + $paper->setDescription2($this->savedCalcValues[$this->getId()]['description_2']); + $paper->setPreis($this->savedCalcValues[$this->getId()]['preis']); + $paper->setGrammatur($this->savedCalcValues[$this->getId()]['grammatur']); + $paper->setOffsetFix($this->savedCalcValues[$this->getId()]['offset_fix']); + $paper->setOffsetVar($this->savedCalcValues[$this->getId()]['offset_var']); + $paper->setDigitalFix($this->savedCalcValues[$this->getId()]['digital_fix']); + $paper->setDigitalVar($this->savedCalcValues[$this->getId()]['digital_var']); + $paper->setVolume($this->savedCalcValues[$this->getId()]['volume']); + + $opt->setPaper($paper); + + return $opt; + } + } public function parseAdditionalValues($variables) { /** @var PaperOpt $option */ $option = $this->getSelectedOption(); - if($option == null) { $variables[$this->getId() . '_grammatur'] = 0; diff --git a/tests/Complex/PaperSavedTest.php b/tests/Complex/PaperSavedTest.php new file mode 100644 index 0000000..77ad138 --- /dev/null +++ b/tests/Complex/PaperSavedTest.php @@ -0,0 +1,56 @@ +parse(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/Complex/papierContainer.xml'))); + + $this->engine = new Engine(); + $this->engine->setPaperContainer($paperContainer); + $this->engine->setPaperRepository($repository); + $this->engine->setFormulas(file_get_contents(__DIR__.'/../TestFiles/Complex/formels.txt')); + $this->engine->setParameters(file_get_contents(__DIR__.'/../TestFiles/Complex/parameters.txt')); + + $this->engine->loadString(file_get_contents(__DIR__ . '/../TestFiles/Complex/1.xml')); + + } + + public function tearDown() + { + $this->engine = null; + } + + public function testIfArticleCountIsCorrect() + { + $this->assertEquals(1, $this->engine->getArticles()->Count()); + } + + public function testIfDefaultPriceIsOk() + { + $this->engine->setVariable('papier', 'INM137'); + + $this->engine->setSavedCalcReferences([ "papier" => + [ "id" => "5039", "install_id" => "1", "uuid" => "0001-00000000-4bfaf9c3-7158-bce189cb", + "art_nr" => "INM137", "description_1" => "Bilderdruck matt 250 gr", + "description_2" => "Bilderdruck matt 135 gr", "grammatur" => "135", "preis" => "28", + "offset_fix" => "0", "offset_var" => "0", "digital_fix" => "0", "digital_var" => "0", "volume" => "0.118" ] ]); + + $this->assertEquals(162.19, $this->engine->getPrice()); + } +} \ No newline at end of file diff --git a/tests/Legacy/selectWithGrenzenTest.php b/tests/Legacy/selectWithGrenzenTest.php index 6d20532..3d006fe 100644 --- a/tests/Legacy/selectWithGrenzenTest.php +++ b/tests/Legacy/selectWithGrenzenTest.php @@ -50,6 +50,8 @@ class selectWithGrenzenTest extends \PHPUnit_Framework_TestCase $this->assertCount(6, $article->getValidOptions()); $this->assertCount(6, $article->getOptionsAsArray()); + + $this->assertInstanceOf('\PSC\Library\Calc\Option\Type\Select\Opt', $article->getOptionById('umschlag')->getSelectedOption()); } } diff --git a/tests/TestFiles/General/complete1.xml b/tests/TestFiles/General/complete1.xml index 756ca7b..55f1ca7 100644 --- a/tests/TestFiles/General/complete1.xml +++ b/tests/TestFiles/General/complete1.xml @@ -19,7 +19,7 @@