| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module\Order; |
| 6 |
|
| 7 |
use Packetery\Core\Api\Soap\Client; |
| 8 |
use Packetery\Core\Api\Soap\Response\PacketInfo; |
| 9 |
use Packetery\Core\Entity\Order; |
| 10 |
use Packetery\Core\Log\ILogger; |
| 11 |
use Packetery\Module\Framework\WcAdapter; |
| 12 |
use Packetery\Module\Options\OptionsProvider; |
| 13 |
use Packetery\Module\Order\ConsignPasswordSynchronizer; |
| 14 |
use Packetery\Module\Order\Repository; |
| 15 |
use PHPUnit\Framework\MockObject\MockObject; |
| 16 |
use PHPUnit\Framework\TestCase; |
| 17 |
|
| 18 |
class ConsignPasswordSynchronizerTest extends TestCase { |
| 19 |
|
| 20 |
private ConsignPasswordSynchronizer $synchronizer; |
| 21 |
private Client|MockObject $client; |
| 22 |
private ILogger|MockObject $logger; |
| 23 |
private Repository|MockObject $orderRepository; |
| 24 |
private OptionsProvider|MockObject $optionsProvider; |
| 25 |
private WcAdapter|MockObject $wcAdapter; |
| 26 |
|
| 27 |
private function createSynchronizer(): void { |
| 28 |
$this->client = $this->createMock( Client::class ); |
| 29 |
$this->logger = $this->createMock( ILogger::class ); |
| 30 |
$this->orderRepository = $this->createMock( Repository::class ); |
| 31 |
$this->optionsProvider = $this->createMock( OptionsProvider::class ); |
| 32 |
$this->wcAdapter = $this->createMock( WcAdapter::class ); |
| 33 |
|
| 34 |
$this->synchronizer = new ConsignPasswordSynchronizer( |
| 35 |
$this->client, |
| 36 |
$this->logger, |
| 37 |
$this->orderRepository, |
| 38 |
$this->optionsProvider, |
| 39 |
$this->wcAdapter |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
public function testProcessByIdStoresFetchedPassword(): void { |
| 44 |
$this->createSynchronizer(); |
| 45 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( true ); |
| 46 |
|
| 47 |
$order = $this->createMock( Order::class ); |
| 48 |
$order->method( 'getPacketId' )->willReturn( 'packet-id' ); |
| 49 |
$order->expects( $this->once() )->method( 'setConsignPassword' )->with( '123456789' ); |
| 50 |
$this->orderRepository->method( 'getByIdWithValidCarrier' )->willReturn( $order ); |
| 51 |
$this->orderRepository->expects( $this->once() )->method( 'save' )->with( $order ); |
| 52 |
|
| 53 |
$response = $this->createMock( PacketInfo::class ); |
| 54 |
$response->method( 'hasFault' )->willReturn( false ); |
| 55 |
$response->method( 'getConsignPassword' )->willReturn( '123456789' ); |
| 56 |
$this->client->method( 'packetInfo' )->willReturn( $response ); |
| 57 |
|
| 58 |
$this->synchronizer->processById( '1' ); |
| 59 |
} |
| 60 |
|
| 61 |
public function testProcessByIdLogsFaultAndDoesNotSave(): void { |
| 62 |
$this->createSynchronizer(); |
| 63 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( true ); |
| 64 |
|
| 65 |
$order = $this->createMock( Order::class ); |
| 66 |
$order->method( 'getPacketId' )->willReturn( 'packet-id' ); |
| 67 |
$order->expects( $this->never() )->method( 'setConsignPassword' ); |
| 68 |
$this->orderRepository->method( 'getByIdWithValidCarrier' )->willReturn( $order ); |
| 69 |
$this->orderRepository->expects( $this->never() )->method( 'save' ); |
| 70 |
|
| 71 |
$response = $this->createMock( PacketInfo::class ); |
| 72 |
$response->method( 'hasFault' )->willReturn( true ); |
| 73 |
$response->method( 'getFaultString' )->willReturn( 'Error message' ); |
| 74 |
$response->expects( $this->never() )->method( 'getConsignPassword' ); |
| 75 |
$this->client->method( 'packetInfo' )->willReturn( $response ); |
| 76 |
|
| 77 |
$this->logger->expects( $this->once() )->method( 'add' ); |
| 78 |
|
| 79 |
$this->synchronizer->processById( '1' ); |
| 80 |
} |
| 81 |
|
| 82 |
public function testProcessByIdDoesNotSaveEmptyPassword(): void { |
| 83 |
$this->createSynchronizer(); |
| 84 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( true ); |
| 85 |
|
| 86 |
$order = $this->createMock( Order::class ); |
| 87 |
$order->method( 'getPacketId' )->willReturn( 'packet-id' ); |
| 88 |
$order->expects( $this->never() )->method( 'setConsignPassword' ); |
| 89 |
$this->orderRepository->method( 'getByIdWithValidCarrier' )->willReturn( $order ); |
| 90 |
$this->orderRepository->expects( $this->never() )->method( 'save' ); |
| 91 |
|
| 92 |
$response = $this->createMock( PacketInfo::class ); |
| 93 |
$response->method( 'hasFault' )->willReturn( false ); |
| 94 |
$response->method( 'getConsignPassword' )->willReturn( null ); |
| 95 |
$this->client->method( 'packetInfo' )->willReturn( $response ); |
| 96 |
|
| 97 |
$this->synchronizer->processById( '1' ); |
| 98 |
} |
| 99 |
|
| 100 |
public function testProcessByIdSkippedWhenFeatureDisabled(): void { |
| 101 |
$this->createSynchronizer(); |
| 102 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( false ); |
| 103 |
|
| 104 |
$this->orderRepository->expects( $this->never() )->method( 'getByIdWithValidCarrier' ); |
| 105 |
$this->client->expects( $this->never() )->method( 'packetInfo' ); |
| 106 |
|
| 107 |
$this->synchronizer->processById( '1' ); |
| 108 |
} |
| 109 |
|
| 110 |
public function testScheduleSkippedWhenFeatureDisabled(): void { |
| 111 |
$this->createSynchronizer(); |
| 112 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( false ); |
| 113 |
|
| 114 |
$order = $this->createMock( Order::class ); |
| 115 |
$order->expects( $this->never() )->method( 'getPacketId' ); |
| 116 |
$this->client->expects( $this->never() )->method( 'packetInfo' ); |
| 117 |
$this->wcAdapter->expects( $this->never() )->method( 'asEnqueueAsyncAction' ); |
| 118 |
|
| 119 |
$this->synchronizer->schedule( $order, true ); |
| 120 |
} |
| 121 |
|
| 122 |
public function testScheduleImmediateDelegatesToProcessing(): void { |
| 123 |
$this->createSynchronizer(); |
| 124 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( true ); |
| 125 |
|
| 126 |
$order = $this->createMock( Order::class ); |
| 127 |
$order->method( 'getPacketId' )->willReturn( 'packet-id' ); |
| 128 |
$order->method( 'getNumber' )->willReturn( '1' ); |
| 129 |
$order->expects( $this->once() )->method( 'setConsignPassword' )->with( '123456789' ); |
| 130 |
$this->orderRepository->method( 'getByIdWithValidCarrier' )->willReturn( $order ); |
| 131 |
$this->orderRepository->expects( $this->once() )->method( 'save' )->with( $order ); |
| 132 |
|
| 133 |
$response = $this->createMock( PacketInfo::class ); |
| 134 |
$response->method( 'hasFault' )->willReturn( false ); |
| 135 |
$response->method( 'getConsignPassword' )->willReturn( '123456789' ); |
| 136 |
$this->client->expects( $this->once() )->method( 'packetInfo' )->willReturn( $response ); |
| 137 |
$this->wcAdapter->expects( $this->never() )->method( 'asEnqueueAsyncAction' ); |
| 138 |
|
| 139 |
$this->synchronizer->schedule( $order, true ); |
| 140 |
} |
| 141 |
|
| 142 |
public function testScheduleEnqueuesAsyncActionWhenNotImmediate(): void { |
| 143 |
$this->createSynchronizer(); |
| 144 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( true ); |
| 145 |
$this->wcAdapter->method( 'isActionSchedulerEnqueueAvailable' )->willReturn( true ); |
| 146 |
|
| 147 |
$order = $this->createMock( Order::class ); |
| 148 |
$order->method( 'getPacketId' )->willReturn( 'packet-id' ); |
| 149 |
$order->method( 'getNumber' )->willReturn( '1' ); |
| 150 |
|
| 151 |
$this->wcAdapter->expects( $this->once() ) |
| 152 |
->method( 'asEnqueueAsyncAction' ) |
| 153 |
->with( 'packetery_consign_password_fetch_hook', [ '1' ] ); |
| 154 |
$this->client->expects( $this->never() )->method( 'packetInfo' ); |
| 155 |
$this->orderRepository->expects( $this->never() )->method( 'getByIdWithValidCarrier' ); |
| 156 |
|
| 157 |
$this->synchronizer->schedule( $order, false ); |
| 158 |
} |
| 159 |
|
| 160 |
public function testScheduleFallsBackToImmediateWhenSchedulerUnavailable(): void { |
| 161 |
$this->createSynchronizer(); |
| 162 |
$this->optionsProvider->method( 'isShowConsignPasswordForZBoxEnabled' )->willReturn( true ); |
| 163 |
$this->wcAdapter->method( 'isActionSchedulerEnqueueAvailable' )->willReturn( false ); |
| 164 |
|
| 165 |
$order = $this->createMock( Order::class ); |
| 166 |
$order->method( 'getPacketId' )->willReturn( 'packet-id' ); |
| 167 |
$order->method( 'getNumber' )->willReturn( '1' ); |
| 168 |
$order->expects( $this->once() )->method( 'setConsignPassword' )->with( '123456789' ); |
| 169 |
$this->orderRepository->method( 'getByIdWithValidCarrier' )->willReturn( $order ); |
| 170 |
$this->orderRepository->expects( $this->once() )->method( 'save' )->with( $order ); |
| 171 |
|
| 172 |
$response = $this->createMock( PacketInfo::class ); |
| 173 |
$response->method( 'hasFault' )->willReturn( false ); |
| 174 |
$response->method( 'getConsignPassword' )->willReturn( '123456789' ); |
| 175 |
$this->client->expects( $this->once() )->method( 'packetInfo' )->willReturn( $response ); |
| 176 |
$this->wcAdapter->expects( $this->never() )->method( 'asEnqueueAsyncAction' ); |
| 177 |
|
| 178 |
$this->synchronizer->schedule( $order, false ); |
| 179 |
} |
| 180 |
} |
| 181 |
|