| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module\Checkout; |
| 6 |
|
| 7 |
use Packetery\Module\Checkout\SessionService; |
| 8 |
use Packetery\Module\Framework\WcAdapter; |
| 9 |
use PHPUnit\Framework\MockObject\MockObject; |
| 10 |
use PHPUnit\Framework\TestCase; |
| 11 |
|
| 12 |
class SessionServiceTest extends TestCase { |
| 13 |
|
| 14 |
private SessionService $sessionService; |
| 15 |
private WcAdapter|MockObject $wcAdapter; |
| 16 |
|
| 17 |
private function createSessionServiceMock(): void { |
| 18 |
$this->wcAdapter = $this->createMock( WcAdapter::class ); |
| 19 |
$this->sessionService = new SessionService( $this->wcAdapter ); |
| 20 |
} |
| 21 |
|
| 22 |
public function testGetChosenMethodFromSession(): void { |
| 23 |
$this->createSessionServiceMock(); |
| 24 |
|
| 25 |
$this->wcAdapter |
| 26 |
->method( 'session' ) |
| 27 |
->willReturn( true ); |
| 28 |
|
| 29 |
$this->wcAdapter |
| 30 |
->method( 'sessionGetArray' ) |
| 31 |
->with( $this->equalTo( 'chosen_shipping_methods' ) ) |
| 32 |
->willReturn( [ 'dummy_shipping_method' ] ); |
| 33 |
|
| 34 |
$this->assertEquals( 'dummy_shipping_method', $this->sessionService->getChosenMethodFromSession() ); |
| 35 |
} |
| 36 |
|
| 37 |
public function testGetChosenMethodFromSessionWithNoShippingMethods(): void { |
| 38 |
$this->createSessionServiceMock(); |
| 39 |
|
| 40 |
$this->wcAdapter |
| 41 |
->method( 'session' ) |
| 42 |
->willReturn( true ); |
| 43 |
|
| 44 |
$this->wcAdapter |
| 45 |
->method( 'sessionGetArray' ) |
| 46 |
->with( $this->equalTo( 'chosen_shipping_methods' ) ) |
| 47 |
->willReturn( [] ); |
| 48 |
|
| 49 |
$this->assertEquals( '', $this->sessionService->getChosenMethodFromSession() ); |
| 50 |
} |
| 51 |
|
| 52 |
public function testGetChosenMethodFromSessionWithFalseShippingMethod(): void { |
| 53 |
$this->createSessionServiceMock(); |
| 54 |
|
| 55 |
$this->wcAdapter |
| 56 |
->method( 'session' ) |
| 57 |
->willReturn( true ); |
| 58 |
|
| 59 |
$this->wcAdapter |
| 60 |
->method( 'sessionGetArray' ) |
| 61 |
->with( $this->equalTo( 'chosen_shipping_methods' ) ) |
| 62 |
->willReturn( [ false ] ); |
| 63 |
|
| 64 |
$this->assertEquals( '', $this->sessionService->getChosenMethodFromSession() ); |
| 65 |
} |
| 66 |
|
| 67 |
public function testGetChosenMethodFromSessionWithoutSession(): void { |
| 68 |
$this->createSessionServiceMock(); |
| 69 |
|
| 70 |
$this->wcAdapter |
| 71 |
->method( 'session' ) |
| 72 |
->willReturn( null ); |
| 73 |
|
| 74 |
$this->assertEquals( '', $this->sessionService->getChosenMethodFromSession() ); |
| 75 |
} |
| 76 |
|
| 77 |
public function testGetChosenPaymentMethodChosen(): void { |
| 78 |
$this->createSessionServiceMock(); |
| 79 |
|
| 80 |
$this->wcAdapter |
| 81 |
->method( 'sessionGetString' ) |
| 82 |
->with( $this->equalTo( 'chosen_payment_method' ) ) |
| 83 |
->willReturn( 'dummy_shipping_method' ); |
| 84 |
|
| 85 |
$this->assertEquals( 'dummy_shipping_method', $this->sessionService->getChosenPaymentMethod() ); |
| 86 |
} |
| 87 |
|
| 88 |
public function testGetChosenPaymentMethodNotChosen(): void { |
| 89 |
$this->createSessionServiceMock(); |
| 90 |
|
| 91 |
$this->wcAdapter |
| 92 |
->method( 'sessionGetString' ) |
| 93 |
->with( $this->equalTo( 'chosen_payment_method' ) ) |
| 94 |
->willReturn( null ); |
| 95 |
|
| 96 |
$this->assertNull( $this->sessionService->getChosenPaymentMethod() ); |
| 97 |
} |
| 98 |
|
| 99 |
public function testActionUpdateShippingRates(): void { |
| 100 |
$this->createSessionServiceMock(); |
| 101 |
$this->wcAdapter |
| 102 |
->expects( $this->exactly( 2 ) ) |
| 103 |
->method( 'sessionSet' ); |
| 104 |
$this->wcAdapter |
| 105 |
->method( 'shippingGetPackages' ) |
| 106 |
->willReturn( |
| 107 |
[ |
| 108 |
1 => [], |
| 109 |
2 => [], |
| 110 |
] |
| 111 |
); |
| 112 |
$this->sessionService->actionUpdateShippingRates(); |
| 113 |
} |
| 114 |
|
| 115 |
public function testFilterUpdateShippingPackagesWithPaymentMethod(): void { |
| 116 |
$this->createSessionServiceMock(); |
| 117 |
|
| 118 |
$this->wcAdapter |
| 119 |
->method( 'sessionGetString' ) |
| 120 |
->with( $this->equalTo( 'chosen_payment_method' ) ) |
| 121 |
->willReturn( 'dummy_payment_method' ); |
| 122 |
|
| 123 |
$packages = [ |
| 124 |
[ 'random_data' => 'foo' ], |
| 125 |
[ 'random_data' => 'bar' ], |
| 126 |
]; |
| 127 |
|
| 128 |
$expectedPackages = [ |
| 129 |
[ |
| 130 |
'random_data' => 'foo', |
| 131 |
'packetery_payment_method' => 'dummy_payment_method', |
| 132 |
], |
| 133 |
[ |
| 134 |
'random_data' => 'bar', |
| 135 |
'packetery_payment_method' => 'dummy_payment_method', |
| 136 |
], |
| 137 |
]; |
| 138 |
|
| 139 |
$this->assertEquals( |
| 140 |
$expectedPackages, |
| 141 |
$this->sessionService->filterUpdateShippingPackages( $packages ) |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
public function testFilterUpdateShippingPackagesWithoutPaymentMethod(): void { |
| 146 |
$this->createSessionServiceMock(); |
| 147 |
|
| 148 |
$this->wcAdapter |
| 149 |
->method( 'sessionGetString' ) |
| 150 |
->with( $this->equalTo( 'chosen_payment_method' ) ) |
| 151 |
->willReturn( null ); |
| 152 |
|
| 153 |
$packages = [ |
| 154 |
[ 'random_data' => 'foo' ], |
| 155 |
[ 'random_data' => 'bar' ], |
| 156 |
]; |
| 157 |
|
| 158 |
$expectedPackages = [ |
| 159 |
[ |
| 160 |
'random_data' => 'foo', |
| 161 |
'packetery_payment_method' => null, |
| 162 |
], |
| 163 |
[ |
| 164 |
'random_data' => 'bar', |
| 165 |
'packetery_payment_method' => null, |
| 166 |
], |
| 167 |
]; |
| 168 |
|
| 169 |
$this->assertEquals( |
| 170 |
$expectedPackages, |
| 171 |
$this->sessionService->filterUpdateShippingPackages( $packages ) |
| 172 |
); |
| 173 |
} |
| 174 |
} |
| 175 |
|