File: D:/HostingSpaces/blijegasten/blijegasten.be/app/Komma/Shop/Tests/Browser/VatRateTest.php
<?php
namespace App\Komma\Shop\Tests\Browser;
use App\Http\Middleware\WildcardResolver;
use App\Komma\Globalization\RegionInfoInterface;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\Product\ProductTranslation;
use App\Komma\Shop\Tests\Browser\Pages\VatRateSectionTestPage;
use App\Komma\Shop\Tests\DuskTestCase;
use App\Komma\Shop\Vat\VatServiceInterface;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Models\KmsUser;
use Laravel\Dusk\Browser;
class VatRateTest extends DuskTestCase
{
/**
* Test creating an empty vat rate
*
* @group VatRate
* @test
* @throws \Throwable
*/
public function createEmptyVatRate()
{
$this->browse(function (Browser $browser) {
$browser->loginAs(KmsUser::find(1), 'kms')
->screenshot($this->getName(false))
->visit(new VatRateSectionTestPage())
->assertSee(__('shop/vatrates.section.title'))
->click('@add_button')
->click('@save_button')
->assertSee(__('validation.required', ['attribute' => __('shop/vatrates.name')]))
->assertSee(__('validation.required', ['attribute' => __('shop/vatrates.percentage')]));
});
}
/**
* Test creating an empty vat rate
*
* @group VatRate
* @test
* @throws \Throwable
*/
public function testCreateVatRateWithNameAndPercentageWithPercentSign()
{
$this->browse(function (Browser $browser) {
$browser->loginAs(KmsUser::find(1), 'kms')
->screenshot($this->getName(false))
->visit(new VatRateSectionTestPage())
->assertSee(__('shop/vatrates.section.title'))
->click('@add_button')
->type('TextField-name', 'Dusk vat rate '.mt_rand(11111, 99999))
->type('TextField-percentage', '30%')
->click('@save_button')
->assertSee(__('shop/vatrates.validation.percentage')); //Should not be saved because the % or any other non numeric character must not be added
});
}
/**
* Test creating an empty vat rate
*
* @group VatRate
* @test
* @throws \Throwable
*/
public function testCreateAndDeleteVatRate()
{
//Fake a name for the vat rate
$name = 'Dusk vat rate '.mt_rand(11111, 99999);
//Create the vat rate
$this->browse(function (Browser $browser) use($name) {
$browser->loginAs(KmsUser::find(1), 'kms')
->screenshot($this->getName(false))
->visit(new VatRateSectionTestPage())
->assertSee(__('shop/vatrates.section.title'))
->click('@add_button')
->type('TextField-name', $name)
->type('TextField-percentage', '1')
->click('@save_button')
->assertSee(__('kms/global.saved'));
});
//Delete the vat rate
$counter = 1;
$this->browse(function (Browser $browser) use ($name, $counter) {
$browser->visit(new VatRateSectionTestPage())
->type('@entity_search_input', $name)
->screenshot($this->getName(false).'-'.++$counter)
->assertSeeIn('@search-result-counter', '1')
->pause(100)
->click('@found_search_item')
->screenshot($this->getName(false).'-'.++$counter)
->assertSeeIn('@entity_header', $name)
->click('@delete_button')
->assertSee(__('kms/global.confirm_deletion'))
->click('@confirmation_confirm')
->assertSee(__('kms/global.removed'))
->screenshot($this->getName(false).'-'.++$counter);
});
}
/**
* Test the vat display of a product in the site catalog
*
* @group VatRate
* @test
* @throws \Throwable
*/
public function testCatalogVat() {
//Initialize some services that are needed
$vatService = app()->make(VatServiceInterface::class);
$regionInfo = app(RegionInfoInterface::class);
//Set the current site to the default one.
/** @var SiteServiceInterface $siteService */
$siteService = app(SiteServiceInterface::class);
$siteService->setCurrentSiteToDefault();
//Get a product
/** @var Product $product */
$product = Product::wherehas('translations')->with('translations.language')->first();
$this->assertInstanceOf(Product::class, $product);
//Construct the catalog url fo the product details
/** @var ProductTranslation $translation */
$translation = $product->translations->first();
$baseRoute = WildcardResolver::getWildCardIndexRouteForPageWithCodeName('products');
$url = $baseRoute->alias.'/'.$translation->slug;
//Calculate model vat and put it in temporary variables in the product
$vatService->calculateVatForProductable($product);
//Go to the product detail page
$this->browse(function(Browser $browser) use($url, $product, $regionInfo) {
$browser->visit($url)
->assertSee($regionInfo->getCurrencySymbol().' '.$regionInfo->getNumberFormat()->centsToCurrency($product->vat_amount))
->assertSee($regionInfo->getCurrencySymbol().' '.$regionInfo->getNumberFormat()->centsToCurrency($product->price))
->assertSee($regionInfo->getCurrencySymbol().' '.$regionInfo->getNumberFormat()->centsToCurrency($product->price_inc));
});
}
}