| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Core; |
| 6 |
|
| 7 |
use DateTimeImmutable; |
| 8 |
use Packetery\Core\CoreHelper; |
| 9 |
use PHPUnit\Framework\TestCase; |
| 10 |
|
| 11 |
class CoreHelperTest extends TestCase { |
| 12 |
|
| 13 |
private CoreHelper $coreHelper; |
| 14 |
|
| 15 |
public function __construct( string $name ) { |
| 16 |
parent::__construct( $name ); |
| 17 |
$this->coreHelper = new CoreHelper( 'dummyTrackingUrl' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function testGetTrackingUrl(): void { |
| 21 |
self::assertIsString( $this->coreHelper->getTrackingUrl( 'dummyPacketId' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
public function testGetTrackingUrlNull(): void { |
| 25 |
self::assertNull( $this->coreHelper->getTrackingUrl( null ) ); |
| 26 |
} |
| 27 |
|
| 28 |
public function testGetStringFromDateTime(): void { |
| 29 |
$dummyDateString = '2023-11-17'; |
| 30 |
$dummyDate = $this->coreHelper->getDateTimeFromString( $dummyDateString ); |
| 31 |
|
| 32 |
self::assertSame( |
| 33 |
$this->coreHelper->getStringFromDateTime( $dummyDate, CoreHelper::MYSQL_DATE_FORMAT ), |
| 34 |
$dummyDateString |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
public function testNow(): void { |
| 39 |
self::assertInstanceOf( DateTimeImmutable::class, CoreHelper::now() ); |
| 40 |
} |
| 41 |
|
| 42 |
public static function simplifyWeightProvider(): array { |
| 43 |
return [ |
| 44 |
'float-with-3-decimals' => [ |
| 45 |
'expected' => 10.222, |
| 46 |
'weight' => 10.2222, |
| 47 |
], |
| 48 |
'full-number-without-decimals' => [ |
| 49 |
'expected' => 1, |
| 50 |
'weight' => 1.000000, |
| 51 |
], |
| 52 |
'null' => [ |
| 53 |
'expected' => null, |
| 54 |
'weight' => null, |
| 55 |
], |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @dataProvider simplifyWeightProvider |
| 61 |
*/ |
| 62 |
public function testSimplifyWeight( ?float $expected, ?float $weight ): void { |
| 63 |
self::assertSame( $expected, CoreHelper::simplifyWeight( $weight ) ); |
| 64 |
} |
| 65 |
|
| 66 |
public static function trimDecimalPlacesProvider(): array { |
| 67 |
return [ |
| 68 |
'float-with-3-decimals' => [ |
| 69 |
'expected' => '4.123', |
| 70 |
'value' => 4.1234566778, |
| 71 |
'decimals' => 3, |
| 72 |
], |
| 73 |
'single-digit-full-number-without-decimals' => [ |
| 74 |
'expected' => '4', |
| 75 |
'value' => 4.1234566778, |
| 76 |
'decimals' => 0, |
| 77 |
], |
| 78 |
'full-number-without-3-decimals' => [ |
| 79 |
'expected' => '20', |
| 80 |
'value' => 20.0, |
| 81 |
'decimals' => 3, |
| 82 |
], |
| 83 |
'full-number-without-decimals' => [ |
| 84 |
'expected' => '10', |
| 85 |
'value' => 10.0, |
| 86 |
'decimals' => 0, |
| 87 |
], |
| 88 |
'negative-float-with-3-decimals' => [ |
| 89 |
'expected' => '-4.123', |
| 90 |
'value' => - 4.1234566778, |
| 91 |
'decimals' => 3, |
| 92 |
], |
| 93 |
'negative-single-digit-full-number-without-decimals' => [ |
| 94 |
'expected' => '-4', |
| 95 |
'value' => - 4.1234566778, |
| 96 |
'decimals' => 0, |
| 97 |
], |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @dataProvider trimDecimalPlacesProvider |
| 103 |
*/ |
| 104 |
public function testTrimDecimalPlaces( string $expected, float $value, int $decimals ): void { |
| 105 |
self::assertSame( $expected, CoreHelper::trimDecimalPlaces( $value, $decimals ) ); |
| 106 |
} |
| 107 |
|
| 108 |
public static function convertToCentimetersProvider(): array { |
| 109 |
return [ |
| 110 |
[ |
| 111 |
'input' => 0, |
| 112 |
'inputUnit' => 'mm', |
| 113 |
'expected' => 0, |
| 114 |
], |
| 115 |
[ |
| 116 |
'input' => 1, |
| 117 |
'inputUnit' => 'mm', |
| 118 |
'expected' => 0.1, |
| 119 |
], |
| 120 |
[ |
| 121 |
'input' => 10, |
| 122 |
'inputUnit' => 'mm', |
| 123 |
'expected' => 1.0, |
| 124 |
], |
| 125 |
[ |
| 126 |
'input' => - 1, |
| 127 |
'inputUnit' => 'mm', |
| 128 |
'expected' => - 0.1, |
| 129 |
], |
| 130 |
|
| 131 |
[ |
| 132 |
'input' => 0, |
| 133 |
'inputUnit' => 'cm', |
| 134 |
'expected' => 0, |
| 135 |
], |
| 136 |
[ |
| 137 |
'input' => 1, |
| 138 |
'inputUnit' => 'cm', |
| 139 |
'expected' => 1, |
| 140 |
], |
| 141 |
[ |
| 142 |
'input' => 10, |
| 143 |
'inputUnit' => 'cm', |
| 144 |
'expected' => 10, |
| 145 |
], |
| 146 |
[ |
| 147 |
'input' => - 1, |
| 148 |
'inputUnit' => 'cm', |
| 149 |
'expected' => - 1, |
| 150 |
], |
| 151 |
|
| 152 |
[ |
| 153 |
'input' => 0, |
| 154 |
'inputUnit' => 'm', |
| 155 |
'expected' => 0, |
| 156 |
], |
| 157 |
[ |
| 158 |
'input' => 1, |
| 159 |
'inputUnit' => 'm', |
| 160 |
'expected' => 100, |
| 161 |
], |
| 162 |
[ |
| 163 |
'input' => 10, |
| 164 |
'inputUnit' => 'm', |
| 165 |
'expected' => 1000, |
| 166 |
], |
| 167 |
[ |
| 168 |
'input' => - 1, |
| 169 |
'inputUnit' => 'm', |
| 170 |
'expected' => - 100, |
| 171 |
], |
| 172 |
|
| 173 |
[ |
| 174 |
'input' => 0, |
| 175 |
'inputUnit' => 'in', |
| 176 |
'expected' => 0, |
| 177 |
], |
| 178 |
[ |
| 179 |
'input' => 1, |
| 180 |
'inputUnit' => 'in', |
| 181 |
'expected' => 2.54, |
| 182 |
], |
| 183 |
[ |
| 184 |
'input' => 10, |
| 185 |
'inputUnit' => 'in', |
| 186 |
'expected' => 25.4, |
| 187 |
], |
| 188 |
[ |
| 189 |
'input' => - 1, |
| 190 |
'inputUnit' => 'in', |
| 191 |
'expected' => - 2.54, |
| 192 |
], |
| 193 |
|
| 194 |
[ |
| 195 |
'input' => 0, |
| 196 |
'inputUnit' => 'yd', |
| 197 |
'expected' => 0, |
| 198 |
], |
| 199 |
[ |
| 200 |
'input' => 1, |
| 201 |
'inputUnit' => 'yd', |
| 202 |
'expected' => 91.44, |
| 203 |
], |
| 204 |
[ |
| 205 |
'input' => 10, |
| 206 |
'inputUnit' => 'yd', |
| 207 |
'expected' => 914.4, |
| 208 |
], |
| 209 |
[ |
| 210 |
'input' => - 1, |
| 211 |
'inputUnit' => 'yd', |
| 212 |
'expected' => - 91.44, |
| 213 |
], |
| 214 |
]; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @dataProvider convertToCentimetersProvider |
| 219 |
*/ |
| 220 |
public function testConvertToCentimeters( int $input, string $inputUnit, ?float $expected ): void { |
| 221 |
$result = CoreHelper::convertToCentimeters( $input, $inputUnit ); |
| 222 |
$this->assertSame( $expected, $result ); |
| 223 |
} |
| 224 |
} |
| 225 |
|