| 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\PacketStatus; |
| 9 |
use Packetery\Core\Entity; |
| 10 |
use Packetery\Core\Entity\Order; |
| 11 |
use Packetery\Core\Log\ILogger; |
| 12 |
use Packetery\Module\Exception\InvalidPasswordException; |
| 13 |
use Packetery\Module\Framework\WcAdapter; |
| 14 |
use Packetery\Module\Framework\WpAdapter; |
| 15 |
use Packetery\Module\Options\OptionsProvider; |
| 16 |
use Packetery\Module\Order\PacketSynchronizer; |
| 17 |
use Packetery\Module\Order\Repository; |
| 18 |
use Packetery\Module\Order\WcOrderActions; |
| 19 |
use PHPUnit\Framework\MockObject\MockObject; |
| 20 |
use PHPUnit\Framework\TestCase; |
| 21 |
use Tests\Core\DummyFactory; |
| 22 |
|
| 23 |
class PacketSynchronizerTest extends TestCase { |
| 24 |
|
| 25 |
private PacketSynchronizer $packetSynchronizer; |
| 26 |
private Client|MockObject $client; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var (object&MockObject)|OptionsProvider|(OptionsProvider&object&MockObject)|(OptionsProvider&MockObject)|MockObject |
| 30 |
*/ |
| 31 |
private MockObject|OptionsProvider $provider; |
| 32 |
|
| 33 |
private function createPacketSynchronize(): void { |
| 34 |
$this->client = $this->createMock( Client::class ); |
| 35 |
$logger = $this->createMock( ILogger::class ); |
| 36 |
$this->provider = $this->createMock( OptionsProvider::class ); |
| 37 |
$orderRepository = $this->createMock( Repository::class ); |
| 38 |
$wcOrderActions = $this->createMock( WcOrderActions::class ); |
| 39 |
$wcAdapter = $this->createMock( WcAdapter::class ); |
| 40 |
$wpAdapter = $this->createMock( WpAdapter::class ); |
| 41 |
$wpAdapter |
| 42 |
->method( '__' ) |
| 43 |
->willReturnCallback( |
| 44 |
static function ( string $key ): string { |
| 45 |
return $key; |
| 46 |
} |
| 47 |
); |
| 48 |
|
| 49 |
$this->packetSynchronizer = new PacketSynchronizer( |
| 50 |
$this->client, |
| 51 |
$logger, |
| 52 |
$this->provider, |
| 53 |
$orderRepository, |
| 54 |
$wcOrderActions, |
| 55 |
$wcAdapter, |
| 56 |
$wpAdapter |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function testSuccessfulSynchronization(): void { |
| 61 |
$this->createPacketSynchronize(); |
| 62 |
$storedUntil = new \DateTimeImmutable( 'now' ); |
| 63 |
$packetStatus = 'some packet status'; |
| 64 |
$order = DummyFactory::createOrderCzPp(); |
| 65 |
$order->setNumber( 'some number' ); |
| 66 |
$order->setPacketId( 'some packet id' ); |
| 67 |
|
| 68 |
$response = $this->createMock( PacketStatus::class ); |
| 69 |
$response->method( 'hasFault' )->willReturn( false ); |
| 70 |
$response->method( 'getStoredUntil' )->willReturn( $storedUntil ); |
| 71 |
$response->method( 'getCodeText' )->willReturn( $packetStatus ); |
| 72 |
|
| 73 |
$this->client->method( 'packetStatus' )->willReturn( $response ); |
| 74 |
|
| 75 |
$this->packetSynchronizer->syncStatus( $order ); |
| 76 |
|
| 77 |
$this->assertSame( $storedUntil, $order->getStoredUntil() ); |
| 78 |
$this->assertSame( $packetStatus, $order->getPacketStatus() ); |
| 79 |
} |
| 80 |
|
| 81 |
public function testSynchronizationHasFault(): void { |
| 82 |
$this->createPacketSynchronize(); |
| 83 |
$order = $this->createMock( Order::class ); |
| 84 |
$order->method( 'getPacketId' )->willReturn( '123456' ); |
| 85 |
$order->method( 'getNumber' )->willReturn( '123456' ); |
| 86 |
|
| 87 |
$response = $this->createMock( PacketStatus::class ); |
| 88 |
$response->method( 'hasFault' )->willReturn( true ); |
| 89 |
$response->method( 'getFaultString' )->willReturn( 'Error message' ); |
| 90 |
$response->expects( $this->never() )->method( 'getStoredUntil' ); |
| 91 |
$response->expects( $this->never() )->method( 'getCodeText' ); |
| 92 |
|
| 93 |
$this->client->method( 'packetStatus' )->willReturn( $response ); |
| 94 |
|
| 95 |
$this->packetSynchronizer->syncStatus( $order ); |
| 96 |
|
| 97 |
$this->assertTrue( true ); // No exceptions thrown. |
| 98 |
} |
| 99 |
|
| 100 |
public function testSynchronizationHasWrongPassword(): void { |
| 101 |
$this->createPacketSynchronize(); |
| 102 |
$order = $this->createMock( Order::class ); |
| 103 |
$order->method( 'getPacketId' )->willReturn( '123456' ); |
| 104 |
$order->method( 'getNumber' )->willReturn( '123456' ); |
| 105 |
|
| 106 |
$response = $this->createMock( PacketStatus::class ); |
| 107 |
$response->method( 'hasFault' )->willReturn( true ); |
| 108 |
$response->method( 'hasWrongPassword' )->willReturn( true ); |
| 109 |
$response->expects( $this->never() )->method( 'getStoredUntil' ); |
| 110 |
$response->expects( $this->never() )->method( 'getCodeText' ); |
| 111 |
|
| 112 |
$this->client->method( 'packetStatus' )->willReturn( $response ); |
| 113 |
|
| 114 |
$this->expectException( InvalidPasswordException::class ); |
| 115 |
$this->packetSynchronizer->syncStatus( $order ); |
| 116 |
} |
| 117 |
|
| 118 |
public function testDefaultPacketStatuses(): void { |
| 119 |
$this->createPacketSynchronize(); |
| 120 |
$packetStatuses = $this->packetSynchronizer->getDefaultPacketStatuses(); |
| 121 |
$this->assertArrayNotHasKey( Entity\PacketStatus::DELIVERED, $packetStatuses ); |
| 122 |
$this->assertArrayNotHasKey( Entity\PacketStatus::RETURNED, $packetStatuses ); |
| 123 |
$this->assertArrayNotHasKey( Entity\PacketStatus::CANCELLED, $packetStatuses ); |
| 124 |
$this->assertArrayNotHasKey( Entity\PacketStatus::UNKNOWN, $packetStatuses ); |
| 125 |
} |
| 126 |
} |
| 127 |
|