Part-DB.Part-DB-server/tests/Services/Parts/PricedetailHelperTest.php

92 lines
3.4 KiB
PHP
Raw Permalink Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-01-05 15:55:16 +01:00
declare(strict_types=1);
2022-12-18 17:28:42 +01:00
namespace App\Tests\Services\Parts;
use App\Entity\Parts\Part;
use App\Entity\PriceInformations\Orderdetail;
use App\Entity\PriceInformations\Pricedetail;
2022-12-18 17:28:42 +01:00
use App\Services\Formatters\AmountFormatter;
use App\Services\Parts\PricedetailHelper;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PricedetailHelperTest extends WebTestCase
{
/**
* @var AmountFormatter
*/
protected $service;
2020-01-05 15:55:16 +01:00
protected function setUp(): void
{
2023-04-15 23:14:53 +02:00
//Get a service instance.
self::bootKernel();
2022-12-18 19:45:04 +01:00
$this->service = self::getContainer()->get(PricedetailHelper::class);
}
2020-03-29 22:47:25 +02:00
public function maxDiscountAmountDataProvider(): ?\Generator
{
$part = new Part();
yield [$part, null, 'Part without any orderdetails failed!'];
//Part with empty orderdetails
$part = new Part();
$orderdetail = new Orderdetail();
$part->addOrderdetail($orderdetail);
yield [$part, null, 'Part with one empty orderdetail failed!'];
$part = new Part();
$orderdetail = new Orderdetail();
$part->addOrderdetail($orderdetail);
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1));
yield [$part, 1.0, 'Part with one pricedetail failed!'];
$part = new Part();
$orderdetail = new Orderdetail();
$part->addOrderdetail($orderdetail);
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1));
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(2));
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1.5));
yield [$part, 2.0, 'Part with multiple pricedetails failed!'];
$part = new Part();
$orderdetail = new Orderdetail();
$orderdetail2 = new Orderdetail();
$part->addOrderdetail($orderdetail);
$part->addOrderdetail($orderdetail2);
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1));
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(2));
$orderdetail->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1.5));
$orderdetail2->addPricedetail((new Pricedetail())->setMinDiscountQuantity(10));
yield [$part, 10.0, 'Part with multiple orderdetails failed'];
}
/**
* @dataProvider maxDiscountAmountDataProvider
*/
2020-01-05 15:55:16 +01:00
public function testGetMaxDiscountAmount(Part $part, ?float $expected_result, string $message): void
{
2020-01-05 15:55:16 +01:00
$this->assertSame($expected_result, $this->service->getMaxDiscountAmount($part), $message);
}
}