Permalink
Browse files

Adds (correct) pagination support for crons w/ pagination

  • Loading branch information...
jacobemerick committed Aug 6, 2017
1 parent 757f0c2 commit 0d26023b0600b872132d1aea3a1ad6a1e948f821
Showing with 45 additions and 34 deletions.
  1. +16 −6 src/Cron/Book.php
  2. +9 −2 src/Cron/Distance.php
  3. +10 −13 tests/unit/Cron/BookTest.php
  4. +10 −13 tests/unit/Cron/DistanceTest.php
View
@@ -55,7 +55,10 @@ public function run()
continue;
}
$bookExists = $this->checkBookExists($this->container->get('bookModel'), $book->book_id);
$bookExists = $this->checkBookExists(
$this->container->get('bookModel'),
(string) $book->book_id
);
if ($bookExists) {
$makeNewRequest = false;
continue;
@@ -83,17 +86,24 @@ public function run()
/**
* @param Client $client
* @param string $shelf
* @param integer $page
* @return array
*/
protected function fetchBooks(Client $client, $shelf)
protected function fetchBooks(Client $client, $shelf, $page)
{
$response = $client->request('GET', "/review/list_rss/{$shelf}");
$response = $client->request(
'GET',
"/review/list_rss/{$shelf}",
[
'query' => [ 'page' => $page ],
]
);
if ($response->getStatusCode() !== 200) {
throw new Exception("Error while trying to fetch books: {$response->getStatusCode()}");
}
$rssString = (string) $response->getBody();
$rss = new SimpleXMLElement($rssString);
$rss = new SimpleXMLElement($rssString, LIBXML_NOCDATA);
return $rss->channel->item;
}
@@ -120,8 +130,8 @@ protected function insertBook(BookModel $bookModel, SimpleXMLElement $book, Date
$datetime->setTimezone($timezone);
$result = $bookModel->insertBook(
$book->book_id,
$book->guid,
(string) $book->book_id,
(string) $book->guid,
$datetime,
json_encode($book)
);
View
@@ -78,11 +78,18 @@ public function run()
/**
* @param Client $client
* @param string $username
* @param integer $page
* @return array
*/
protected function fetchEntries(Client $client, $username)
protected function fetchEntries(Client $client, $username, $page)
{
$response = $client->request('GET', "people/{$username}/entries.json");
$response = $client->request(
'GET',
"people/{$username}/entries.json",
[
'query' => [ 'page' => $page ],
]
);
if ($response->getStatusCode() !== 200) {
throw new Exception("Error while trying to fetch entries: {$response->getStatusCode()}");
}
@@ -764,6 +764,7 @@ public function testRunMakesMultipleRequestsIfInitialRequestAllNew()
public function testFetchBooksPullsFromClient()
{
$shelf = 'user_shelf';
$page = 2;
$mockResponse = $this->createMock(Response::class);
$mockResponse->method('getBody')
@@ -776,7 +777,10 @@ public function testFetchBooksPullsFromClient()
->method('request')
->with(
$this->equalTo('GET'),
$this->equalTo("/review/list_rss/{$shelf}")
$this->equalTo("/review/list_rss/{$shelf}"),
$this->equalTo([
'query' => [ 'page' => $page ],
])
)
->willReturn($mockResponse);
@@ -792,6 +796,7 @@ public function testFetchBooksPullsFromClient()
$reflectedFetchBooksMethod->invokeArgs($book, [
$mockClient,
$shelf,
$page,
]);
}
@@ -808,12 +813,7 @@ public function testFetchBooksThrowsExceptionOnNon200Status()
->willReturn(400);
$mockClient = $this->createMock(Client::class);
$mockClient->expects($this->once())
->method('request')
->with(
$this->equalTo('GET'),
$this->equalTo('/review/list_rss/')
)
$mockClient->method('request')
->willReturn($mockResponse);
$book = $this->getMockBuilder(Book::class)
@@ -828,6 +828,7 @@ public function testFetchBooksThrowsExceptionOnNon200Status()
$reflectedFetchBooksMethod->invokeArgs($book, [
$mockClient,
'',
0,
]);
}
@@ -846,12 +847,7 @@ public function testFetchBooksReturnsItems()
->willReturn(200);
$mockClient = $this->createMock(Client::class);
$mockClient->expects($this->once())
->method('request')
->with(
$this->equalTo('GET'),
$this->equalTo('/review/list_rss/')
)
$mockClient->method('request')
->willReturn($mockResponse);
$book = $this->getMockBuilder(Book::class)
@@ -866,6 +862,7 @@ public function testFetchBooksReturnsItems()
$result = $reflectedFetchBooksMethod->invokeArgs($book, [
$mockClient,
'',
0,
]);
$this->assertEquals($xmlItems, $result);
@@ -726,6 +726,7 @@ public function testRunMakesMultipleRequestsIfInitialRequestAllNew()
public function testFetchEntriesPullsFromClient()
{
$username = 'user';
$page = 2;
$mockResponse = $this->createMock(Response::class);
$mockResponse->method('getBody')
@@ -738,7 +739,10 @@ public function testFetchEntriesPullsFromClient()
->method('request')
->with(
$this->equalTo('GET'),
$this->equalTo("people/{$username}/entries.json")
$this->equalTo("people/{$username}/entries.json"),
$this->equalTo([
'query' => [ 'page' => $page ],
])
)
->willReturn($mockResponse);
@@ -754,6 +758,7 @@ public function testFetchEntriesPullsFromClient()
$reflectedFetchEntriesMethod->invokeArgs($distance, [
$mockClient,
$username,
$page,
]);
}
@@ -770,12 +775,7 @@ public function testFetchEntriesThrowsExceptionOnNon200Status()
->willReturn(400);
$mockClient = $this->createMock(Client::class);
$mockClient->expects($this->once())
->method('request')
->with(
$this->equalTo('GET'),
$this->equalTo('people//entries.json')
)
$mockClient->method('request')
->willReturn($mockResponse);
$distance = $this->getMockBuilder(Distance::class)
@@ -790,6 +790,7 @@ public function testFetchEntriesThrowsExceptionOnNon200Status()
$reflectedFetchEntriesMethod->invokeArgs($distance, [
$mockClient,
'',
0,
]);
}
@@ -815,12 +816,7 @@ public function testFetchEntriesReturnsEntries()
->willReturn(200);
$mockClient = $this->createMock(Client::class);
$mockClient->expects($this->once())
->method('request')
->with(
$this->equalTo('GET'),
$this->equalTo('people//entries.json')
)
$mockClient->method('request')
->willReturn($mockResponse);
$distance = $this->getMockBuilder(Distance::class)
@@ -835,6 +831,7 @@ public function testFetchEntriesReturnsEntries()
$result = $reflectedFetchEntriesMethod->invokeArgs($distance, [
$mockClient,
'',
0,
]);
$this->assertEquals($entries, $result);

0 comments on commit 0d26023

Please sign in to comment.