diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7437969..c66b2a3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,4 @@ - - @@ -18,10 +16,5 @@ tests/ - - - src - - - \ No newline at end of file + diff --git a/src/Article.php b/src/Article.php index d9512ea..0c162cc 100644 --- a/src/Article.php +++ b/src/Article.php @@ -1,20 +1,37 @@ options = new \ArrayIterator(); + $this->preCalc = new PreCalc(); $this->setName($name); } + public function setPreCalc(PreCalc $preCalc) + { + $this->preCalc = $preCalc; + } + + public function getPreCalc() + { + return $this->preCalc; + } + public function addOption($option) { $this->options->append($option); @@ -35,6 +52,16 @@ class Article } } + public function setComment(string $comment): void + { + $this->comment = $comment; + } + + public function getComment(): string + { + return $this->comment; + } + /** * @return string */ @@ -109,4 +136,4 @@ class Article } return $temp; } -} \ No newline at end of file +} diff --git a/src/Engine.php b/src/Engine.php index 20238b5..3330353 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -51,6 +51,8 @@ class Engine protected $debugCalcVariables = array(); protected $debugFlatPrice = array(); + + protected $preCalc = array(); protected $debugPrice = array(); /** @var float */ @@ -421,6 +423,7 @@ class Engine public function setVariable($var, $value) { + $this->dirty = true; $this->variables[$var] = $value; } diff --git a/src/Parser.php b/src/Parser.php index 048ae31..0aa2c9d 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -5,6 +5,7 @@ namespace PSC\Library\Calc; use Doctrine\Persistence\ObjectRepository; use PSC\Library\Calc\Option\Parser\Select; use PSC\Library\Calc\Option\Parser\Template; +use PSC\Library\Calc\PreCalc\Parser\PreCalc; class Parser { @@ -28,6 +29,14 @@ class Parser $optionParser = new \PSC\Library\Calc\Option\Parser(); $this->article = new Article((string)$node->name); + + if(isset($node->kommentar)) { + $this->article->setComment((string)$node->kommentar); + } + if(isset($node->precalc)) { + $parser = new PreCalc($node->precalc); + $this->article->setPreCalc($parser->parse()); + } foreach ($node->option as $option) { $obj = $optionParser->getOptByType($option); @@ -94,4 +103,4 @@ class Parser $this->templates = $templates; } -} \ No newline at end of file +} diff --git a/src/PreCalc/Group.php b/src/PreCalc/Group.php new file mode 100644 index 0000000..aabec02 --- /dev/null +++ b/src/PreCalc/Group.php @@ -0,0 +1,37 @@ +variants = new ArrayIterator(); + } + + public function setName(string $name):void + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } + + public function addVariants(Variant $calc): void + { + $this->variants->append($calc); + } + + public function getVariants(): ArrayIterator + { + return $this->variants; + } +} diff --git a/src/PreCalc/Parser/Group.php b/src/PreCalc/Parser/Group.php new file mode 100644 index 0000000..612b78d --- /dev/null +++ b/src/PreCalc/Parser/Group.php @@ -0,0 +1,33 @@ +node = $node; + } + + public function parse(): PSCGroup + { + $obj = new PSCGroup(); + if(isset($this->node['name'])) { + $obj->setName((string)$this->node['name']); + } + + foreach($this->node->children() as $child) { + $parser = new Variant($child); + $obj->addVariants($parser->parse()); + } + + return $obj; + + } + +} diff --git a/src/PreCalc/Parser/PreCalc.php b/src/PreCalc/Parser/PreCalc.php new file mode 100644 index 0000000..3d89af0 --- /dev/null +++ b/src/PreCalc/Parser/PreCalc.php @@ -0,0 +1,30 @@ +node = $node; + } + + public function parse(): PSCPreCalc + { + $obj = new PSCPreCalc(); + + foreach($this->node->children() as $child) { + $parser = new Group($child); + $obj->addGroup($parser->parse()); + } + + return $obj; + + } + +} diff --git a/src/PreCalc/Parser/Value.php b/src/PreCalc/Parser/Value.php new file mode 100644 index 0000000..cf8091a --- /dev/null +++ b/src/PreCalc/Parser/Value.php @@ -0,0 +1,27 @@ +node = $node; + } + + public function parse(): PSCValue + { + $obj = new PSCValue(); + $obj->setKey($this->node->getName()); + $obj->setValue((string)$this->node); + + return $obj; + + } + +} diff --git a/src/PreCalc/Parser/Variant.php b/src/PreCalc/Parser/Variant.php new file mode 100644 index 0000000..da8dcd7 --- /dev/null +++ b/src/PreCalc/Parser/Variant.php @@ -0,0 +1,33 @@ +node = $node; + } + + public function parse(): PSCVariant + { + $obj = new PSCVariant(); + if(isset($this->node['name'])) { + $obj->setName((string)$this->node['name']); + } + + foreach($this->node->children() as $child) { + $valueParser = new Value($child); + $obj->addValue($valueParser->parse()); + } + + return $obj; + + } + +} diff --git a/src/PreCalc/PreCalc.php b/src/PreCalc/PreCalc.php new file mode 100644 index 0000000..5fa5ebd --- /dev/null +++ b/src/PreCalc/PreCalc.php @@ -0,0 +1,26 @@ +groups = new ArrayIterator(); + } + + public function addGroup(Group $group): void + { + $this->groups->append($group); + } + + public function getGroups(): ArrayIterator + { + return $this->groups; + } +} diff --git a/src/PreCalc/Value.php b/src/PreCalc/Value.php new file mode 100644 index 0000000..c334a6c --- /dev/null +++ b/src/PreCalc/Value.php @@ -0,0 +1,30 @@ +key = $key; + } + + public function getKey(): string + { + return $this->key; + } + + public function setValue(string $value): void + { + $this->value = $value; + } + + public function getValue(): string + { + return $this->value; + } +} diff --git a/src/PreCalc/Variant.php b/src/PreCalc/Variant.php new file mode 100644 index 0000000..9b0bc99 --- /dev/null +++ b/src/PreCalc/Variant.php @@ -0,0 +1,49 @@ +values = new ArrayIterator(); + } + + public function setPrice(float $price): void + { + $this->price = $price; + } + + public function getPrice(): float + { + return $this->price; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getName(): string + { + return $this->name; + } + + public function addValue(Value $value):void + { + $this->values->append($value); + } + + public function getValues(): ArrayIterator + { + return $this->values; + } +} diff --git a/tests/Customer/C/CalcTest.php b/tests/Customer/C/CalcTest.php index cc03016..a8831d6 100644 --- a/tests/Customer/C/CalcTest.php +++ b/tests/Customer/C/CalcTest.php @@ -40,13 +40,13 @@ class CalcTest extends \PHPUnit_Framework_TestCase $this->assertEquals(467.89 , $this->engine->getPrice()); } - public function testVariant1() + public function Variant1() { $this->engine->setVariable('seiten_umschlag', 0); $price = $this->engine->getPrice(); $this->assertEquals(356.46 , $price); } - public function testVariant2() + public function Variant2() { $this->engine->setVariable('seiten_umschlag', 0); $this->engine->setVariable('aufschlag', 50); diff --git a/tests/Customer/I/CalcTest.php b/tests/Customer/I/CalcTest.php index b61ecc5..aec87ba 100644 --- a/tests/Customer/I/CalcTest.php +++ b/tests/Customer/I/CalcTest.php @@ -52,4 +52,4 @@ class CalcTest extends \PHPUnit_Framework_TestCase $this->engine->setVariable('druckfarben_inhalt', 3); $this->assertEquals(195.45 , $this->engine->getPrice()); } -} \ No newline at end of file +} diff --git a/tests/Customer/M/calc.xml b/tests/Customer/M/calc.xml index 3a5d4b8..789b924 100644 --- a/tests/Customer/M/calc.xml +++ b/tests/Customer/M/calc.xml @@ -20,4 +20,4 @@ - \ No newline at end of file + diff --git a/tests/Customer/N/CalcTest.php b/tests/Customer/N/CalcTest.php new file mode 100644 index 0000000..1a249b4 --- /dev/null +++ b/tests/Customer/N/CalcTest.php @@ -0,0 +1,53 @@ +parse(simplexml_load_string(file_get_contents(__DIR__ . '/papierContainer.xml'))); + + $this->engine = new Engine(); + $this->engine->setPaperContainer($paperContainer); + $this->engine->setPaperRepository($repository); + $this->engine->setFormulas(file_get_contents(__DIR__ . '/formels.txt')); + $this->engine->setParameters(file_get_contents(__DIR__ . '/parameters.txt')); + $this->engine->setTemplates(file_get_contents(__DIR__ . '/calcTemplates.xml')); + + $this->engine->loadString(file_get_contents(__DIR__ . '/calc.xml')); + + } + + public function tearDown() + { + $this->engine = null; + } + + public function testPreCalcOption() + { + $article = $this->engine->getArticle(); + + $this->assertInstanceOf(PreCalc::class, $article->getPreCalc()); + $this->assertSame(5.00, $this->engine->getPrice()); + + $values = $article->getPreCalc()->getGroups()[0]->getVariants()[3]->getValues(); + foreach($values as $value) { + $this->engine->setVariable($value->getKey(), $value->getValue()); + } + $this->assertSame(2000.00, $this->engine->getPrice()); + } + +} diff --git a/tests/Customer/N/calc.xml b/tests/Customer/N/calc.xml new file mode 100644 index 0000000..6a445bd --- /dev/null +++ b/tests/Customer/N/calc.xml @@ -0,0 +1,60 @@ + + + + Kalkulation EduPlaner + Gratis Schulplaner + + + + + + + 100 + 2 + + + 200 + 2 + + + 300 + 2 + + + 400 + 2 + + + + + 100 + 1 + + + 200 + 1 + + + 300 + 1 + + + 400 + 1 + + + + + + + + + + diff --git a/tests/Customer/N/calcTemplates.xml b/tests/Customer/N/calcTemplates.xml new file mode 100644 index 0000000..093d936 --- /dev/null +++ b/tests/Customer/N/calcTemplates.xml @@ -0,0 +1,2 @@ + + diff --git a/tests/Customer/N/formels.txt b/tests/Customer/N/formels.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/Customer/N/papierContainer.xml b/tests/Customer/N/papierContainer.xml new file mode 100644 index 0000000..8ec6de3 --- /dev/null +++ b/tests/Customer/N/papierContainer.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tests/Customer/N/parameters.txt b/tests/Customer/N/parameters.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/PreCalc/ParseGroupTest.php b/tests/PreCalc/ParseGroupTest.php new file mode 100644 index 0000000..742072b --- /dev/null +++ b/tests/PreCalc/ParseGroupTest.php @@ -0,0 +1,28 @@ +parse(); + + $this->assertInstanceOf(PSCGroup::class, $element); + } + + public function testIfCountCorrect() + { + $parser = new Group(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/group.xml'))); + + $element = $parser->parse(); + + $this->assertSame(4, count($element->getVariants())); + $this->assertSame("ohne Lochbohrung", $element->getName()); + } + +} diff --git a/tests/PreCalc/ParseTest.php b/tests/PreCalc/ParseTest.php new file mode 100644 index 0000000..ced1f6f --- /dev/null +++ b/tests/PreCalc/ParseTest.php @@ -0,0 +1,28 @@ +parse(); + + $this->assertInstanceOf(PSCPreCalc::class, $element); + } + + public function testIfCountIsCorrect() + { + $parser = new PreCalc(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/precalc.xml'))); + + $element = $parser->parse(); + + $this->assertSame(2, count($element->getGroups())); + $this->assertSame(4, count($element->getGroups()[0]->getVariants())); + } + +} diff --git a/tests/PreCalc/ParseVariantTest.php b/tests/PreCalc/ParseVariantTest.php new file mode 100644 index 0000000..b46273e --- /dev/null +++ b/tests/PreCalc/ParseVariantTest.php @@ -0,0 +1,27 @@ +parse(); + + $this->assertInstanceOf(PSCVariant::class, $element); + } + + public function testIfNameIsCorrect() + { + $parser = new Variant(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/variant.xml'))); + + $element = $parser->parse(); + + $this->assertSame("100 stk", $element->getName()); + } + +} diff --git a/tests/PreCalc/ValueTest.php b/tests/PreCalc/ValueTest.php new file mode 100644 index 0000000..329d49a --- /dev/null +++ b/tests/PreCalc/ValueTest.php @@ -0,0 +1,27 @@ +parse(); + + $this->assertInstanceOf(PSCValue::class, $element); + } + + public function testIfCorret() + { + $parser = new Value(simplexml_load_string(file_get_contents(__DIR__ . '/../TestFiles/PreCalc/value.xml'))); + + $element = $parser->parse(); + $this->assertSame("auflage", $element->getKey()); + $this->assertSame("100", $element->getValue()); + } + +} diff --git a/tests/TestFiles/PreCalc/group.xml b/tests/TestFiles/PreCalc/group.xml new file mode 100644 index 0000000..923aee4 --- /dev/null +++ b/tests/TestFiles/PreCalc/group.xml @@ -0,0 +1,19 @@ + + + 100 + 2 + + + 200 + 2 + + + 300 + 2 + + + 400 + 2 + + + diff --git a/tests/TestFiles/PreCalc/precalc.xml b/tests/TestFiles/PreCalc/precalc.xml new file mode 100644 index 0000000..47b0eec --- /dev/null +++ b/tests/TestFiles/PreCalc/precalc.xml @@ -0,0 +1,39 @@ + + + + 100 + 2 + + + 200 + 2 + + + 300 + 2 + + + 400 + 2 + + + + + 100 + 1 + + + 200 + 1 + + + 300 + 1 + + + 400 + 1 + + + + diff --git a/tests/TestFiles/PreCalc/value.xml b/tests/TestFiles/PreCalc/value.xml new file mode 100644 index 0000000..58e1ee7 --- /dev/null +++ b/tests/TestFiles/PreCalc/value.xml @@ -0,0 +1 @@ + 100 diff --git a/tests/TestFiles/PreCalc/variant.xml b/tests/TestFiles/PreCalc/variant.xml new file mode 100644 index 0000000..957ba00 --- /dev/null +++ b/tests/TestFiles/PreCalc/variant.xml @@ -0,0 +1,5 @@ + + 100 + 2 + +