PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Module / Labels / LabelPrintParametersServiceTest.php

LabelPrintParametersServiceTest.php in Packeta trunk, at tests/Module/Labels/LabelPrintParametersServiceTest.php

204 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tests\Module\Labels;
4
5 use Packetery\Core\Entity\Order;
6 use Packetery\Module\FormFactory;
7 use Packetery\Module\Framework\WpAdapter;
8 use Packetery\Module\Labels\LabelPrintParametersService;
9 use Packetery\Module\Options\OptionsProvider;
10 use Packetery\Module\Order\Repository;
11 use Packetery\Nette\Forms\Form;
12 use Packetery\Nette\Http\Request;
13 use PHPUnit\Framework\MockObject\MockObject;
14 use PHPUnit\Framework\TestCase;
15
16 class LabelPrintParametersServiceTest extends TestCase {
17 private WpAdapter|MockObject $wpAdapterMock;
18 private OptionsProvider|MockObject $optionsProviderMock;
19 private FormFactory|MockObject $formFactoryMock;
20 private Request|MockObject $httpRequestMock;
21 private Repository|MockObject $orderRepositoryMock;
22 private LabelPrintParametersService $labelPrintParametersService;
23
24 protected function createLabelPrintParametersServiceMock(): void {
25 $this->wpAdapterMock = $this->createMock( WpAdapter::class );
26 $this->optionsProviderMock = $this->createMock( OptionsProvider::class );
27 $this->formFactoryMock = $this->createMock( FormFactory::class );
28 $this->httpRequestMock = $this->createMock( Request::class );
29 $this->orderRepositoryMock = $this->createMock( Repository::class );
30
31 $this->labelPrintParametersService = new LabelPrintParametersService(
32 $this->wpAdapterMock,
33 $this->optionsProviderMock,
34 $this->formFactoryMock,
35 $this->httpRequestMock,
36 $this->orderRepositoryMock
37 );
38 }
39
40 public function testGetOffsetWithMaxOffsetZero(): void {
41 $this->createLabelPrintParametersServiceMock();
42
43 $this->optionsProviderMock->expects( $this->once() )
44 ->method( 'getLabelMaxOffset' )
45 ->willReturn( 0 );
46 $this->assertSame( 0, $this->labelPrintParametersService->getOffset() );
47 }
48
49 public function testGetOffsetWhenOffsetQueryExists(): void {
50 $this->createLabelPrintParametersServiceMock();
51 $offsetQueryValue = '3';
52
53 $this->optionsProviderMock->expects( $this->once() )
54 ->method( 'getLabelMaxOffset' )
55 ->willReturn( 5 );
56
57 $this->httpRequestMock->method( 'getQuery' )
58 ->willReturn( $offsetQueryValue );
59
60 $this->wpAdapterMock->method( '__' )
61 ->willReturn( 'foo %s bar' );
62
63 $this->assertSame( (int) $offsetQueryValue, $this->labelPrintParametersService->getOffset() );
64 }
65
66 public function testGetOffsetWhenFormIsSubmitted(): void {
67 $this->createLabelPrintParametersServiceMock();
68 $offset = '2';
69 $submittedOffset = [ 'offset' => $offset ];
70
71 $mockForm = $this->createMock( Form::class );
72 $mockForm->expects( $this->once() )
73 ->method( 'isSubmitted' )
74 ->willReturn( true );
75 $mockForm->expects( $this->once() )
76 ->method( 'getValues' )
77 ->with( 'array' )
78 ->willReturn( $submittedOffset );
79
80 $this->optionsProviderMock->method( 'getLabelMaxOffset' )
81 ->willReturn( 5 );
82 $this->formFactoryMock->expects( $this->once() )
83 ->method( 'create' )
84 ->willReturn( $mockForm );
85 $this->httpRequestMock->method( 'getQuery' )
86 ->willReturn( null );
87 $this->wpAdapterMock->method( '__' )
88 ->willReturn( 'foo %s bar' );
89
90 $this->assertSame( (int) $offset, $this->labelPrintParametersService->getOffset() );
91 }
92
93 public function testGetOffsetWhenNothingIsSet(): void {
94 $this->createLabelPrintParametersServiceMock();
95
96 $mockForm = $this->createMock( Form::class );
97 $mockForm->expects( $this->once() )
98 ->method( 'isSubmitted' )
99 ->willReturn( false );
100
101 $this->optionsProviderMock->expects( $this->once() )
102 ->method( 'getLabelMaxOffset' )
103 ->willReturn( 5 );
104 $this->formFactoryMock->expects( $this->once() )
105 ->method( 'create' )
106 ->willReturn( $mockForm );
107 $this->httpRequestMock->method( 'getQuery' )
108 ->willReturn( null );
109 $this->wpAdapterMock->method( '__' )
110 ->willReturn( 'foo %s bar' );
111
112 $this->assertNull( $this->labelPrintParametersService->getOffset() );
113 }
114
115 /**
116 * Tests that external carrier packet IDs are removed when neither carrier labels nor fallback to Packeta label is allowed.
117 */
118 public function testRemoveExternalCarrierPacketIdsPacketaOnly(): void {
119 $this->createLabelPrintParametersServiceMock();
120
121 $packetIds = [
122 1 => 'packet_1',
123 2 => 'packet_2',
124 ];
125
126 $orderMock1 = $this->createMock( Order::class );
127 $orderMock1->method( 'isExternalCarrier' )->willReturn( false );
128
129 $orderMock2 = $this->createMock( Order::class );
130 $orderMock2->method( 'isExternalCarrier' )->willReturn( true );
131
132 $this->orderRepositoryMock
133 ->method( 'getByIdWithValidCarrier' )
134 ->will(
135 $this->returnValueMap(
136 [
137 [ 1, $orderMock1 ],
138 [ 2, $orderMock2 ],
139 ]
140 )
141 );
142
143 $result = $this->labelPrintParametersService->removeExternalCarrierPacketIds( $packetIds, false, false );
144
145 $this->assertEquals( [ 1 => 'packet_1' ], $result );
146 }
147
148 /**
149 * Tests that packet IDs are not removed when carrier labels are enabled.
150 */
151 public function testRemoveExternalCarrierPacketIdsKeepAll(): void {
152 $this->createLabelPrintParametersServiceMock();
153
154 $packetIds = [
155 1 => 'packet_1',
156 2 => 'packet_2',
157 ];
158
159 $this->orderRepositoryMock->expects( $this->never() )->method( 'getByIdWithValidCarrier' );
160
161 $result = $this->labelPrintParametersService->removeExternalCarrierPacketIds( $packetIds, true, false );
162
163 $this->assertEquals( $packetIds, $result );
164 }
165
166 public function testGetLabelFormatByOrderWithExternalCarrier(): void {
167 $this->createLabelPrintParametersServiceMock();
168
169 $orderMock = $this->createMock( Order::class );
170 $orderMock->expects( $this->once() )
171 ->method( 'isExternalCarrier' )
172 ->willReturn( true );
173
174 $expectedFormat = 'CARRIER_FORMAT';
175 $this->optionsProviderMock->expects( $this->once() )
176 ->method( 'get_carrier_label_format' )
177 ->willReturn( $expectedFormat );
178
179 $this->assertSame(
180 $expectedFormat,
181 $this->labelPrintParametersService->getLabelFormatByOrder( $orderMock )
182 );
183 }
184
185 public function testGetLabelFormatByOrderWithPacketaInternalCarrier(): void {
186 $this->createLabelPrintParametersServiceMock();
187
188 $orderMock = $this->createMock( Order::class );
189 $orderMock->expects( $this->once() )
190 ->method( 'isExternalCarrier' )
191 ->willReturn( false );
192
193 $expectedFormat = 'PACKETA_FORMAT';
194 $this->optionsProviderMock->expects( $this->once() )
195 ->method( 'get_packeta_label_format' )
196 ->willReturn( $expectedFormat );
197
198 $this->assertSame(
199 $expectedFormat,
200 $this->labelPrintParametersService->getLabelFormatByOrder( $orderMock )
201 );
202 }
203 }
204