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 / Carrier / CarrierUpdaterTest.php

CarrierUpdaterTest.php in Packeta trunk, at tests/Module/Carrier/CarrierUpdaterTest.php

124 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Tests\Module\Tests\Carrier;
6
7 use DateTimeZone;
8 use Packetery\Module\Carrier\CarrierUpdater;
9 use Packetery\Module\Carrier\Downloader;
10 use Packetery\Module\Framework\WpAdapter;
11 use Packetery\Module\Options\OptionNames;
12 use Packetery\Module\Transients;
13 use Packetery\Nette\Http\Request;
14 use PHPUnit\Framework\MockObject\MockObject;
15 use PHPUnit\Framework\TestCase;
16
17 class CarrierUpdaterTest extends TestCase {
18
19 private Request|MockObject $httpRequestMock;
20 private Downloader|MockObject $downloaderMock;
21 private WpAdapter|MockObject $wpAdapterMock;
22
23 private function createCarrierUpdater(): CarrierUpdater {
24 $this->httpRequestMock = $this->createMock( Request::class );
25 $this->downloaderMock = $this->createMock( Downloader::class );
26 $this->wpAdapterMock = $this->createMock( WpAdapter::class );
27
28 return new CarrierUpdater(
29 $this->httpRequestMock,
30 $this->downloaderMock,
31 $this->wpAdapterMock
32 );
33 }
34
35 public function testStartUpdateSetsTransientAndCallsSafeRedirect(): void {
36 $redirectUrl = 'https://example.com';
37
38 $carrierUpdater = $this->createCarrierUpdater();
39 $this->httpRequestMock->method( 'getQuery' )
40 ->with( 'update_carriers' )
41 ->willReturn( '1' );
42
43 $this->wpAdapterMock->expects( $this->once() )
44 ->method( 'setTransient' )
45 ->with( Transients::RUN_UPDATE_CARRIERS, true );
46
47 $this->wpAdapterMock->expects( $this->once() )
48 ->method( 'safeRedirect' )
49 ->with( $redirectUrl )
50 ->willReturn( false );
51
52 $carrierUpdater->startUpdate( $redirectUrl );
53 }
54
55 public function testRunUpdateShouldReturnUpdateParamsAndDeleteTransient(): void {
56 $expectedResult = [ 'some_result' ];
57 $expectedResultClass = 'SuccessClass';
58
59 $carrierUpdater = $this->createCarrierUpdater();
60 $this->wpAdapterMock->method( 'getTransient' )
61 ->with( Transients::RUN_UPDATE_CARRIERS )
62 ->willReturn( true );
63 $this->wpAdapterMock->expects( $this->once() )
64 ->method( 'deleteTransient' )
65 ->with( Transients::RUN_UPDATE_CARRIERS );
66
67 $this->downloaderMock->method( 'run' )
68 ->willReturn( [ $expectedResult, $expectedResultClass ] );
69
70 $result = $carrierUpdater->runUpdate();
71
72 $this->assertEquals(
73 [
74 'result' => $expectedResult,
75 'resultClass' => $expectedResultClass,
76 ],
77 $result
78 );
79 }
80
81 public function testRunUpdateShouldReturnEmptyArrayWhenTransientNotSet(): void {
82 $carrierUpdater = $this->createCarrierUpdater();
83 $this->wpAdapterMock->method( 'getTransient' )
84 ->with( Transients::RUN_UPDATE_CARRIERS )
85 ->willReturn( false );
86
87 $result = $carrierUpdater->runUpdate();
88
89 $this->assertEquals( [], $result );
90 }
91
92 public function testGetLastUpdateShouldReturnFormattedDate(): void {
93 $lastUpdate = '2025-03-12T12:34:56+00:00';
94 $expectedDateString = '12.03.2025 12:34';
95
96 $carrierUpdater = $this->createCarrierUpdater();
97 $this->wpAdapterMock->method( 'getOption' )
98 ->willReturnMap(
99 [
100 [ OptionNames::LAST_CARRIER_UPDATE, $lastUpdate ],
101 [ 'date_format', 'd.m.Y' ],
102 [ 'time_format', 'H:i' ],
103 ]
104 );
105 $timezoneMock = $this->createMock( DateTimeZone::class );
106 $this->wpAdapterMock->method( 'timezone' )->willReturn( $timezoneMock );
107
108 $result = $carrierUpdater->getLastUpdate();
109
110 $this->assertEquals( $expectedDateString, $result );
111 }
112
113 public function testGetLastUpdateShouldReturnNullWhenNoLastUpdate(): void {
114 $carrierUpdater = $this->createCarrierUpdater();
115 $this->wpAdapterMock->method( 'getOption' )
116 ->with( OptionNames::LAST_CARRIER_UPDATE )
117 ->willReturn( false );
118
119 $result = $carrierUpdater->getLastUpdate();
120
121 $this->assertNull( $result );
122 }
123 }
124