HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Shop/Tests/Unit/VatRateTest.php
<?php

namespace App\Komma\Shop\Tests\Unit;

use App\Komma\Shop\Cart\ShoppingCartServiceInterface;
use App\Komma\Shop\Checkout\CheckoutServiceInterface;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Tests\TestCase;
use App\Komma\Shop\Vat\VatService;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class VatRateTest extends TestCase
{
    use DatabaseTransactions; //Automatically rolls back database actions after tests

    /**
     * @var VatService
     */
    private $vatService;

    /**
     * @var ShoppingCartServiceInterface
     */
    private $shoppingCartService;

    /**
     * @var CheckoutServiceInterface
     */
    private $checkoutService;

    /**
     * Runs before each test
     */
    public function setUp(): void
    {
        parent::setUp();
        $this->vatService = new VatService();
        $this->shoppingCartService = app(ShoppingCartServiceInterface::class);
        $this->checkoutService = app(CheckoutServiceInterface::class);
    }

    /**
     * @group VatRate
     * @test
     */
    public function testOrderVatCalculation()
    {
        $order = $this->createSampleOrder();
        $expectedVatTotal = round($order->total_price_ex * 0.21, 0, config('financial.cent_rounding.vat_total', PHP_ROUND_HALF_UP)); //Default vat percentage. Check  SampleVatRates seed.
        $expectedTotalIncVat = round($order->total_price_ex * 1.21, 0, config('financial.cent_rounding.vat_total', PHP_ROUND_HALF_UP)); //Default vat percentage. Check  SampleVatRates seed.

        $this->vatService->calculateOrderVatTotal($order);
        $this->assertEquals($expectedVatTotal, $order->vat_amount);
        $this->assertEquals($expectedTotalIncVat, $order->total_price);
        $this->assertEquals(21, $order->vat_percentage);
    }

    /**
     * Test calculating a certain price including vat rate.
     *
     * @group VatRate
     * @test
     */
    public function testCalculatingIncVatRatePrice()
    {
        $this->assertEquals(1490, $this->vatService->calculateIncVatRatePrice(1231));
    }

    /**
     * Test calculating a certain price including vat rate.
     *
     * @group VatRate
     * @test
     */
    public function testCalculatingExVatRatePrice()
    {
        $this->assertEquals(1231, $this->vatService->calculateExVatRatePrice(1490));
    }

    /**
     * Test calculating a certain price including vat rate.
     *
     * @group VatRate
     * @test
     */
    public function testCalculatingVatRateAmountFromExAmount()
    {
        $priceEx = 1231;
        $vatRateAmount = $this->vatService->calculateVatRateAmountFromExAmount($priceEx);
        $this->assertEquals(259, $vatRateAmount);
        //Verify the result by doing the reverse
        $this->assertEquals($priceEx + $vatRateAmount, $this->vatService->calculateIncVatRatePrice($priceEx));
    }

    /**
     * Test calculating a certain price including vat rate.
     *
     * @group VatRate
     * @test
     */
    public function testCalculatingVatRateAmountFromIncAmount()
    {
        $priceInc = 1490;
        $vatRateAmount = $this->vatService->calculateVatRateAmountFromIncAmount($priceInc);
        $this->assertEquals(259, $vatRateAmount);
        //Verify the result by doing the reverse
        $this->assertEquals($priceInc - $vatRateAmount, $this->vatService->calculateExVatRatePrice($priceInc));
    }

    /**
     * Test calculation of vat for a product or productable.
     * The productable will be given vat details. Those details are not to be saved into the database.
     * Just for carrying the information around.
     *
     * @group VatRate
     * @test
     */
    public function TestCalculatingVatForProductable()
    {
        $sampleProduct = new Product();
        $sampleProduct->price = 822;

        $sampleProduct = $this->vatService->calculateVatForProductable($sampleProduct);
        $this->assertEquals(173, $sampleProduct->vat_amount);
        $this->assertEquals(995, $sampleProduct->price_inc);
    }

    //HELPER FUNCTIONS
    /**
     * @return Order
     */
    public function createSampleOrder()
    {
        /** @var Order $order */
        $order = factory(Order::class)->create();
        $order->total_price_ex = mt_rand(100, 6599);

        return $order;
    }
}