| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module\Options; |
| 6 |
|
| 7 |
use Packetery\Core\Entity\PacketStatus; |
| 8 |
use Packetery\Module\Framework\WpAdapter; |
| 9 |
use Packetery\Module\Options\OptionNames; |
| 10 |
use Packetery\Module\Options\OptionsProvider; |
| 11 |
use PHPUnit\Framework\TestCase; |
| 12 |
|
| 13 |
class OptionsProviderTest extends TestCase { |
| 14 |
public static function dimensionsUnitProvider(): array { |
| 15 |
return [ |
| 16 |
[ |
| 17 |
'unit' => 'cm', |
| 18 |
'expectedDecimals' => 1, |
| 19 |
], |
| 20 |
[ |
| 21 |
'unit' => 'mm', |
| 22 |
'expectedDecimals' => 0, |
| 23 |
], |
| 24 |
[ |
| 25 |
'unit' => 'in', |
| 26 |
'expectedDecimals' => 0, |
| 27 |
], |
| 28 |
[ |
| 29 |
'unit' => 'l', |
| 30 |
'expectedDecimals' => 0, |
| 31 |
], |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @dataProvider dimensionsUnitProvider |
| 37 |
*/ |
| 38 |
public function testGetDimensionsNumberOfDecimals( string $unit, int $expectedDecimals ): void { |
| 39 |
$provider = $this->getMockBuilder( OptionsProvider::class ) |
| 40 |
->disableOriginalConstructor() |
| 41 |
->onlyMethods( [ 'getDimensionsUnit' ] ) |
| 42 |
->getMock(); |
| 43 |
|
| 44 |
$provider->method( 'getDimensionsUnit' ) |
| 45 |
->willReturn( $unit ); |
| 46 |
|
| 47 |
$result = $provider->getDimensionsNumberOfDecimals(); |
| 48 |
$this->assertSame( $expectedDecimals, $result ); |
| 49 |
} |
| 50 |
|
| 51 |
public static function sanitiseDimensionProvider(): array { |
| 52 |
return [ |
| 53 |
[ |
| 54 |
'dimensionValue' => '', |
| 55 |
'expectedValue' => null, |
| 56 |
'numberOfDecimals' => 1, |
| 57 |
'unit' => 'cm', |
| 58 |
], |
| 59 |
[ |
| 60 |
'dimensionValue' => 23.3567, |
| 61 |
'expectedValue' => 234, |
| 62 |
'numberOfDecimals' => 1, |
| 63 |
'unit' => 'cm', |
| 64 |
], |
| 65 |
[ |
| 66 |
'dimensionValue' => 10.0, |
| 67 |
'expectedValue' => 100, |
| 68 |
'numberOfDecimals' => 1, |
| 69 |
'unit' => 'cm', |
| 70 |
], |
| 71 |
[ |
| 72 |
'dimensionValue' => 0.100000000, |
| 73 |
'expectedValue' => 1, |
| 74 |
'numberOfDecimals' => 1, |
| 75 |
'unit' => 'cm', |
| 76 |
], |
| 77 |
[ |
| 78 |
'dimensionValue' => 200, |
| 79 |
'expectedValue' => 200, |
| 80 |
'numberOfDecimals' => 0, |
| 81 |
'unit' => 'mm', |
| 82 |
], |
| 83 |
[ |
| 84 |
'dimensionValue' => 200, |
| 85 |
'expectedValue' => 200, |
| 86 |
'numberOfDecimals' => 0, |
| 87 |
'unit' => 'mm', |
| 88 |
], |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @dataProvider sanitiseDimensionProvider |
| 94 |
*/ |
| 95 |
public function testGetSanitizedDimensionValueInMm( |
| 96 |
float|int|string $dimensionValue, |
| 97 |
?int $expectedValue, |
| 98 |
int $numberOfDecimals, |
| 99 |
string $unit |
| 100 |
): void { |
| 101 |
$provider = $this->getMockBuilder( OptionsProvider::class ) |
| 102 |
->disableOriginalConstructor() |
| 103 |
->onlyMethods( [ 'getDimensionsNumberOfDecimals', 'getDimensionsUnit' ] ) |
| 104 |
->getMock(); |
| 105 |
|
| 106 |
$provider->method( 'getDimensionsNumberOfDecimals' ) |
| 107 |
->willReturn( $numberOfDecimals ); |
| 108 |
|
| 109 |
$provider->method( 'getDimensionsUnit' ) |
| 110 |
->willReturn( $unit ); |
| 111 |
|
| 112 |
$result = $provider->getSanitizedDimensionValueInMm( $dimensionValue ); |
| 113 |
$this->assertEquals( $expectedValue, $result ); |
| 114 |
} |
| 115 |
|
| 116 |
public function testStatusSyncingPacketStatuses(): void { |
| 117 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 118 |
$wpAdapterMock |
| 119 |
->method( 'getOption' ) |
| 120 |
->willReturnCallback( |
| 121 |
static function ( string $key ): array { |
| 122 |
if ( $key === OptionNames::PACKETERY_SYNC ) { |
| 123 |
return [ 'status_syncing_packet_statuses' => [ PacketStatus::DEPARTED, PacketStatus::UNKNOWN ] ]; |
| 124 |
} |
| 125 |
|
| 126 |
return []; |
| 127 |
} |
| 128 |
); |
| 129 |
|
| 130 |
$provider = new OptionsProvider( |
| 131 |
$wpAdapterMock |
| 132 |
); |
| 133 |
|
| 134 |
$result = $provider->getStatusSyncingPacketStatuses( |
| 135 |
[ |
| 136 |
new PacketStatus( PacketStatus::DEPARTED, PacketStatus::DEPARTED, true ), |
| 137 |
new PacketStatus( PacketStatus::ARRIVED, PacketStatus::ARRIVED, true ), |
| 138 |
] |
| 139 |
); |
| 140 |
$this->assertSame( [ PacketStatus::DEPARTED ], $result ); |
| 141 |
} |
| 142 |
|
| 143 |
public static function wcCarrierConfigEnabledNullableProvider(): array { |
| 144 |
return [ |
| 145 |
[ |
| 146 |
'inputValue' => true, |
| 147 |
'expectedValue' => true, |
| 148 |
], |
| 149 |
[ |
| 150 |
'inputValue' => false, |
| 151 |
'expectedValue' => false, |
| 152 |
], |
| 153 |
[ |
| 154 |
'inputValue' => null, |
| 155 |
'expectedValue' => null, |
| 156 |
], |
| 157 |
[ |
| 158 |
'inputValue' => 1, |
| 159 |
'expectedValue' => true, |
| 160 |
], |
| 161 |
[ |
| 162 |
'inputValue' => 0, |
| 163 |
'expectedValue' => false, |
| 164 |
], |
| 165 |
[ |
| 166 |
'inputValue' => '1', |
| 167 |
'expectedValue' => true, |
| 168 |
], |
| 169 |
[ |
| 170 |
'inputValue' => '0', |
| 171 |
'expectedValue' => false, |
| 172 |
], |
| 173 |
[ |
| 174 |
'inputValue' => '', |
| 175 |
'expectedValue' => false, |
| 176 |
], |
| 177 |
[ |
| 178 |
'inputValue' => 'nonsense', |
| 179 |
'expectedValue' => true, |
| 180 |
], |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @dataProvider wcCarrierConfigEnabledNullableProvider |
| 186 |
*/ |
| 187 |
public function testIsWcCarrierConfigEnabledNullable( mixed $inputValue, ?bool $expectedValue ): void { |
| 188 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 189 |
$wpAdapterMock->method( 'getOption' ) |
| 190 |
->willReturn( [ 'new_carrier_settings_enabled' => $inputValue ] ); |
| 191 |
|
| 192 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 193 |
|
| 194 |
$this->assertSame( $expectedValue, $provider->isWcCarrierConfigEnabledNullable() ); |
| 195 |
} |
| 196 |
|
| 197 |
public function testGetPacketAutoSubmissionMappedUniqueEvents(): void { |
| 198 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 199 |
$wpAdapterMock |
| 200 |
->method( 'getOption' ) |
| 201 |
->willReturnCallback( |
| 202 |
static function ( string $key ): array { |
| 203 |
if ( $key === OptionNames::PACKETERY_AUTO_SUBMISSION ) { |
| 204 |
return [ |
| 205 |
'payment_method_events' => [ |
| 206 |
[ |
| 207 |
'event' => 'order_paid', |
| 208 |
], |
| 209 |
[ |
| 210 |
'event' => 'order_shipped', |
| 211 |
], |
| 212 |
[ |
| 213 |
'event' => null, |
| 214 |
], |
| 215 |
], |
| 216 |
]; |
| 217 |
} |
| 218 |
|
| 219 |
return []; |
| 220 |
} |
| 221 |
); |
| 222 |
|
| 223 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 224 |
|
| 225 |
$result = $provider->getPacketAutoSubmissionMappedUniqueEvents(); |
| 226 |
$this->assertSame( [ 'order_paid', 'order_shipped' ], array_values( $result ) ); |
| 227 |
} |
| 228 |
} |
| 229 |
|