From 9c0961fd87580e2b3938bfdfbd7faad93056c218 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 23 Feb 2020 01:08:14 +0000 Subject: [PATCH] fix tests --- src/CrawlObserver.php | 2 +- tests/CrawlerTest.php | 147 +++++++++++++++++++++--------------------- 2 files changed, 75 insertions(+), 74 deletions(-) diff --git a/src/CrawlObserver.php b/src/CrawlObserver.php index cb7831e..75f0c55 100644 --- a/src/CrawlObserver.php +++ b/src/CrawlObserver.php @@ -124,7 +124,7 @@ class CrawlObserver extends \Spatie\Crawler\CrawlObserver if( $response=$requestException->getResponse() ){ $this->crawled($url,$response,$foundOnUrl); }else{ - $this->addResult((String)$url,(string)$foundOnUrl); + $this->addResult((String)$url,(string)$foundOnUrl,'---',''); } } diff --git a/tests/CrawlerTest.php b/tests/CrawlerTest.php index 06d61b4..bf894ef 100644 --- a/tests/CrawlerTest.php +++ b/tests/CrawlerTest.php @@ -5,123 +5,124 @@ use JHodges\Sitemap\Crawler; class CrawlerTest extends TestCase{ - public function testCanCrawlSite(){ - $crawler=new Crawler('http://localhost:8080'); - $crawler->crawl('http://localhost:8080'); + public function testFound(){ + $crawler=new Crawler(); + $crawler->crawl('http://localhost:8080/found'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/' => ['code' => 200 ], - 'http://localhost:8080/link1' => ['code' => 200 ], - 'http://localhost:8080/link2' => ['code' => 200 ], - 'http://localhost:8080/link3' => ['code' => 200 ], - 'http://localhost:8080/link4' => ['code' => 302 ], - 'http://localhost:8080/notExists' => ['code' => 404 ], + 'http://localhost:8080/found' => ['code' => 200], + ], print_r($sitemap,true)); + } + + public function testNotFound(){ + $crawler=new Crawler(); + $crawler->crawl('http://localhost:8080/notFound'); + $sitemap=$crawler->getResults(); + $this->assertTreeContains($sitemap,[ + 'http://localhost:8080/notFound' => ['code' => 404], + ], print_r($sitemap,true)); + } + + public function testExternalLink(){ + $crawler=new Crawler(); + $crawler->crawl('http://localhost:8080/externalLink'); + $sitemap=$crawler->getResults(); + $this->assertTreeContains($sitemap,[ + 'http://localhost:8080/externalLink' => ['code' => 200], + 'http://example.com/' => ['code' => 200], + ], print_r($sitemap,true)); + } + + public function testDeeplink(){ + $crawler=new Crawler(); + $crawler->crawl('http://localhost:8080/deeplink1'); + $sitemap=$crawler->getResults(); + $this->assertTreeContains($sitemap,[ + 'http://localhost:8080/deeplink1' => ['code' => 200], + 'http://localhost:8080/deeplink2' => ['code' => 200], + 'http://localhost:8080/deeplink3' => ['code' => 200], + ], print_r($sitemap,true)); + } + + + public function testInterlinked(){ + $crawler=new Crawler(); + $crawler->crawl('http://localhost:8080/interlinked1'); + //$crawler->crawl('http://localhost:8080/page4'); // TODO!!! this ensures the order or results for the URL tracking test 3PARTS. + $sitemap=$crawler->getResults(); + $this->assertTreeContains($sitemap,[ + 'http://localhost:8080/interlinked1' => ['code' => 200 , 'foundOn' => [ + 'http://localhost:8080/interlinked1' => 1, + 'http://localhost:8080/interlinked2' => 1, + 'http://localhost:8080/interlinked3' => 1, + ]], + 'http://localhost:8080/interlinked2' => ['code' => 200 , 'foundOn' => [ + 'http://localhost:8080/interlinked1' => 1, + 'http://localhost:8080/interlinked2' => 1, + 'http://localhost:8080/interlinked3' => 1, + ]], + 'http://localhost:8080/interlinked3' => ['code' => 200 , 'foundOn' => [ + 'http://localhost:8080/interlinked1' => 1, + 'http://localhost:8080/interlinked2' => 1, + 'http://localhost:8080/interlinked3' => 1, + ]], ]); } - public function testCollectsAllFoundOnUrls(){ - $crawler=new Crawler('http://localhost:8080'); - $crawler->crawl('http://localhost:8080/page1'); - $crawler->crawl('http://localhost:8080/page4'); // this ensures the order or results for the URL tracking test 3PARTS. - $sitemap=$crawler->getResults(); - print_r($sitemap); - $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/page1' => ['code' => 200 , 'foundOn' => [ - 'http://localhost:8080/page1' => 1, - 'http://localhost:8080/page2' => 1, - 'http://localhost:8080/page3' => 1, - ]], - 'http://localhost:8080/page2' => ['code' => 200 , 'foundOn' => [ - 'http://localhost:8080/page1' => 1, - 'http://localhost:8080/page2' => 1, - 'http://localhost:8080/page3' => 1, - ]], - 'http://localhost:8080/page3' => ['code' => 200 , 'foundOn' => [ - 'http://localhost:8080/page1' => 1, - 'http://localhost:8080/page2' => 1, - 'http://localhost:8080/page3' => 1, - ]], - 'http://localhost:8080/notFound1' => ['code' => 404 , 'foundOn' => [ - 'http://localhost:8080/page1' => 1, - 'http://localhost:8080/page2' => 1, - 'http://localhost:8080/page3' => 1, - ]], - 'http://localhost:8080/notFound2' => ['code' => 404 , 'foundOn' => [ - 'http://localhost:8080/page1' => 1, - 'http://localhost:8080/page2' => 1, - 'http://localhost:8080/page3' => 1, - 'http://localhost:8080/page4' => 1, - ]], - 'http://localhost:8080/redirectToNotFound' => ['code' => 302 , 'foundOn' => [ - 'http://localhost:8080/page1' => 1, - 'http://localhost:8080/page2' => 1, - 'http://localhost:8080/page3' => 1, - 'http://localhost:8080/page4' => 1, - ]], - 'http://localhost:8080/redirectToRedirectToNotFound' => ['code' => 302 , 'foundOn' => [ - 'http://localhost:8080/page1' => 1, - 'http://localhost:8080/page2' => 1, - 'http://localhost:8080/page3' => 1, - 'http://localhost:8080/page4' => 1, - ]], - - ]); - } - - public function testCanFollowRedirectToFound(){ - $crawler=new Crawler('http://localhost:8080'); + public function testRedirectToFound(){ + $crawler=new Crawler(); $crawler->crawl('http://localhost:8080/redirectToFound'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ 'http://localhost:8080/redirectToFound' => ['code' => 302 ], - 'http://localhost:8080/' => ['code' => 200 ], + 'http://localhost:8080/found' => ['code' => 200 ], ]); } - public function testCanFollowRedirectToNotFound(){ - $crawler=new Crawler('http://localhost:8080'); + public function testRedirectToNotFound(){ + $crawler=new Crawler(); $crawler->crawl('http://localhost:8080/redirectToNotFound'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ 'http://localhost:8080/redirectToNotFound' => ['code' => 302 ], - 'http://localhost:8080/notFound2' => ['code' => 404 ], + 'http://localhost:8080/notFound' => ['code' => 404 ], ]); } - public function testCanFollowRedirectToRedirectToNotFound(){ - $crawler=new Crawler('http://localhost:8080'); + public function testRedirectToRedirectToNotFound(){ + $crawler=new Crawler(); $crawler->crawl('http://localhost:8080/redirectToRedirectToNotFound'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ 'http://localhost:8080/redirectToRedirectToNotFound' => ['code' => 302 ], 'http://localhost:8080/redirectToNotFound' => ['code' => 302 ], - 'http://localhost:8080/notFound2' => ['code' => 404 ], + 'http://localhost:8080/notFound' => ['code' => 404 ], ]); } - public function testCanFollowTwoRedirectsToSameLocation(){ - $crawler=new Crawler('http://localhost:8080'); + public function testTwoRedirectsToSameLocation(){ + $crawler=new Crawler(); $crawler->crawl('http://localhost:8080/twoRedirectsToSameLocation'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ 'http://localhost:8080/twoRedirectsToSameLocation' => ['code' => 200 ], 'http://localhost:8080/redirect1' => ['code' => 302 ], 'http://localhost:8080/redirect2' => ['code' => 302 ], - 'http://localhost:8080/link1' => ['code' => 200 ], + 'http://localhost:8080/found' => ['code' => 200 ], ]); } public function testTimeout(){ - $crawler=new Crawler('http://localhost:8080'); + $crawler=new Crawler(); $crawler->crawl('http://localhost:8080/timeout'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/timeout' => ['code' => '' ], + 'http://localhost:8080/timeout' => ['code' => '---' ], ]); } public function testInternalServerError(){ - $crawler=new Crawler('http://localhost:8080'); + $crawler=new Crawler(); $crawler->crawl('http://localhost:8080/internalServerError'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[