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