| 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 |
private const DUMMY_NONSENSE_VALUE = 'nonsense'; |
| 15 |
|
| 16 |
public static function dimensionsUnitProvider(): array { |
| 17 |
return [ |
| 18 |
[ |
| 19 |
'unit' => 'cm', |
| 20 |
'expectedDecimals' => 1, |
| 21 |
], |
| 22 |
[ |
| 23 |
'unit' => 'mm', |
| 24 |
'expectedDecimals' => 0, |
| 25 |
], |
| 26 |
[ |
| 27 |
'unit' => 'in', |
| 28 |
'expectedDecimals' => 0, |
| 29 |
], |
| 30 |
[ |
| 31 |
'unit' => 'l', |
| 32 |
'expectedDecimals' => 0, |
| 33 |
], |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @dataProvider dimensionsUnitProvider |
| 39 |
*/ |
| 40 |
public function testGetDimensionsNumberOfDecimals( string $unit, int $expectedDecimals ): void { |
| 41 |
$provider = $this->getMockBuilder( OptionsProvider::class ) |
| 42 |
->disableOriginalConstructor() |
| 43 |
->onlyMethods( [ 'getDimensionsUnit' ] ) |
| 44 |
->getMock(); |
| 45 |
|
| 46 |
$provider->method( 'getDimensionsUnit' ) |
| 47 |
->willReturn( $unit ); |
| 48 |
|
| 49 |
$result = $provider->getDimensionsNumberOfDecimals(); |
| 50 |
$this->assertSame( $expectedDecimals, $result ); |
| 51 |
} |
| 52 |
|
| 53 |
public static function sanitiseDimensionProvider(): array { |
| 54 |
return [ |
| 55 |
[ |
| 56 |
'dimensionValue' => '', |
| 57 |
'expectedValue' => null, |
| 58 |
'numberOfDecimals' => 1, |
| 59 |
'unit' => 'cm', |
| 60 |
], |
| 61 |
[ |
| 62 |
'dimensionValue' => 23.3567, |
| 63 |
'expectedValue' => 234, |
| 64 |
'numberOfDecimals' => 1, |
| 65 |
'unit' => 'cm', |
| 66 |
], |
| 67 |
[ |
| 68 |
'dimensionValue' => 10.0, |
| 69 |
'expectedValue' => 100, |
| 70 |
'numberOfDecimals' => 1, |
| 71 |
'unit' => 'cm', |
| 72 |
], |
| 73 |
[ |
| 74 |
'dimensionValue' => 0.100000000, |
| 75 |
'expectedValue' => 1, |
| 76 |
'numberOfDecimals' => 1, |
| 77 |
'unit' => 'cm', |
| 78 |
], |
| 79 |
[ |
| 80 |
'dimensionValue' => 200, |
| 81 |
'expectedValue' => 200, |
| 82 |
'numberOfDecimals' => 0, |
| 83 |
'unit' => 'mm', |
| 84 |
], |
| 85 |
[ |
| 86 |
'dimensionValue' => 200, |
| 87 |
'expectedValue' => 200, |
| 88 |
'numberOfDecimals' => 0, |
| 89 |
'unit' => 'mm', |
| 90 |
], |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @dataProvider sanitiseDimensionProvider |
| 96 |
*/ |
| 97 |
public function testGetSanitizedDimensionValueInMm( |
| 98 |
float|int|string $dimensionValue, |
| 99 |
?int $expectedValue, |
| 100 |
int $numberOfDecimals, |
| 101 |
string $unit |
| 102 |
): void { |
| 103 |
$provider = $this->getMockBuilder( OptionsProvider::class ) |
| 104 |
->disableOriginalConstructor() |
| 105 |
->onlyMethods( [ 'getDimensionsNumberOfDecimals', 'getDimensionsUnit' ] ) |
| 106 |
->getMock(); |
| 107 |
|
| 108 |
$provider->method( 'getDimensionsNumberOfDecimals' ) |
| 109 |
->willReturn( $numberOfDecimals ); |
| 110 |
|
| 111 |
$provider->method( 'getDimensionsUnit' ) |
| 112 |
->willReturn( $unit ); |
| 113 |
|
| 114 |
$result = $provider->getSanitizedDimensionValueInMm( $dimensionValue ); |
| 115 |
$this->assertEquals( $expectedValue, $result ); |
| 116 |
} |
| 117 |
|
| 118 |
public function testStatusSyncingPacketStatuses(): void { |
| 119 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 120 |
$wpAdapterMock |
| 121 |
->method( 'getOption' ) |
| 122 |
->willReturnCallback( |
| 123 |
static function ( string $key ): array { |
| 124 |
if ( $key === OptionNames::PACKETERY_SYNC ) { |
| 125 |
return [ 'status_syncing_packet_statuses' => [ PacketStatus::DEPARTED, PacketStatus::UNKNOWN ] ]; |
| 126 |
} |
| 127 |
|
| 128 |
return []; |
| 129 |
} |
| 130 |
); |
| 131 |
|
| 132 |
$provider = new OptionsProvider( |
| 133 |
$wpAdapterMock |
| 134 |
); |
| 135 |
|
| 136 |
$result = $provider->getStatusSyncingPacketStatuses( |
| 137 |
[ |
| 138 |
new PacketStatus( PacketStatus::DEPARTED, PacketStatus::DEPARTED, true ), |
| 139 |
new PacketStatus( PacketStatus::ARRIVED, PacketStatus::ARRIVED, true ), |
| 140 |
] |
| 141 |
); |
| 142 |
$this->assertSame( [ PacketStatus::DEPARTED ], $result ); |
| 143 |
} |
| 144 |
|
| 145 |
public static function wcCarrierConfigEnabledNullableProvider(): array { |
| 146 |
return [ |
| 147 |
[ |
| 148 |
'inputValue' => true, |
| 149 |
'expectedValue' => true, |
| 150 |
], |
| 151 |
[ |
| 152 |
'inputValue' => false, |
| 153 |
'expectedValue' => false, |
| 154 |
], |
| 155 |
[ |
| 156 |
'inputValue' => null, |
| 157 |
'expectedValue' => null, |
| 158 |
], |
| 159 |
[ |
| 160 |
'inputValue' => 1, |
| 161 |
'expectedValue' => true, |
| 162 |
], |
| 163 |
[ |
| 164 |
'inputValue' => 0, |
| 165 |
'expectedValue' => false, |
| 166 |
], |
| 167 |
[ |
| 168 |
'inputValue' => '1', |
| 169 |
'expectedValue' => true, |
| 170 |
], |
| 171 |
[ |
| 172 |
'inputValue' => '0', |
| 173 |
'expectedValue' => false, |
| 174 |
], |
| 175 |
[ |
| 176 |
'inputValue' => '', |
| 177 |
'expectedValue' => false, |
| 178 |
], |
| 179 |
[ |
| 180 |
'inputValue' => self::DUMMY_NONSENSE_VALUE, |
| 181 |
'expectedValue' => true, |
| 182 |
], |
| 183 |
]; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @dataProvider wcCarrierConfigEnabledNullableProvider |
| 188 |
*/ |
| 189 |
public function testIsWcCarrierConfigEnabledNullable( mixed $inputValue, ?bool $expectedValue ): void { |
| 190 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 191 |
$wpAdapterMock->method( 'getOption' ) |
| 192 |
->willReturn( [ 'new_carrier_settings_enabled' => $inputValue ] ); |
| 193 |
|
| 194 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 195 |
|
| 196 |
$this->assertSame( $expectedValue, $provider->isWcCarrierConfigEnabledNullable() ); |
| 197 |
} |
| 198 |
|
| 199 |
public static function autoEmailInfoInsertionEnabledProvider(): array { |
| 200 |
return [ |
| 201 |
[ |
| 202 |
'inputValue' => true, |
| 203 |
'expectedValue' => true, |
| 204 |
], |
| 205 |
[ |
| 206 |
'inputValue' => false, |
| 207 |
'expectedValue' => false, |
| 208 |
], |
| 209 |
[ |
| 210 |
'inputValue' => null, |
| 211 |
'expectedValue' => true, |
| 212 |
], |
| 213 |
[ |
| 214 |
'inputValue' => 1, |
| 215 |
'expectedValue' => true, |
| 216 |
], |
| 217 |
[ |
| 218 |
'inputValue' => 0, |
| 219 |
'expectedValue' => false, |
| 220 |
], |
| 221 |
[ |
| 222 |
'inputValue' => '1', |
| 223 |
'expectedValue' => true, |
| 224 |
], |
| 225 |
[ |
| 226 |
'inputValue' => '0', |
| 227 |
'expectedValue' => false, |
| 228 |
], |
| 229 |
[ |
| 230 |
'inputValue' => '', |
| 231 |
'expectedValue' => false, |
| 232 |
], |
| 233 |
[ |
| 234 |
'inputValue' => self::DUMMY_NONSENSE_VALUE, |
| 235 |
'expectedValue' => true, |
| 236 |
], |
| 237 |
]; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* @dataProvider autoEmailInfoInsertionEnabledProvider |
| 242 |
*/ |
| 243 |
public function testIsAutoEmailInfoInsertionEnabled( mixed $inputValue, bool $expectedValue ): void { |
| 244 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 245 |
$wpAdapterMock->method( 'getOption' ) |
| 246 |
->willReturn( [ 'auto_email_info_insertion' => $inputValue ] ); |
| 247 |
|
| 248 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 249 |
|
| 250 |
$this->assertSame( $expectedValue, $provider->isAutoEmailInfoInsertionEnabled() ); |
| 251 |
} |
| 252 |
|
| 253 |
public function testIsAutoEmailInfoInsertionEnabledReturnsDefaultWhenOptionNotSet(): void { |
| 254 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 255 |
$wpAdapterMock->method( 'getOption' ) |
| 256 |
->willReturn( [] ); |
| 257 |
|
| 258 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 259 |
|
| 260 |
$this->assertSame( true, $provider->isAutoEmailInfoInsertionEnabled() ); |
| 261 |
} |
| 262 |
|
| 263 |
public function testGetPacketAutoSubmissionMappedUniqueEvents(): void { |
| 264 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 265 |
$wpAdapterMock |
| 266 |
->method( 'getOption' ) |
| 267 |
->willReturnCallback( |
| 268 |
static function ( string $key ): array { |
| 269 |
if ( $key === OptionNames::PACKETERY_AUTO_SUBMISSION ) { |
| 270 |
return [ |
| 271 |
'payment_method_events' => [ |
| 272 |
[ |
| 273 |
'event' => 'order_paid', |
| 274 |
], |
| 275 |
[ |
| 276 |
'event' => 'order_shipped', |
| 277 |
], |
| 278 |
[ |
| 279 |
'event' => null, |
| 280 |
], |
| 281 |
], |
| 282 |
]; |
| 283 |
} |
| 284 |
|
| 285 |
return []; |
| 286 |
} |
| 287 |
); |
| 288 |
|
| 289 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 290 |
|
| 291 |
$result = $provider->getPacketAutoSubmissionMappedUniqueEvents(); |
| 292 |
$this->assertSame( [ 'order_paid', 'order_shipped' ], array_values( $result ) ); |
| 293 |
} |
| 294 |
|
| 295 |
public function testShowConsignPasswordForZBoxDefaultsToDisabled(): void { |
| 296 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 297 |
$wpAdapterMock->method( 'getOption' )->willReturn( [] ); |
| 298 |
|
| 299 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 300 |
|
| 301 |
$this->assertFalse( $provider->isShowConsignPasswordForZBoxEnabled() ); |
| 302 |
} |
| 303 |
|
| 304 |
public function testShowConsignPasswordForZBoxReadsStoredValue(): void { |
| 305 |
$wpAdapterMock = $this->createMock( WpAdapter::class ); |
| 306 |
$wpAdapterMock->method( 'getOption' ) |
| 307 |
->willReturnCallback( |
| 308 |
static function ( string $key ) { |
| 309 |
if ( $key === OptionNames::PACKETERY ) { |
| 310 |
return [ 'show_consign_password_for_z_box' => true ]; |
| 311 |
} |
| 312 |
|
| 313 |
return []; |
| 314 |
} |
| 315 |
); |
| 316 |
|
| 317 |
$provider = new OptionsProvider( $wpAdapterMock ); |
| 318 |
|
| 319 |
$this->assertTrue( $provider->isShowConsignPasswordForZBoxEnabled() ); |
| 320 |
} |
| 321 |
} |
| 322 |
|