Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit f352cab

Browse files
author
anatolii
committed
Add argument to getNextPage - issue #2
Improve test code coverage - issue #7
1 parent 3dd0906 commit f352cab

File tree

2 files changed

+138
-46
lines changed

2 files changed

+138
-46
lines changed

src/RecursivePagination.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,17 @@ class RecursivePagination {
3030
/**
3131
* @param Grabber $grabber
3232
* @param array $xpath
33-
* @throws \Exception
3433
*/
3534
public function __construct(Grabber $grabber, $xpath = []) {
3635
$this->grabber = $grabber;
37-
if (!is_string($xpath) and !is_array($xpath)) {
38-
throw new \InvalidArgumentException('xPath should be an array or a string');
39-
}
40-
$xpath = (array) $xpath;
41-
foreach ($xpath as $path) {
42-
if (!is_string($path)) {
43-
throw new \InvalidArgumentException('Incorrect xPath, should be an array or a string');
44-
}
45-
$this->defaultXpath[] = $path;
46-
}
36+
$this->addXpath($xpath);
4737
}
4838

4939

5040
/**
5141
* @param array|string $links
5242
* @param bool $state
53-
* @throws \Exception
43+
* @throws \InvalidArgumentException
5444
* @return $this
5545
*/
5646
public function addToQueue($links, $state = false) {
@@ -70,21 +60,23 @@ public function addToQueue($links, $state = false) {
7060

7161

7262
/**
63+
* @param null $customXpath
7364
* @return \Fiv\Parser\Dom\ElementFinder|null
7465
*/
75-
public function getNextPage() {
66+
public function getNextPage($customXpath = null) {
67+
if (isset($customXpath)) {
68+
$this->addXpath($customXpath);
69+
}
7670
$page = $this->grabber->getLastPage();
7771
if (!empty($page)) {
78-
foreach ($this->defaultXpath as $xPath) {
79-
$queueLinks = $page->attribute($xPath)->getItems();
80-
72+
foreach ($this->defaultXpath as $xpath) {
73+
$queueLinks = $page->attribute($xpath)->getItems();
8174
if (!empty($queueLinks)) {
8275
$queueLinks = array_combine($queueLinks, array_fill(0, count($queueLinks), false));
8376
$this->queue = array_merge($queueLinks, $this->queue);
8477
}
8578
}
8679
}
87-
8880
$link = array_search(false, $this->queue, true);
8981

9082
if (empty($link)) {
@@ -94,4 +86,23 @@ public function getNextPage() {
9486
$this->queue[$link] = true;
9587
return $this->grabber->getHtml($link);
9688
}
89+
90+
91+
/**
92+
* @param string|array $xpath
93+
* @throws \InvalidArgumentException
94+
*/
95+
private function addXpath($xpath) {
96+
if (!is_string($xpath) and !is_array($xpath)) {
97+
throw new \InvalidArgumentException('Xpath should be an array or a string');
98+
}
99+
$xpath = (array) $xpath;
100+
foreach ($xpath as $path) {
101+
if (!is_string($path)) {
102+
throw new \InvalidArgumentException('Incorrect xpath, should be an array or a string');
103+
}
104+
$this->defaultXpath[] = $path;
105+
}
106+
}
107+
97108
}

tests/RecursivePaginationTest.php

Lines changed: 110 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
namespace Xparse\RecursivePagination\Test;
44

5-
class RecursivePaginationTest extends \PHPUnit_Framework_TestCase
6-
{
5+
use InvalidArgumentException;
6+
use Xparse\RecursivePagination\RecursivePagination;
77

8-
public function testAllLinks()
9-
{
10-
$grabber = new \Xparse\RecursivePagination\Test\TestGrabber();
8+
/**
9+
*
10+
* @package Xparse\RecursivePagination\Test
11+
*/
12+
class RecursivePaginationTest extends \PHPUnit_Framework_TestCase {
13+
14+
15+
/**
16+
* Asserts that $allLinks array has 22 elements.
17+
*
18+
*/
19+
public function testAllLinks() {
20+
$grabber = new TestGrabber();
1121
$linksArrayPath = ["//span[@class='inner']/a/@href", "//a[@class='pagenav']/@href"];
1222

13-
$paginator = new \Xparse\RecursivePagination\RecursivePagination($grabber, $linksArrayPath);
23+
$paginator = new RecursivePagination($grabber, $linksArrayPath);
1424
$paginator->addToQueue('osmosis/page1.html');
1525

1626
$allLinks = [];
@@ -21,12 +31,16 @@ public function testAllLinks()
2131
$this->assertTrue(count($allLinks) == 22);
2232
}
2333

24-
public function testOneLink()
25-
{
26-
$grabber = new \Xparse\RecursivePagination\Test\TestGrabber();
34+
35+
/**
36+
* Asserts that $allLinks array has 10 elements.
37+
*
38+
*/
39+
public function testOneLink() {
40+
$grabber = new TestGrabber();
2741
$linksArrayPath = ["//span[@class='inner'][1]/a/@href", "//a[@class='pagenav']/@href"];
2842

29-
$paginator = new \Xparse\RecursivePagination\RecursivePagination($grabber, $linksArrayPath);
43+
$paginator = new RecursivePagination($grabber, $linksArrayPath);
3044
$paginator->addToQueue('osmosis/page1.html');
3145

3246
$allLinks = [];
@@ -37,37 +51,104 @@ public function testOneLink()
3751
$this->assertTrue(count($allLinks) == 10);
3852
}
3953

40-
/*
41-
* @expectedException \InvalidArgumentException
54+
55+
/**
56+
* Asserts that $allLinks array has 10 elements.
57+
*
4258
*/
43-
public function testXpathCorrectString()
44-
{
45-
$grabber = new \Xparse\RecursivePagination\Test\TestGrabber();
59+
public function testGetNextPageCustomPath() {
60+
$grabber = new TestGrabber();
61+
$linksArrayPath = ["//span[@class='inner'][1]/a/@href"];
62+
63+
$paginator = new RecursivePagination($grabber, $linksArrayPath);
64+
$paginator->addToQueue('osmosis/page1.html');
65+
66+
$allLinks = [];
67+
while ($page = $paginator->getNextPage("//a[@class='pagenav']/@href")) {
68+
$adsList = $page->attribute("//h2/a/@href")->getItems();
69+
$allLinks = array_values(array_unique(array_merge($allLinks, $adsList)));
70+
}
71+
$this->assertTrue(count($allLinks) == 10);
72+
}
73+
74+
75+
/**
76+
* Asserts that InvalidArgumentException is thrown while passing a string to default xpath
77+
*
78+
*/
79+
public function testXpathCorrectString() {
80+
$grabber = new TestGrabber();
81+
$linksArrayPath = $grabber; // passing wrong path
82+
83+
$exception = null;
84+
try {
85+
new RecursivePagination($grabber, $linksArrayPath);
86+
} catch (InvalidArgumentException $e) {
87+
$this->assertTrue(true);
88+
return;
89+
}
90+
$this->fail('Unexpected exception type');
91+
}
92+
93+
94+
/**
95+
* Asserts that InvalidArgumentException is thrown while passing an array to default xpath
96+
*
97+
*/
98+
public function testXpathCorrectArray() {
99+
$grabber = new TestGrabber();
46100
$linksArrayPath = ["//span[@class='inner'][1]/a/@href", "//a[@class='pagenav']/@href", $grabber]; // passing wrong path
47101

48102
$exception = null;
49103
try {
50-
$paginator = new \Xparse\RecursivePagination\RecursivePagination($grabber, $linksArrayPath);
51-
} catch (\Exception $e) {
52-
$exception = $e;
104+
new RecursivePagination($grabber, $linksArrayPath);
105+
} catch (InvalidArgumentException $e) {
106+
$this->assertTrue(true);
107+
return;
108+
}
109+
$this->fail('Unexpected exception type');
110+
}
111+
112+
113+
/**
114+
* Asserts that InvalidArgumentException is thrown while passing array of links to queue
115+
*
116+
*/
117+
public function testAddToQueueLinksArray() {
118+
$grabber = new TestGrabber();
119+
$linksArrayPath = ["//span[@class='inner']/a/@href", "//a[@class='pagenav']/@href"];
120+
121+
$exception = null;
122+
try {
123+
$paginator = new RecursivePagination($grabber, $linksArrayPath);
124+
$paginator->addToQueue([
125+
'osmosis/page1.html',
126+
$grabber, //wrong link
127+
]);
128+
} catch (InvalidArgumentException $e) {
129+
$this->assertTrue(true);
130+
return;
53131
}
54-
$this->assertInstanceOf('InvalidArgumentException', $exception);
132+
$this->fail('Unexpected exception type');
55133
}
56134

57-
/*
58-
* @expectedException \InvalidArgumentException
135+
136+
/**
137+
* Asserts that InvalidArgumentException is thrown while passing one link to queue
138+
*
59139
*/
60-
public function testXpathCorrectArray()
61-
{
62-
$grabber = new \Xparse\RecursivePagination\Test\TestGrabber();
63-
$linksArrayPath = $grabber; // passing wrong path
140+
public function testAddToQueueLink() {
141+
$grabber = new TestGrabber();
142+
$linksArrayPath = ["//span[@class='inner']/a/@href", "//a[@class='pagenav']/@href"];
64143

65144
$exception = null;
66145
try {
67-
$paginator = new \Xparse\RecursivePagination\RecursivePagination($grabber, $linksArrayPath);
68-
} catch (\Exception $e) {
69-
$exception = $e;
146+
$paginator = new RecursivePagination($grabber, $linksArrayPath);
147+
$paginator->addToQueue($grabber); //wrong link
148+
} catch (InvalidArgumentException $e) {
149+
$this->assertTrue(true);
150+
return;
70151
}
71-
$this->assertInstanceOf('InvalidArgumentException', $exception);
152+
$this->fail('Unexpected exception type');
72153
}
73154
}

0 commit comments

Comments
 (0)