Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/unit/Money/MultipliedMoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class MultipliedMoneyTest extends TestCase
{
/**
* @param numeric-string $amountCents
* @dataProvider moneyParsingProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('moneyParsingProvider')]
public function testMultipliedMoneyParsing(string $amountCents, string $expectedCents, int $expectedMultiplier): void
{
$currencyCode = 'USD';
Expand All @@ -28,7 +28,7 @@ public function testMultipliedMoneyParsing(string $amountCents, string $expected
$this->assertSame($expectedMultiplier, $multipliedMoney->multiplier());
}

public function moneyParsingProvider(): Generator
public static function moneyParsingProvider(): Generator
{
yield ['0', '0', 1];
yield ['1', '100', 1];
Expand Down
22 changes: 7 additions & 15 deletions tests/unit/action/UsageIntervalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function testWholeMonthConstructor(): void
}


/**
* @dataProvider provideWithinMonth
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideWithinMonth')]
public function testWithinMonth(array $constructor, array $expectations): void
{
$month = new DateTimeImmutable($constructor['month']);
Expand All @@ -45,9 +43,7 @@ public function testWithinMonth(array $constructor, array $expectations): void
}


/**
* @dataProvider provideWithinMonth
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideWithinMonth')]
public function testWithMonthAndFraction(array $constructor, array $expectations): void
{
$month = new DateTimeImmutable($constructor['month']);
Expand All @@ -62,7 +58,7 @@ public function testWithMonthAndFraction(array $constructor, array $expectations
$this->assertSame($expectations['secondsInMonth'], $interval->secondsInMonth());
}

public function provideWithinMonth()
public static function provideWithinMonth()
{
yield 'For a start and end dates outside the month, the interval is the whole month' => [
['month' => '2023-02-01 00:00:00', 'start' => '2023-01-01 00:00:00', 'end' => '2023-10-01 00:00:00', 'fraction' => 1],
Expand Down Expand Up @@ -142,9 +138,7 @@ public function provideWithinMonth()
];
}

/**
* @dataProvider provideWithinMonthFailed
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideWithinMonthFailed')]
public function testWithinMonthFailed(array $constructor, array $expectations): void
{
$month = new DateTimeImmutable($constructor['month']);
Expand All @@ -157,7 +151,7 @@ public function testWithinMonthFailed(array $constructor, array $expectations):
UsageInterval::withinMonth($month, $start, $end);
}

public function provideWithinMonthFailed()
public static function provideWithinMonthFailed()
{
yield 'When a start date is greater than the end an exception is thrown' => [
['month' => '2023-02-01 00:00:00', 'start' => '2023-03-15 00:00:00', 'end' => '2023-02-15 00:00:00'],
Expand All @@ -168,9 +162,7 @@ public function provideWithinMonthFailed()
];
}

/**
* @dataProvider provideInvalidFractionOfMonthValues
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidFractionOfMonthValues')]
public function testWithMonthAndFractionInvalidValues(float $fractionOfMonth): void
{
$month = new DateTimeImmutable('2023-01-01');
Expand All @@ -182,7 +174,7 @@ public function testWithMonthAndFractionInvalidValues(float $fractionOfMonth): v
UsageInterval::withMonthAndFraction($month, $start, $fractionOfMonth);
}

public function provideInvalidFractionOfMonthValues(): array
public static function provideInvalidFractionOfMonthValues(): array
{
return [
[-0.1],
Expand Down
12 changes: 4 additions & 8 deletions tests/unit/charge/modifiers/OnceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ private function createPrice(TypeInterface $type): PriceInterface
return new SinglePrice(5, $type, $this->target, null, $this->prepaid, $this->money);
}

/**
* @dataProvider periodCreationProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('periodCreationProvider')]
public function testPeriodCreation(string $interval, string $expectedClass, int $expectedValue): void
{
$oncePeriod = $this->buildOnce($interval);
Expand All @@ -51,7 +49,7 @@ public function testPeriodCreation(string $interval, string $expectedClass, int
$this->assertSame($expectedValue, $period->getValue());
}

public function periodCreationProvider(): array
public static function periodCreationProvider(): array
{
return [
'yearly' => ['1 year', YearPeriod::class, 1],
Expand All @@ -64,17 +62,15 @@ protected function buildOnce(string $interval): Once
return (new Once())->per($interval);
}

/**
* @dataProvider fractionDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('fractionDataProvider')]
public function testFraction(string $interval): void
{
$this->expectException(FormulaEngineException::class);

$this->buildOnce($interval);
}

private function fractionDataProvider(): iterable
public static function fractionDataProvider(): iterable
{
yield ['1.5 months'];
yield ['day'];
Expand Down
79 changes: 45 additions & 34 deletions tests/unit/charge/modifiers/addons/DiscountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,90 @@
*/
class DiscountTest extends \PHPUnit\Framework\TestCase
{
protected $absolute;
protected $relative;
protected $rate = 11;
protected $sum = 1234;
protected $currency = 'USD';
private const int RATE = 11;

private const int SUM = 1234;

private Discount $absolute;

private Discount $relative;

protected function setUp(): void
{
$this->absolute = new Discount($this->sum/100 . ' ' . $this->currency);
$this->relative = new Discount($this->rate . '%');
$this->absolute = self::createAbsoluteDiscount();
$this->relative = self::createRelativeDiscount();
}

private static function createAbsoluteDiscount(): Discount
{
$currency = 'USD';

return new Discount(self::SUM / 100 . ' ' . $currency);
}

private static function createRelativeDiscount(): Discount
{
return new Discount(self::RATE . '%');
}

public function testEnsureValidValue()
public function testEnsureValidValue(): void
{
$money = Money::USD($this->sum);
$money = Money::USD(self::SUM);
$this->assertEquals($money, $this->absolute->getValue());
$this->assertEquals($this->rate, $this->relative->getValue());
$this->assertEquals(self::RATE, $this->relative->getValue());
}

public function testMultiply()
public function testMultiply(): void
{
$money = Money::USD($this->sum*10);
$money = Money::USD(self::SUM*10);
$this->assertEquals($money, $this->absolute->multiply(10)->getValue());
$this->assertEquals($this->rate*10, $this->relative->multiply(10)->getValue());
$this->assertEquals(self::RATE*10, $this->relative->multiply(10)->getValue());
}

public function badMultipliers()
public static function badMultipliers(): iterable
{
return [
['aasd'], ['10%'], [Money::USD(12)],
];
}

/**
* @dataProvider badMultipliers
*/
public function testMultiplyFailed($multiplier)
#[\PHPUnit\Framework\Attributes\DataProvider('badMultipliers')]
public function testMultiplyFailed($multiplier): void
{
$this->expectException(\Exception::class);
$this->absolute->multiply($multiplier);
}

public function testAdd()
public function testAdd(): void
{
$money = Money::USD($this->sum+10);
$money = Money::USD(self::SUM+10);
$this->assertEquals($money, $this->absolute->add(Money::USD(10))->getValue());
$this->assertEquals($this->rate+10, $this->relative->add(10)->getValue());
$this->assertEquals(self::RATE+10, $this->relative->add(10)->getValue());
}

public function badAddends()
public static function badAddends(): iterable
{
$this->setUp();
$relative = self::createRelativeDiscount();
$absolute = self::createAbsoluteDiscount();

return [
[$this->relative, 'aasd'],
[$this->relative, '10a'],
[$this->relative, Money::USD(12)],
[$this->absolute, 'aasd'],
[$this->absolute, '10b'],
[$this->absolute, 10],
[$relative, 'aasd'],
[$relative, '10a'],
[$relative, Money::USD(12)],
[$absolute, 'aasd'],
[$absolute, '10b'],
[$absolute, 10],
];
}

/**
* @dataProvider badAddends
*/
public function testAddFailed($discount, $addend)
#[\PHPUnit\Framework\Attributes\DataProvider('badAddends')]
public function testAddFailed($discount, $addend): void
{
$this->expectException(\Exception::class);
$discount->add($addend);
}

public function testCompare()
public function testCompare(): void
{
$money = Money::USD(1);
$this->assertTrue($this->absolute->compare($money) > 0);
Expand Down
12 changes: 4 additions & 8 deletions tests/unit/formula/FormulaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function checkSimpleInstallment($date, $num, $reason)
$this->assertNull($formula->getTill());
}

public function normalizeDataProvider()
public static function normalizeDataProvider()
{
return [
["ab\ncd", "ab\ncd"],
Expand All @@ -86,23 +86,19 @@ public function normalizeDataProvider()
];
}

/**
* @dataProvider normalizeDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('normalizeDataProvider')]
public function testNormalize($formula, $expected)
{
return $this->assertSame($expected, $this->engine->normalize($formula));
}

/**
* @dataProvider validateDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('validateDataProvider')]
public function testValidate($formula, $error)
{
return $this->assertSame($error, $this->engine->validate($formula));
}

public function validateDataProvider()
public static function validateDataProvider()
{
return [
['', "Unexpected token \"EOF\" (EOF) at line 1 and column 1:\n\n↑ : "],
Expand Down
Loading
Loading