@@ -117,4 +117,126 @@ public function testCreateSubscriptionHeaders(): void
117117 );
118118 self ::assertSame (['application/json ' ], $ request ->getHeaders ()['Content-type ' ]);
119119 }
120+
121+ public function testDeleteSubscriptionHeaders (): void
122+ {
123+ $ mock = new MockHandler ([
124+ new Response (204 , [], '' ),
125+ ]);
126+ $ requestHistory = [];
127+ $ history = Middleware::history ($ requestHistory );
128+ $ stack = HandlerStack::create ($ mock );
129+ $ stack ->push ($ history );
130+ $ client = new SubscriptionClient (
131+ 'https://example.com/ ' ,
132+ 'testToken ' ,
133+ ['handler ' => $ stack , 'backoffMaxTries ' => 3 , 'userAgent ' => 'Test ' ],
134+ );
135+
136+ $ client ->deleteSubscription ('subscription-123 ' );
137+
138+ /** @var Request $request */
139+ $ request = $ requestHistory [0 ]['request ' ];
140+ self ::assertSame ('DELETE ' , $ request ->getMethod ());
141+ self ::assertSame ('project-subscriptions/subscription-123 ' , (string ) $ request ->getUri ());
142+ self ::assertSame (
143+ ['User-Agent ' , 'X-StorageApi-Token ' , 'Host ' ],
144+ array_keys ($ request ->getHeaders ()),
145+ );
146+ }
147+
148+ public function testListSubscriptionsHeaders (): void
149+ {
150+ $ responseBody = json_encode ([
151+ [
152+ 'id ' => 'sub-1 ' ,
153+ 'event ' => 'job-failed ' ,
154+ 'filters ' => [
155+ ['field ' => 'project.id ' , 'value ' => '123 ' ],
156+ ],
157+ 'recipient ' => ['channel ' => 'email ' , 'address ' => 'a@example.com ' ],
158+ ],
159+ [
160+ 'id ' => 'sub-2 ' ,
161+ 'event ' => 'job-succeeded ' ,
162+ 'filters ' => [],
163+ 'recipient ' => ['channel ' => 'email ' , 'address ' => 'b@example.com ' ],
164+ ],
165+ ]);
166+
167+ $ mock = new MockHandler ([
168+ new Response (200 , ['Content-Type ' => 'application/json ' ], (string ) $ responseBody ),
169+ ]);
170+ $ requestHistory = [];
171+ $ history = Middleware::history ($ requestHistory );
172+ $ stack = HandlerStack::create ($ mock );
173+ $ stack ->push ($ history );
174+ $ client = new SubscriptionClient (
175+ 'https://example.com/ ' ,
176+ 'testToken ' ,
177+ ['handler ' => $ stack , 'backoffMaxTries ' => 3 , 'userAgent ' => 'Test ' ],
178+ );
179+
180+ $ result = $ client ->listSubscriptions ();
181+
182+ /** @var Request $request */
183+ $ request = $ requestHistory [0 ]['request ' ];
184+ self ::assertSame ('GET ' , $ request ->getMethod ());
185+ self ::assertSame ('project-subscriptions ' , (string ) $ request ->getUri ());
186+ self ::assertSame (
187+ ['User-Agent ' , 'X-StorageApi-Token ' , 'Host ' ],
188+ array_keys ($ request ->getHeaders ()),
189+ );
190+
191+ self ::assertCount (2 , $ result );
192+ self ::assertContainsOnlyInstancesOf (
193+ \Keboola \NotificationClient \Responses \Subscription::class,
194+ $ result ,
195+ );
196+ self ::assertSame ('sub-1 ' , $ result [0 ]->getId ());
197+ self ::assertSame ('job-failed ' , $ result [0 ]->getEvent ());
198+ self ::assertSame ('project.id ' , $ result [0 ]->getFilters ()[0 ]->getField ());
199+ self ::assertSame ('123 ' , $ result [0 ]->getFilters ()[0 ]->getValue ());
200+ self ::assertSame ('email ' , $ result [0 ]->getRecipientChannel ());
201+ self ::assertSame ('a@example.com ' , $ result [0 ]->getRecipientAddress ());
202+ self ::assertSame ('sub-2 ' , $ result [1 ]->getId ());
203+ }
204+
205+ public function testListAndDeleteSubscriptionLifecycle (): void
206+ {
207+ $ client = $ this ->getClient ();
208+
209+ // create
210+ $ created = $ client ->createSubscription (new Subscription (
211+ 'job-failed ' ,
212+ new EmailRecipient ('ajda-2680@example.com ' ),
213+ [
214+ new Filter ('project.id ' , (string ) getenv ('TEST_STORAGE_API_PROJECT_ID ' )),
215+ ],
216+ ));
217+ self ::assertNotEmpty ($ created ->getId ());
218+
219+ // list — must contain the new subscription
220+ $ beforeDelete = $ client ->listSubscriptions ();
221+ self ::assertContainsOnlyInstancesOf (
222+ \Keboola \NotificationClient \Responses \Subscription::class,
223+ $ beforeDelete ,
224+ );
225+ $ ids = array_map (
226+ fn (\Keboola \NotificationClient \Responses \Subscription $ s ): string => $ s ->getId (),
227+ $ beforeDelete ,
228+ );
229+ self ::assertContains ($ created ->getId (), $ ids );
230+
231+ // delete
232+ $ client ->deleteSubscription ($ created ->getId ());
233+
234+ // list — must no longer contain it
235+ $ afterDelete = $ client ->listSubscriptions ();
236+ $ idsAfter = array_map (
237+ fn (\Keboola \NotificationClient \Responses \Subscription $ s ): string => $ s ->getId (),
238+ $ afterDelete ,
239+ );
240+ self ::assertNotContains ($ created ->getId (), $ idsAfter );
241+ }
120242}
0 commit comments