Skip to content

Commit cb7833a

Browse files
xh3n1oliverklee
authored andcommitted
[FEATURE] Get the number of subscribers of list and added tests (#116)
Closes #115
1 parent c8e3636 commit cb7833a

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

src/Controller/ListController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* This controller provides REST API access to subscriber lists.
1717
*
1818
* @author Oliver Klee <[email protected]>
19+
* @author Xheni Myrtaj <[email protected]>
1920
*/
2021
class ListController extends FOSRestController implements ClassResourceInterface
2122
{
@@ -96,4 +97,19 @@ public function getMembersAction(Request $request, SubscriberList $list): View
9697

9798
return View::create()->setData($list->getSubscribers());
9899
}
100+
101+
/**
102+
* Gets the total number of subscribers of a list.
103+
*
104+
* @param Request $request
105+
* @param SubscriberList $list
106+
*
107+
* @return View
108+
*/
109+
public function getSubscribersCountAction(Request $request, SubscriberList $list): View
110+
{
111+
$this->requireAuthentication($request);
112+
113+
return View::create()->setData(count($list->getSubscribers()));
114+
}
99115
}

tests/Integration/Controller/AbstractControllerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ protected function getDecodedJsonResponseContent(): array
102102
return json_decode($this->client->getResponse()->getContent(), true);
103103
}
104104

105+
/**
106+
* Returns the response content as int.
107+
*
108+
* @return int
109+
*/
110+
protected function getResponseContentAsInt(): int
111+
{
112+
return json_decode($this->client->getResponse()->getContent(), true);
113+
}
114+
105115
/**
106116
* Asserts that the (decoded) JSON response content is the same as the expected array.
107117
*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
id,name,description,entered,modified,listorder,prefix,active,category,owner
22
1,"News","News (and some fun stuff)","2016-06-22 15:01:17","2016-06-23 19:50:43",12,"phpList",1,"news",1
33
2,"More news","","2016-06-22 15:01:17","2016-06-23 19:50:43",12,"",1,"",1
4+
3,"Tech news","","2019-02-11 15:01:15","2019-02-11 19:50:43",12,"",1,"",1

tests/Integration/Controller/ListControllerTest.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Testcase.
1111
*
1212
* @author Oliver Klee <[email protected]>
13+
* @author Xheni Myrtaj <[email protected]>
1314
*/
1415
class ListControllerTest extends AbstractControllerTest
1516
{
@@ -107,7 +108,17 @@ public function getListsWithCurrentSessionKeyReturnsListData()
107108
'public' => true,
108109
'category' => '',
109110
'id' => 2,
110-
]
111+
],
112+
[
113+
'name' => 'Tech news',
114+
'description' => '',
115+
'creation_date' => '2019-02-11T15:01:15+00:00',
116+
'list_position' => 12,
117+
'subject_prefix' => '',
118+
'public' => true,
119+
'category' => '',
120+
'id' => 3,
121+
],
111122
]
112123
);
113124
}
@@ -320,4 +331,83 @@ public function getListMembersWithCurrentSessionKeyForExistingListWithSubscriber
320331
]
321332
);
322333
}
334+
335+
/**
336+
* @test
337+
*/
338+
public function getListSubscribersCountForExistingListWithoutSessionKeyReturnsForbiddenStatus()
339+
{
340+
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
341+
$this->applyDatabaseChanges();
342+
343+
$this->client->request('get', '/api/v2/lists/1/subscribers/count');
344+
345+
$this->assertHttpForbidden();
346+
}
347+
348+
/**
349+
* @test
350+
*/
351+
public function getListSubscribersCountForExistingListWithExpiredSessionKeyReturnsForbiddenStatus()
352+
{
353+
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
354+
$this->getDataSet()->addTable(static::ADMINISTRATOR_TABLE_NAME, __DIR__ . '/Fixtures/Administrator.csv');
355+
$this->getDataSet()->addTable(static::TOKEN_TABLE_NAME, __DIR__ . '/Fixtures/AdministratorToken.csv');
356+
$this->applyDatabaseChanges();
357+
358+
$this->client->request(
359+
'get',
360+
'/api/v2/lists/1/subscribers/count',
361+
[],
362+
[],
363+
['PHP_AUTH_USER' => 'unused', 'PHP_AUTH_PW' => 'cfdf64eecbbf336628b0f3071adba763']
364+
);
365+
366+
$this->assertHttpForbidden();
367+
}
368+
369+
/**
370+
* @test
371+
*/
372+
public function getListSubscribersCountWithCurrentSessionKeyForExistingListReturnsOkayStatus()
373+
{
374+
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
375+
$this->applyDatabaseChanges();
376+
377+
$this->authenticatedJsonRequest('get', '/api/v2/lists/1/subscribers/count');
378+
379+
$this->assertHttpOkay();
380+
}
381+
382+
/**
383+
* @test
384+
*/
385+
public function getListSubscribersCountWithCurrentSessionKeyForExistingListWithNoSubscribersReturnsZero()
386+
{
387+
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
388+
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/Fixtures/Subscriber.csv');
389+
$this->getDataSet()->addTable(static::SUBSCRIPTION_TABLE_NAME, __DIR__ . '/Fixtures/Subscription.csv');
390+
$this->applyDatabaseChanges();
391+
392+
$this->authenticatedJsonRequest('get', '/api/v2/lists/3/subscribers/count');
393+
$responseContent = $this->getResponseContentAsInt();
394+
395+
static::assertSame(0, $responseContent);
396+
}
397+
398+
/**
399+
* @test
400+
*/
401+
public function getListSubscribersCountWithCurrentSessionKeyForExistingListWithSubscribersReturnsSubscribersCount()
402+
{
403+
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
404+
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/Fixtures/Subscriber.csv');
405+
$this->getDataSet()->addTable(static::SUBSCRIPTION_TABLE_NAME, __DIR__ . '/Fixtures/Subscription.csv');
406+
$this->applyDatabaseChanges();
407+
408+
$this->authenticatedJsonRequest('get', '/api/v2/lists/2/subscribers/count');
409+
$responseContent = $this->getResponseContentAsInt();
410+
411+
static::assertSame(1, $responseContent);
412+
}
323413
}

0 commit comments

Comments
 (0)