From 8fb00c746f5f968459532c61cfcff3fc46074580 Mon Sep 17 00:00:00 2001 From: Xheni Myrtaj Date: Mon, 28 Jan 2019 00:30:01 +0100 Subject: [PATCH] get the number of subscribers of list, Fix #115 Signed-off-by: Xheni Myrtaj --- src/Controller/ListController.php | 15 +++++ .../Controller/ListControllerTest.php | 61 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/Controller/ListController.php b/src/Controller/ListController.php index f54006f..cfe249b 100644 --- a/src/Controller/ListController.php +++ b/src/Controller/ListController.php @@ -16,6 +16,7 @@ * This controller provides REST API access to subscriber lists. * * @author Oliver Klee + * @author Xheni Myrtaj */ class ListController extends FOSRestController implements ClassResourceInterface { @@ -96,4 +97,18 @@ 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 getNumberAction(Request $request, SubscriberList $list): View + { + $this->requireAuthentication($request); + + return View::create()->setData(count($list->getSubscribers())); + } } diff --git a/tests/Integration/Controller/ListControllerTest.php b/tests/Integration/Controller/ListControllerTest.php index ff19769..b634fb4 100644 --- a/tests/Integration/Controller/ListControllerTest.php +++ b/tests/Integration/Controller/ListControllerTest.php @@ -10,6 +10,8 @@ * Testcase. * * @author Oliver Klee + * @author Xheni Myrtaj + * */ class ListControllerTest extends AbstractControllerTest { @@ -320,4 +322,63 @@ public function getListMembersWithCurrentSessionKeyForExistingListWithSubscriber ] ); } + + /** + * @test + */ + public function getListNumberForExistingListWithoutSessionKeyReturnsForbiddenStatus() + { + $this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv'); + $this->applyDatabaseChanges(); + + $this->client->request('get', '/api/v2/lists/1/number'); + + $this->assertHttpForbidden(); + } + + /** + * @test + */ + public function getListNumberForExistingListWithExpiredSessionKeyReturnsForbiddenStatus() + { + $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/number', + [], + [], + ['PHP_AUTH_USER' => 'unused', 'PHP_AUTH_PW' => 'cfdf64eecbbf336628b0f3071adba763'] + ); + + $this->assertHttpForbidden(); + } + + /** + * @test + */ + public function getListNumberWithCurrentSessionKeyForExistingListReturnsOkayStatus() + { + $this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv'); + $this->applyDatabaseChanges(); + + $this->authenticatedJsonRequest('get', '/api/v2/lists/1/number'); + + $this->assertHttpOkay(); + } + + public function getListNumberWithCurrentSessionKeyForExistingListWithSubscribersReturnsSubscribers() + { + $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/number'); + + $this->assertEquals(1, true); + } }