PluginProbe
Packeta / 2.1.2
Packeta v2.1.2
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 / Options / FlagManager / FeatureFlagDownloaderTest.php

FeatureFlagDownloaderTest.php in Packeta 2.1.2, at tests/Module/Options/FlagManager/FeatureFlagDownloaderTest.php

164 lines 4.7 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\Options\FlagManager;
6
7 use DateTimeImmutable;
8 use DateTimeZone;
9 use Packetery\Core\CoreHelper;
10 use Packetery\Module\Framework\WcAdapter;
11 use Packetery\Module\Options\FlagManager\FeatureFlagDownloader;
12 use Packetery\Module\Options\FlagManager\FeatureFlagStorage;
13 use Packetery\Module\Options\OptionNames;
14 use Packetery\Module\Options\OptionsProvider;
15 use PHPUnit\Framework\TestCase;
16 use Tests\Module\MockFactory;
17
18 class FeatureFlagDownloaderTest extends TestCase {
19 public static function getFlagsProvider(): array {
20 $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
21 $nowFormatted = $now->format( CoreHelper::MYSQL_DATETIME_FORMAT );
22
23 $fresh = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
24 $fresh = $fresh->modify( '-2 hours' );
25 $freshFormatted = $fresh->format( CoreHelper::MYSQL_DATETIME_FORMAT );
26
27 $old = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
28 $old = $old->modify( '-8 hours' );
29 $oldFormatted = $old->format( CoreHelper::MYSQL_DATETIME_FORMAT );
30
31 $freshData = [
32 'splitActive' => false,
33 'lastDownload' => $nowFormatted,
34 ];
35 $freshEnoughData = [
36 'splitActive' => false,
37 'lastDownload' => $freshFormatted,
38 ];
39 $oldData = [
40 'splitActive' => false,
41 'lastDownload' => $oldFormatted,
42 ];
43
44 return [
45 'fills storage from wp options when storage empty' => [
46 'apiKey' => 'dummy-api-key',
47 'errors' => false,
48 'getFromStorageCount' => 3,
49 'dataInStorage' => [ null, $freshEnoughData, $freshEnoughData ],
50 'dataInOptions' => $freshEnoughData,
51 'dataFromApi' => [
52 'split' => false,
53 ],
54 'expectedResult' => $freshEnoughData,
55 ],
56 'returns from storage in case of errors' => [
57 'apiKey' => 'dummy-api-key',
58 'errors' => true,
59 'getFromStorageCount' => 2,
60 'dataInStorage' => [ null, $oldData, $oldData ],
61 'dataInOptions' => $oldData,
62 'dataFromApi' => [
63 'split' => false,
64 ],
65 'expectedResult' => $oldData,
66 ],
67 'returns empty in case of no api key' => [
68 'apiKey' => null,
69 'errors' => false,
70 'getFromStorageCount' => 3,
71 'dataInStorage' => [ null, null, [] ],
72 'dataInOptions' => false,
73 'dataFromApi' => [],
74 'expectedResult' => [],
75 ],
76 'downloads when storage empty' => [
77 'apiKey' => 'dummy-api-key',
78 'errors' => false,
79 'getFromStorageCount' => 3,
80 'dataInStorage' => [ null, null, $freshData ],
81 'dataInOptions' => false,
82 'dataFromApi' => [
83 'split' => false,
84 ],
85 'expectedResult' => $freshData,
86 ],
87 'downloads when storage holds old data' => [
88 'apiKey' => 'dummy-api-key',
89 'errors' => false,
90 'getFromStorageCount' => 3,
91 'dataInStorage' => [ null, $oldData, $freshData ],
92 'dataInOptions' => $oldData,
93 'dataFromApi' => [
94 'split' => false,
95 ],
96 'expectedResult' => $freshData,
97 ],
98 ];
99 }
100
101 /**
102 * @dataProvider getFlagsProvider
103 */
104 public function testGetFlags(
105 ?string $apiKey,
106 bool $errors,
107 int $getFromStorageCount,
108 array $dataInStorage,
109 array|false $dataInOptions,
110 array $dataFromApi,
111 array $expectedResult,
112 ): void {
113 $optionsProvider = $this->createMock( OptionsProvider::class );
114 $optionsProvider
115 ->method( 'get_api_key' )
116 ->willReturn( $apiKey );
117
118 $wpAdapter = MockFactory::createWpAdapter( $this );
119 $wpAdapter
120 ->method( 'getOption' )
121 ->willReturnCallback(
122 function ( $option ) use ( $dataInOptions, $errors ) {
123 if ( $option === OptionNames::FEATURE_FLAGS ) {
124 return $dataInOptions;
125 }
126 if ( $option === OptionNames::FEATURE_FLAGS_DISABLED_DUE_ERRORS ) {
127 return $errors;
128 }
129 self::fail( 'unexpected option: ' . $option );
130 }
131 );
132 $wpAdapter
133 ->method( 'remoteRetrieveBody' )
134 ->willReturn(
135 // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
136 json_encode(
137 [
138 'features' => $dataFromApi,
139 ]
140 )
141 );
142 $wpAdapter->method( 'addQueryArg' )
143 ->willReturn( 'dummyUrl' );
144
145 $wcAdapter = $this->createMock( WcAdapter::class );
146
147 $storageMock = $this->createMock( FeatureFlagStorage::class );
148 $storageMock
149 ->expects( $this->exactly( $getFromStorageCount ) )
150 ->method( 'getFlags' )
151 ->willReturnOnConsecutiveCalls( ...$dataInStorage );
152
153 $downloader = new FeatureFlagDownloader(
154 'dummy-url',
155 $optionsProvider,
156 $wpAdapter,
157 $wcAdapter,
158 $storageMock,
159 );
160
161 self::assertEquals( $expectedResult, $downloader->getFlags() );
162 }
163 }
164