Skip to content

Commit

Permalink
Fix build for Sylius 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed May 14, 2024
1 parent fb08556 commit c3eaecc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/Application/config/sylius/1.12/services_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:

sylius.behat.context.transform.lexical:
public: true
class: Tests\Sylius\AdminOrderCreationPlugin\Behat\Context\Transform\LexicalContext
49 changes: 49 additions & 0 deletions tests/Behat/Context/Transform/LexicalContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\AdminOrderCreationPlugin\Behat\Context\Transform;

use Behat\Behat\Context\Context;

final class LexicalContext implements Context
{
/**
* @Transform /^"(\-)?(?:€|£|¥|\$)([0-9,]+(\.[0-9]*)?)"$/
*/
public function getPriceFromString(string $sign, string $price): int
{
$this->validatePriceString($price);

$price = str_replace(',', '', $price);
$price = (int) round((float) $price * 100, 2);

if ('-' === $sign) {
$price *= -1;
}

return $price;
}

/**
* @Transform /^"((?:\d+\.)?\d+)%"$/
*/
public function getPercentageFromString(string $percentage): float
{
return (float) $percentage / 100;
}

/**
* @throws \InvalidArgumentException
*/
private function validatePriceString(string $price): void
{
if (!preg_match('/^.*\.\d{2}$/', $price)) {
throw new \InvalidArgumentException('The price string should have exactly 2 decimal digits.');
}

if (!preg_match('/^\d{1,3}(,\d{3})*\.\d{2}$/', $price)) {
throw new \InvalidArgumentException('Thousands and larger numbers should be separated by commas.');
}
}
}

0 comments on commit c3eaecc

Please sign in to comment.