PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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.0.9, at tests/Module/Options/FlagManager/FeatureFlagDownloaderTest.php

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