| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module\Options; |
| 6 |
|
| 7 |
use Packetery\Module\Options\OptionsProvider; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
|
| 10 |
class OptionsProviderTest extends TestCase { |
| 11 |
public static function dimensionsUnitProvider(): array { |
| 12 |
return [ |
| 13 |
[ |
| 14 |
'unit' => 'cm', |
| 15 |
'expectedDecimals' => 1, |
| 16 |
], |
| 17 |
[ |
| 18 |
'unit' => 'mm', |
| 19 |
'expectedDecimals' => 0, |
| 20 |
], |
| 21 |
[ |
| 22 |
'unit' => 'in', |
| 23 |
'expectedDecimals' => 0, |
| 24 |
], |
| 25 |
[ |
| 26 |
'unit' => 'l', |
| 27 |
'expectedDecimals' => 0, |
| 28 |
], |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @dataProvider dimensionsUnitProvider |
| 34 |
*/ |
| 35 |
public function testGetDimensionsNumberOfDecimals( string $unit, int $expectedDecimals ): void { |
| 36 |
$provider = $this->getMockBuilder( OptionsProvider::class ) |
| 37 |
->onlyMethods( [ 'getDimensionsUnit' ] ) |
| 38 |
->getMock(); |
| 39 |
|
| 40 |
$provider->method( 'getDimensionsUnit' ) |
| 41 |
->willReturn( $unit ); |
| 42 |
|
| 43 |
$result = $provider->getDimensionsNumberOfDecimals(); |
| 44 |
$this->assertSame( $expectedDecimals, $result ); |
| 45 |
} |
| 46 |
|
| 47 |
public static function sanitiseDimensionProvider(): array { |
| 48 |
return [ |
| 49 |
[ |
| 50 |
'dimensionValue' => '', |
| 51 |
'expectedValue' => null, |
| 52 |
'numberOfDecimals' => 1, |
| 53 |
'unit' => 'cm', |
| 54 |
], |
| 55 |
[ |
| 56 |
'dimensionValue' => 23.3567, |
| 57 |
'expectedValue' => 234, |
| 58 |
'numberOfDecimals' => 1, |
| 59 |
'unit' => 'cm', |
| 60 |
], |
| 61 |
[ |
| 62 |
'dimensionValue' => 10.0, |
| 63 |
'expectedValue' => 100, |
| 64 |
'numberOfDecimals' => 1, |
| 65 |
'unit' => 'cm', |
| 66 |
], |
| 67 |
[ |
| 68 |
'dimensionValue' => 0.100000000, |
| 69 |
'expectedValue' => 1, |
| 70 |
'numberOfDecimals' => 1, |
| 71 |
'unit' => 'cm', |
| 72 |
], |
| 73 |
[ |
| 74 |
'dimensionValue' => 200, |
| 75 |
'expectedValue' => 200, |
| 76 |
'numberOfDecimals' => 0, |
| 77 |
'unit' => 'mm', |
| 78 |
], |
| 79 |
[ |
| 80 |
'dimensionValue' => 200, |
| 81 |
'expectedValue' => 200, |
| 82 |
'numberOfDecimals' => 0, |
| 83 |
'unit' => 'mm', |
| 84 |
], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @dataProvider sanitiseDimensionProvider |
| 90 |
*/ |
| 91 |
public function testGetSanitizedDimensionValueInMm( |
| 92 |
float|int|string $dimensionValue, |
| 93 |
?int $expectedValue, |
| 94 |
int $numberOfDecimals, |
| 95 |
string $unit |
| 96 |
): void { |
| 97 |
$provider = $this->getMockBuilder( OptionsProvider::class ) |
| 98 |
->onlyMethods( [ 'getDimensionsNumberOfDecimals', 'getDimensionsUnit' ] ) |
| 99 |
->getMock(); |
| 100 |
|
| 101 |
$provider->method( 'getDimensionsNumberOfDecimals' ) |
| 102 |
->willReturn( $numberOfDecimals ); |
| 103 |
|
| 104 |
$provider->method( 'getDimensionsUnit' ) |
| 105 |
->willReturn( $unit ); |
| 106 |
|
| 107 |
$result = $provider->getSanitizedDimensionValueInMm( $dimensionValue ); |
| 108 |
$this->assertEquals( $expectedValue, $result ); |
| 109 |
} |
| 110 |
} |
| 111 |
|