Skip to content

Commit d87b945

Browse files
committed
Fix routing for cursorUserCollection, remove unnecessary stuff, add test cases
- remove some unnecessary stuff from the `EntryFrontController` - Add test case for the `cursorUserCollection` endpoint in the `CombinedRetrieveApi` - assert the item counts in the pagination test
1 parent fa1b3a7 commit d87b945

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

assets/controllers/infinite_scroll_controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export default class extends Controller {
2525
const cursorPaginationElement = this.paginationTarget.getElementsByClassName('cursor-pagination');
2626
let paginationElem = null;
2727
if (cursorPaginationElement.length) {
28-
console.log(cursorPaginationElement[0]);
2928
const button = cursorPaginationElement[0].getElementsByTagName('a');
3029
if (!button.length) {
3130
throw new Error('No more pages');

config/mbin_routes/combined_api.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ api_combined_cursor:
66

77
api_combined_user_cursor:
88
controller: App\Controller\Api\Combined\CombinedRetrieveApi::cursorUserCollection
9-
path: /api/combined/2.0/{contentType}
9+
path: /api/combined/2.0/{collectionType}
1010
requirements:
11-
contentType: subscribed|moderated|favourited
12-
methods: [ PUT ]
11+
collectionType: subscribed|moderated|favourited
12+
methods: [ GET ]
1313
format: json
1414

1515
api_combined:

src/Controller/Entry/EntryFrontController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public function front(
6565
}
6666

6767
$entities = $this->contentRepository->findByCriteriaCursored($criteria, $this->getCursorByCriteria($criteria, $cursor));
68-
$page = $entities->getCurrentPageResults();
6968
$templatePath = 'content/';
7069
$dataKey = 'results';
7170

@@ -313,7 +312,6 @@ private function getCursorByCriteria(Criteria $criteria, ?string $cursor): int|\
313312
$guessedCursor = $this->contentRepository->guessInitialCursor($criteria);
314313
if ($guessedCursor instanceof \DateTimeImmutable) {
315314
$currentCursor = null !== $cursor ? new \DateTimeImmutable($cursor) : $guessedCursor;
316-
// $currentCursor = null !== $cursor ? (new \DateTimeImmutable)->setTimestamp(intval($cursor)) : $guessedCursor;
317315
} elseif (\is_int($guessedCursor)) {
318316
$currentCursor = null !== $cursor ? \intval($cursor) : $guessedCursor;
319317
} else {

tests/Functional/Controller/Api/Combined/CombinedRetrieveApiTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55
namespace App\Tests\Functional\Controller\Api\Combined;
66

77
use App\Entity\Magazine;
8+
use App\Entity\User;
89
use App\Tests\WebTestCase;
910

1011
use function PHPUnit\Framework\assertEquals;
1112

1213
class CombinedRetrieveApiTest extends WebTestCase
1314
{
1415
private Magazine $magazine;
16+
private User $user;
1517
private array $generatedEntries = [];
1618
private array $generatedPosts = [];
1719

1820
public function setUp(): void
1921
{
2022
parent::setUp();
2123
$this->magazine = $this->getMagazineByName('acme');
24+
$this->user = $this->getUserByUsername('user');
25+
$this->magazineManager->subscribe($this->magazine, $this->user);
2226
for ($i = 0; $i < 10; ++$i) {
2327
$entry = $this->getEntryByTitle("Test Entry $i", magazine: $this->magazine);
2428
$entry->createdAt = new \DateTimeImmutable("now - $i minutes");
@@ -58,6 +62,24 @@ public function testCombinedCursoredAnonymous(): void
5862

5963
self::assertResponseIsSuccessful();
6064
$data = self::getJsonResponse($this->client);
65+
$this->assertCursorDataShape($data);
66+
}
67+
68+
public function testUserCombinedCursored(): void
69+
{
70+
$this->client->loginUser($this->user);
71+
self::createOAuth2PublicAuthCodeClient();
72+
$codes = self::getPublicAuthorizationCodeTokenResponse($this->client, scopes: 'read');
73+
$token = $codes['token_type'].' '.$codes['access_token'];
74+
$this->client->request('GET', '/api/combined/2.0/subscribed?perPage=2&sort=newest', server: ['HTTP_AUTHORIZATION' => $token]);
75+
76+
self::assertResponseIsSuccessful();
77+
$data = self::getJsonResponse($this->client);
78+
$this->assertCursorDataShape($data);
79+
}
80+
81+
private function assertCursorDataShape(array $data): void
82+
{
6183
self::assertArrayKeysMatch(WebTestCase::PAGINATED_KEYS, $data);
6284

6385
self::assertCount(2, $data['items']);
@@ -73,7 +95,7 @@ public function testCombinedCursoredAnonymous(): void
7395
self::assertNull($data['items'][1]['entry']);
7496
assertEquals($this->generatedPosts[0]->getId(), $data['items'][1]['post']['postId']);
7597

76-
$this->client->request('GET', '/api/combined/2.0?perPage=2&sort=newest&cursor='.urlencode($data['pagination']['nextCursor']));
98+
$this->client->request('GET', '/api/combined/2.0?perPage=2&sort=newest&cursor=' . urlencode($data['pagination']['nextCursor']));
7799
self::assertResponseIsSuccessful();
78100
$data = self::getJsonResponse($this->client);
79101

tests/Unit/CursorPaginationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function testCursorPaginationInteger(): void
5151
self::assertEquals($i, $result['value']);
5252
++$i;
5353
}
54+
self::assertEquals(3, $i);
5455

5556
self::assertTrue($this->cursorPagination->hasNextPage());
5657
self::assertFalse($this->cursorPagination->hasPreviousPage());
@@ -63,6 +64,7 @@ public function testCursorPaginationInteger(): void
6364
self::assertEquals($i, $result['value']);
6465
++$i;
6566
}
67+
self::assertEquals(6, $i);
6668

6769
self::assertTrue($this->cursorPagination->hasNextPage());
6870
self::assertTrue($this->cursorPagination->hasPreviousPage());
@@ -75,6 +77,7 @@ public function testCursorPaginationInteger(): void
7577
self::assertEquals($i, $result['value']);
7678
++$i;
7779
}
80+
self::assertEquals(9, $i);
7881

7982
self::assertTrue($this->cursorPagination->hasNextPage());
8083
self::assertTrue($this->cursorPagination->hasPreviousPage());
@@ -87,6 +90,7 @@ public function testCursorPaginationInteger(): void
8790
self::assertEquals($i, $result['value']);
8891
++$i;
8992
}
93+
self::assertEquals(10, $i);
9094

9195
self::assertFalse($this->cursorPagination->hasNextPage());
9296
self::assertTrue($this->cursorPagination->hasPreviousPage());
@@ -99,6 +103,7 @@ public function testCursorPaginationInteger(): void
99103
self::assertEquals($i, $result['value']);
100104
++$i;
101105
}
106+
self::assertEquals(9, $i);
102107

103108
self::assertTrue($this->cursorPagination->hasNextPage());
104109
self::assertTrue($this->cursorPagination->hasPreviousPage());
@@ -111,6 +116,7 @@ public function testCursorPaginationInteger(): void
111116
self::assertEquals($i, $result['value']);
112117
++$i;
113118
}
119+
self::assertEquals(6, $i);
114120

115121
self::assertTrue($this->cursorPagination->hasNextPage());
116122
self::assertTrue($this->cursorPagination->hasPreviousPage());
@@ -123,6 +129,7 @@ public function testCursorPaginationInteger(): void
123129
self::assertEquals($i, $result['value']);
124130
++$i;
125131
}
132+
self::assertEquals(3, $i);
126133
self::assertTrue($this->cursorPagination->hasNextPage());
127134
self::assertFalse($this->cursorPagination->hasPreviousPage());
128135
}

0 commit comments

Comments
 (0)