Skip to content

Commit

Permalink
[FEATURE] Get the number of subscribers of list and added tests (#116)
Browse files Browse the repository at this point in the history
Closes #115
  • Loading branch information
xh3n1 authored and oliverklee committed May 31, 2019
1 parent c8e3636 commit cb7833a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Controller/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* This controller provides REST API access to subscriber lists.
*
* @author Oliver Klee <[email protected]>
* @author Xheni Myrtaj <[email protected]>
*/
class ListController extends FOSRestController implements ClassResourceInterface
{
Expand Down Expand Up @@ -96,4 +97,19 @@ public function getMembersAction(Request $request, SubscriberList $list): View

return View::create()->setData($list->getSubscribers());
}

/**
* Gets the total number of subscribers of a list.
*
* @param Request $request
* @param SubscriberList $list
*
* @return View
*/
public function getSubscribersCountAction(Request $request, SubscriberList $list): View
{
$this->requireAuthentication($request);

return View::create()->setData(count($list->getSubscribers()));
}
}
10 changes: 10 additions & 0 deletions tests/Integration/Controller/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ protected function getDecodedJsonResponseContent(): array
return json_decode($this->client->getResponse()->getContent(), true);
}

/**
* Returns the response content as int.
*
* @return int
*/
protected function getResponseContentAsInt(): int
{
return json_decode($this->client->getResponse()->getContent(), true);
}

/**
* Asserts that the (decoded) JSON response content is the same as the expected array.
*
Expand Down
1 change: 1 addition & 0 deletions tests/Integration/Controller/Fixtures/SubscriberList.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
id,name,description,entered,modified,listorder,prefix,active,category,owner
1,"News","News (and some fun stuff)","2016-06-22 15:01:17","2016-06-23 19:50:43",12,"phpList",1,"news",1
2,"More news","","2016-06-22 15:01:17","2016-06-23 19:50:43",12,"",1,"",1
3,"Tech news","","2019-02-11 15:01:15","2019-02-11 19:50:43",12,"",1,"",1
92 changes: 91 additions & 1 deletion tests/Integration/Controller/ListControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Testcase.
*
* @author Oliver Klee <[email protected]>
* @author Xheni Myrtaj <[email protected]>
*/
class ListControllerTest extends AbstractControllerTest
{
Expand Down Expand Up @@ -107,7 +108,17 @@ public function getListsWithCurrentSessionKeyReturnsListData()
'public' => true,
'category' => '',
'id' => 2,
]
],
[
'name' => 'Tech news',
'description' => '',
'creation_date' => '2019-02-11T15:01:15+00:00',
'list_position' => 12,
'subject_prefix' => '',
'public' => true,
'category' => '',
'id' => 3,
],
]
);
}
Expand Down Expand Up @@ -320,4 +331,83 @@ public function getListMembersWithCurrentSessionKeyForExistingListWithSubscriber
]
);
}

/**
* @test
*/
public function getListSubscribersCountForExistingListWithoutSessionKeyReturnsForbiddenStatus()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->applyDatabaseChanges();

$this->client->request('get', '/api/v2/lists/1/subscribers/count');

$this->assertHttpForbidden();
}

/**
* @test
*/
public function getListSubscribersCountForExistingListWithExpiredSessionKeyReturnsForbiddenStatus()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->getDataSet()->addTable(static::ADMINISTRATOR_TABLE_NAME, __DIR__ . '/Fixtures/Administrator.csv');
$this->getDataSet()->addTable(static::TOKEN_TABLE_NAME, __DIR__ . '/Fixtures/AdministratorToken.csv');
$this->applyDatabaseChanges();

$this->client->request(
'get',
'/api/v2/lists/1/subscribers/count',
[],
[],
['PHP_AUTH_USER' => 'unused', 'PHP_AUTH_PW' => 'cfdf64eecbbf336628b0f3071adba763']
);

$this->assertHttpForbidden();
}

/**
* @test
*/
public function getListSubscribersCountWithCurrentSessionKeyForExistingListReturnsOkayStatus()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->applyDatabaseChanges();

$this->authenticatedJsonRequest('get', '/api/v2/lists/1/subscribers/count');

$this->assertHttpOkay();
}

/**
* @test
*/
public function getListSubscribersCountWithCurrentSessionKeyForExistingListWithNoSubscribersReturnsZero()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/Fixtures/Subscriber.csv');
$this->getDataSet()->addTable(static::SUBSCRIPTION_TABLE_NAME, __DIR__ . '/Fixtures/Subscription.csv');
$this->applyDatabaseChanges();

$this->authenticatedJsonRequest('get', '/api/v2/lists/3/subscribers/count');
$responseContent = $this->getResponseContentAsInt();

static::assertSame(0, $responseContent);
}

/**
* @test
*/
public function getListSubscribersCountWithCurrentSessionKeyForExistingListWithSubscribersReturnsSubscribersCount()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/Fixtures/Subscriber.csv');
$this->getDataSet()->addTable(static::SUBSCRIPTION_TABLE_NAME, __DIR__ . '/Fixtures/Subscription.csv');
$this->applyDatabaseChanges();

$this->authenticatedJsonRequest('get', '/api/v2/lists/2/subscribers/count');
$responseContent = $this->getResponseContentAsInt();

static::assertSame(1, $responseContent);
}
}

0 comments on commit cb7833a

Please sign in to comment.