Skip to content

Commit 6015ba8

Browse files
Merge pull request #110 from VadymHrechukha/HP-2818_fix_falling_phpunit_tests_during_build_php-billing_package
HP-2818: Fix falling PHPUnit tests during build php-billing package
2 parents ab843be + 3673260 commit 6015ba8

File tree

9 files changed

+99
-114
lines changed

9 files changed

+99
-114
lines changed

tests/unit/Money/MultipliedMoneyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class MultipliedMoneyTest extends TestCase
1717
{
1818
/**
1919
* @param numeric-string $amountCents
20-
* @dataProvider moneyParsingProvider
2120
*/
21+
#[\PHPUnit\Framework\Attributes\DataProvider('moneyParsingProvider')]
2222
public function testMultipliedMoneyParsing(string $amountCents, string $expectedCents, int $expectedMultiplier): void
2323
{
2424
$currencyCode = 'USD';
@@ -28,7 +28,7 @@ public function testMultipliedMoneyParsing(string $amountCents, string $expected
2828
$this->assertSame($expectedMultiplier, $multipliedMoney->multiplier());
2929
}
3030

31-
public function moneyParsingProvider(): Generator
31+
public static function moneyParsingProvider(): Generator
3232
{
3333
yield ['0', '0', 1];
3434
yield ['1', '100', 1];

tests/unit/action/UsageIntervalTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public function testWholeMonthConstructor(): void
2121
}
2222

2323

24-
/**
25-
* @dataProvider provideWithinMonth
26-
*/
24+
#[\PHPUnit\Framework\Attributes\DataProvider('provideWithinMonth')]
2725
public function testWithinMonth(array $constructor, array $expectations): void
2826
{
2927
$month = new DateTimeImmutable($constructor['month']);
@@ -45,9 +43,7 @@ public function testWithinMonth(array $constructor, array $expectations): void
4543
}
4644

4745

48-
/**
49-
* @dataProvider provideWithinMonth
50-
*/
46+
#[\PHPUnit\Framework\Attributes\DataProvider('provideWithinMonth')]
5147
public function testWithMonthAndFraction(array $constructor, array $expectations): void
5248
{
5349
$month = new DateTimeImmutable($constructor['month']);
@@ -62,7 +58,7 @@ public function testWithMonthAndFraction(array $constructor, array $expectations
6258
$this->assertSame($expectations['secondsInMonth'], $interval->secondsInMonth());
6359
}
6460

65-
public function provideWithinMonth()
61+
public static function provideWithinMonth()
6662
{
6763
yield 'For a start and end dates outside the month, the interval is the whole month' => [
6864
['month' => '2023-02-01 00:00:00', 'start' => '2023-01-01 00:00:00', 'end' => '2023-10-01 00:00:00', 'fraction' => 1],
@@ -142,9 +138,7 @@ public function provideWithinMonth()
142138
];
143139
}
144140

145-
/**
146-
* @dataProvider provideWithinMonthFailed
147-
*/
141+
#[\PHPUnit\Framework\Attributes\DataProvider('provideWithinMonthFailed')]
148142
public function testWithinMonthFailed(array $constructor, array $expectations): void
149143
{
150144
$month = new DateTimeImmutable($constructor['month']);
@@ -157,7 +151,7 @@ public function testWithinMonthFailed(array $constructor, array $expectations):
157151
UsageInterval::withinMonth($month, $start, $end);
158152
}
159153

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

171-
/**
172-
* @dataProvider provideInvalidFractionOfMonthValues
173-
*/
165+
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidFractionOfMonthValues')]
174166
public function testWithMonthAndFractionInvalidValues(float $fractionOfMonth): void
175167
{
176168
$month = new DateTimeImmutable('2023-01-01');
@@ -182,7 +174,7 @@ public function testWithMonthAndFractionInvalidValues(float $fractionOfMonth): v
182174
UsageInterval::withMonthAndFraction($month, $start, $fractionOfMonth);
183175
}
184176

185-
public function provideInvalidFractionOfMonthValues(): array
177+
public static function provideInvalidFractionOfMonthValues(): array
186178
{
187179
return [
188180
[-0.1],

tests/unit/charge/modifiers/OnceTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ private function createPrice(TypeInterface $type): PriceInterface
4040
return new SinglePrice(5, $type, $this->target, null, $this->prepaid, $this->money);
4141
}
4242

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

54-
public function periodCreationProvider(): array
52+
public static function periodCreationProvider(): array
5553
{
5654
return [
5755
'yearly' => ['1 year', YearPeriod::class, 1],
@@ -64,17 +62,15 @@ protected function buildOnce(string $interval): Once
6462
return (new Once())->per($interval);
6563
}
6664

67-
/**
68-
* @dataProvider fractionDataProvider
69-
*/
65+
#[\PHPUnit\Framework\Attributes\DataProvider('fractionDataProvider')]
7066
public function testFraction(string $interval): void
7167
{
7268
$this->expectException(FormulaEngineException::class);
7369

7470
$this->buildOnce($interval);
7571
}
7672

77-
private function fractionDataProvider(): iterable
73+
public static function fractionDataProvider(): iterable
7874
{
7975
yield ['1.5 months'];
8076
yield ['day'];

tests/unit/charge/modifiers/addons/DiscountTest.php

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,79 +18,90 @@
1818
*/
1919
class DiscountTest extends \PHPUnit\Framework\TestCase
2020
{
21-
protected $absolute;
22-
protected $relative;
23-
protected $rate = 11;
24-
protected $sum = 1234;
25-
protected $currency = 'USD';
21+
private const int RATE = 11;
2622

23+
private const int SUM = 1234;
24+
25+
private Discount $absolute;
26+
27+
private Discount $relative;
28+
2729
protected function setUp(): void
2830
{
29-
$this->absolute = new Discount($this->sum/100 . ' ' . $this->currency);
30-
$this->relative = new Discount($this->rate . '%');
31+
$this->absolute = self::createAbsoluteDiscount();
32+
$this->relative = self::createRelativeDiscount();
33+
}
34+
35+
private static function createAbsoluteDiscount(): Discount
36+
{
37+
$currency = 'USD';
38+
39+
return new Discount(self::SUM / 100 . ' ' . $currency);
40+
}
41+
42+
private static function createRelativeDiscount(): Discount
43+
{
44+
return new Discount(self::RATE . '%');
3145
}
3246

33-
public function testEnsureValidValue()
47+
public function testEnsureValidValue(): void
3448
{
35-
$money = Money::USD($this->sum);
49+
$money = Money::USD(self::SUM);
3650
$this->assertEquals($money, $this->absolute->getValue());
37-
$this->assertEquals($this->rate, $this->relative->getValue());
51+
$this->assertEquals(self::RATE, $this->relative->getValue());
3852
}
3953

40-
public function testMultiply()
54+
public function testMultiply(): void
4155
{
42-
$money = Money::USD($this->sum*10);
56+
$money = Money::USD(self::SUM*10);
4357
$this->assertEquals($money, $this->absolute->multiply(10)->getValue());
44-
$this->assertEquals($this->rate*10, $this->relative->multiply(10)->getValue());
58+
$this->assertEquals(self::RATE*10, $this->relative->multiply(10)->getValue());
4559
}
4660

47-
public function badMultipliers()
61+
public static function badMultipliers(): iterable
4862
{
4963
return [
5064
['aasd'], ['10%'], [Money::USD(12)],
5165
];
5266
}
5367

54-
/**
55-
* @dataProvider badMultipliers
56-
*/
57-
public function testMultiplyFailed($multiplier)
68+
#[\PHPUnit\Framework\Attributes\DataProvider('badMultipliers')]
69+
public function testMultiplyFailed($multiplier): void
5870
{
5971
$this->expectException(\Exception::class);
6072
$this->absolute->multiply($multiplier);
6173
}
6274

63-
public function testAdd()
75+
public function testAdd(): void
6476
{
65-
$money = Money::USD($this->sum+10);
77+
$money = Money::USD(self::SUM+10);
6678
$this->assertEquals($money, $this->absolute->add(Money::USD(10))->getValue());
67-
$this->assertEquals($this->rate+10, $this->relative->add(10)->getValue());
79+
$this->assertEquals(self::RATE+10, $this->relative->add(10)->getValue());
6880
}
6981

70-
public function badAddends()
82+
public static function badAddends(): iterable
7183
{
72-
$this->setUp();
84+
$relative = self::createRelativeDiscount();
85+
$absolute = self::createAbsoluteDiscount();
7386

7487
return [
75-
[$this->relative, 'aasd'],
76-
[$this->relative, '10a'],
77-
[$this->relative, Money::USD(12)],
78-
[$this->absolute, 'aasd'],
79-
[$this->absolute, '10b'],
80-
[$this->absolute, 10],
88+
[$relative, 'aasd'],
89+
[$relative, '10a'],
90+
[$relative, Money::USD(12)],
91+
[$absolute, 'aasd'],
92+
[$absolute, '10b'],
93+
[$absolute, 10],
8194
];
8295
}
8396

84-
/**
85-
* @dataProvider badAddends
86-
*/
87-
public function testAddFailed($discount, $addend)
97+
#[\PHPUnit\Framework\Attributes\DataProvider('badAddends')]
98+
public function testAddFailed($discount, $addend): void
8899
{
89100
$this->expectException(\Exception::class);
90101
$discount->add($addend);
91102
}
92103

93-
public function testCompare()
104+
public function testCompare(): void
94105
{
95106
$money = Money::USD(1);
96107
$this->assertTrue($this->absolute->compare($money) > 0);

tests/unit/formula/FormulaEngineTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function checkSimpleInstallment($date, $num, $reason)
7272
$this->assertNull($formula->getTill());
7373
}
7474

75-
public function normalizeDataProvider()
75+
public static function normalizeDataProvider()
7676
{
7777
return [
7878
["ab\ncd", "ab\ncd"],
@@ -86,23 +86,19 @@ public function normalizeDataProvider()
8686
];
8787
}
8888

89-
/**
90-
* @dataProvider normalizeDataProvider
91-
*/
89+
#[\PHPUnit\Framework\Attributes\DataProvider('normalizeDataProvider')]
9290
public function testNormalize($formula, $expected)
9391
{
9492
return $this->assertSame($expected, $this->engine->normalize($formula));
9593
}
9694

97-
/**
98-
* @dataProvider validateDataProvider
99-
*/
95+
#[\PHPUnit\Framework\Attributes\DataProvider('validateDataProvider')]
10096
public function testValidate($formula, $error)
10197
{
10298
return $this->assertSame($error, $this->engine->validate($formula));
10399
}
104100

105-
public function validateDataProvider()
101+
public static function validateDataProvider()
106102
{
107103
return [
108104
['', "Unexpected token \"EOF\" (EOF) at line 1 and column 1:\n\n↑ : "],

0 commit comments

Comments
 (0)