Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Get the number of subscribers of list and added tests #116

Merged
merged 7 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
*
* @param Request $request
* @param SubscriberList $list
*
* @return View
*/
public function getCountsubscribersAction(Request $request, SubscriberList $list): View
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
{
$this->requireAuthentication($request);

return View::create()->setData(count($list->getSubscribers()));
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 getListCountsubscribersForExistingListWithoutSessionKeyReturnsForbiddenStatus()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->applyDatabaseChanges();

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

$this->assertHttpForbidden();
}

/**
* @test
*/
public function getListCountsubscribersForExistingListWithExpiredSessionKeyReturnsForbiddenStatus()
{
$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/countsubscribers',
[],
[],
['PHP_AUTH_USER' => 'unused', 'PHP_AUTH_PW' => 'cfdf64eecbbf336628b0f3071adba763']
);

$this->assertHttpForbidden();
}

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

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

$this->assertHttpOkay();
}

/**
* @test
*/
public function getListCountsubscribersWithCurrentSessionKeyForExistingListWithNoSubscribersReturnsZero()
{
$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/countsubscribers');
$response = $this->getResponseContentAsInt();
oliverklee marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* @test
*/
public function getListCountsubscribersWithCurrentSessionKeyForExistingListWithSubscribersReturnsSubscribersCount()
{
$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/countsubscribers');
$response = $this->getResponseContentAsInt();

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