From b9f9de56c9aa4e9fb3a5f77add7701086b5919ea Mon Sep 17 00:00:00 2001 From: James Date: Wed, 30 Sep 2020 14:48:11 +0100 Subject: [PATCH] allow testing on custom host/port --- tests/CrawlerTest.php | 172 ++++++++++++++++++++++-------------------- 1 file changed, 92 insertions(+), 80 deletions(-) diff --git a/tests/CrawlerTest.php b/tests/CrawlerTest.php index b31b37d..9ce086f 100644 --- a/tests/CrawlerTest.php +++ b/tests/CrawlerTest.php @@ -6,188 +6,200 @@ use GuzzleHttp\RequestOptions; class CrawlerTest extends TestCase{ + private $url='http://localhost:8080'; + + public function __construct(){ + parent::__construct(); + if(getenv('URL')){ + $this->url=getenv('URL'); + } + echo $this->url; + + } + public function testFullSite(){ + echo $this->url; $crawler=new Crawler([RequestOptions::CONNECT_TIMEOUT => 3, RequestOptions::TIMEOUT => 3]); - $crawler->crawl('http://localhost:8080/'); + $crawler->crawl($this->url); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ 'http://example.com/' => ['code' => 200], - 'http://localhost:8080/deeplink1' => ['code' => 200], - 'http://localhost:8080/deeplink2' => ['code' => 200], - 'http://localhost:8080/deeplink3' => ['code' => 200], - 'http://localhost:8080/externalLink' => ['code' => 200], - 'http://localhost:8080/found' => ['code' => 200], - 'http://localhost:8080/interlinked1' => ['code' => 200], - 'http://localhost:8080/interlinked2' => ['code' => 200], - 'http://localhost:8080/interlinked3' => ['code' => 200], - 'http://localhost:8080/internalServerError' => ['code' => 500], - 'http://localhost:8080/invalidStatusCode' => ['code' => '---'], - 'http://localhost:8080/notFound' => ['code' => 404], - 'http://localhost:8080/redirect1' => ['code' => 302], - 'http://localhost:8080/redirect2' => ['code' => 302], - 'http://localhost:8080/redirectLoop' => ['code' => '---'], - 'http://localhost:8080/redirectToFound' => ['code' => 302 ], - 'http://localhost:8080/redirectToNotFound' => ['code' => 302 ], - 'http://localhost:8080/redirectToRedirectToNotFound' => ['code' => 302], - 'http://localhost:8080/timeout' => ['code' => '---'], - 'http://localhost:8080/twoRedirectsToSameLocation' => ['code' => 200], + $this->url.'/deeplink1' => ['code' => 200], + $this->url.'/deeplink2' => ['code' => 200], + $this->url.'/deeplink3' => ['code' => 200], + $this->url.'/externalLink' => ['code' => 200], + $this->url.'/found' => ['code' => 200], + $this->url.'/interlinked1' => ['code' => 200], + $this->url.'/interlinked2' => ['code' => 200], + $this->url.'/interlinked3' => ['code' => 200], + $this->url.'/internalServerError' => ['code' => 500], + $this->url.'/invalidStatusCode' => ['code' => '---'], + $this->url.'/notFound' => ['code' => 404], + $this->url.'/redirect1' => ['code' => 302], + $this->url.'/redirect2' => ['code' => 302], + $this->url.'/redirectLoop' => ['code' => '---'], + $this->url.'/redirectToFound' => ['code' => 302 ], + $this->url.'/redirectToNotFound' => ['code' => 302 ], + $this->url.'/redirectToRedirectToNotFound' => ['code' => 302], + $this->url.'/timeout' => ['code' => '---'], + $this->url.'/twoRedirectsToSameLocation' => ['code' => 200], ], print_r($sitemap,true)); } public function testFound(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/found'); + $crawler->crawl($this->url.'/found'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/found' => ['code' => 200], + $this->url.'/found' => ['code' => 200], ], print_r($sitemap,true)); } public function testNotFound(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/notFound'); + $crawler->crawl($this->url.'/notFound'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/notFound' => ['code' => 404], + $this->url.'/notFound' => ['code' => 404], ], print_r($sitemap,true)); } public function testExternalLink(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/externalLink'); + $crawler->crawl($this->url.'/externalLink'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/externalLink' => ['code' => 200], + $this->url.'/externalLink' => ['code' => 200], 'http://example.com/' => ['code' => 200], ], print_r($sitemap,true)); } public function testDeeplink(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/deeplink1'); + $crawler->crawl($this->url.'/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], + $this->url.'/deeplink1' => ['code' => 200], + $this->url.'/deeplink2' => ['code' => 200], + $this->url.'/deeplink3' => ['code' => 200], ], print_r($sitemap,true)); } public function testInterlinked(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/interlinked1'); - $crawler->crawl('http://localhost:8080/interlinked4'); //this ensures the order or results for the URL tracking test PART2 + $crawler->crawl($this->url.'/interlinked1'); + $crawler->crawl($this->url.'/interlinked4'); //this ensures the order or results for the URL tracking test PART2 $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/interlinked4' => 1, + $this->url.'/interlinked1' => ['code' => 200 , 'foundOn' => [ + $this->url.'/interlinked1' => 1, + $this->url.'/interlinked2' => 1, + $this->url.'/interlinked3' => 1, + $this->url.'/interlinked4' => 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/interlinked4' => 1, + $this->url.'/interlinked2' => ['code' => 200 , 'foundOn' => [ + $this->url.'/interlinked1' => 1, + $this->url.'/interlinked2' => 1, + $this->url.'/interlinked3' => 1, + $this->url.'/interlinked4' => 1, ]], - 'http://localhost:8080/interlinked3' => ['code' => 200 , 'foundOn' => [ - 'http://localhost:8080/interlinked1' => 1, - 'http://localhost:8080/interlinked2' => 1, - 'http://localhost:8080/interlinked3' => 1, - 'http://localhost:8080/interlinked4' => 1, + $this->url.'/interlinked3' => ['code' => 200 , 'foundOn' => [ + $this->url.'/interlinked1' => 1, + $this->url.'/interlinked2' => 1, + $this->url.'/interlinked3' => 1, + $this->url.'/interlinked4' => 1, ]], - 'http://localhost:8080/found' => ['code' => 200 , 'foundOn' => [ - 'http://localhost:8080/interlinked1' => 1, - 'http://localhost:8080/interlinked2' => 1, - 'http://localhost:8080/interlinked3' => 1, - 'http://localhost:8080/interlinked4' => 1, + $this->url.'/found' => ['code' => 200 , 'foundOn' => [ + $this->url.'/interlinked1' => 1, + $this->url.'/interlinked2' => 1, + $this->url.'/interlinked3' => 1, + $this->url.'/interlinked4' => 1, ]], - 'http://localhost:8080/redirectToFound' => ['code' => 302 , 'foundOn' => [ - 'http://localhost:8080/interlinked1' => 1, - 'http://localhost:8080/interlinked2' => 1, - 'http://localhost:8080/interlinked3' => 1, - 'http://localhost:8080/interlinked4' => 1, + $this->url.'/redirectToFound' => ['code' => 302 , 'foundOn' => [ + $this->url.'/interlinked1' => 1, + $this->url.'/interlinked2' => 1, + $this->url.'/interlinked3' => 1, + $this->url.'/interlinked4' => 1, ]], ], print_r($sitemap,true)); } public function testRedirectToFound(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/redirectToFound'); + $crawler->crawl($this->url.'/redirectToFound'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/redirectToFound' => ['code' => 302], - 'http://localhost:8080/found' => ['code' => 200 ], + $this->url.'/redirectToFound' => ['code' => 302], + $this->url.'/found' => ['code' => 200 ], ], print_r($sitemap,true)); } public function testRedirectToNotFound(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/redirectToNotFound'); + $crawler->crawl($this->url.'/redirectToNotFound'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/redirectToNotFound' => ['code' => 302], - 'http://localhost:8080/notFound' => ['code' => 404 ], + $this->url.'/redirectToNotFound' => ['code' => 302], + $this->url.'/notFound' => ['code' => 404 ], ], print_r($sitemap,true)); } public function testRedirectToRedirectToNotFound(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/redirectToRedirectToNotFound'); + $crawler->crawl($this->url.'/redirectToRedirectToNotFound'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/redirectToRedirectToNotFound' => ['code' => 302], - 'http://localhost:8080/redirectToNotFound' => ['code' => 302], - 'http://localhost:8080/notFound' => ['code' => 404], + $this->url.'/redirectToRedirectToNotFound' => ['code' => 302], + $this->url.'/redirectToNotFound' => ['code' => 302], + $this->url.'/notFound' => ['code' => 404], ], print_r($sitemap,true)); } public function testTwoRedirectsToSameLocation(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/twoRedirectsToSameLocation'); + $crawler->crawl($this->url.'/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/found' => ['code' => 200], + $this->url.'/twoRedirectsToSameLocation' => ['code' => 200], + $this->url.'/redirect1' => ['code' => 302], + $this->url.'/redirect2' => ['code' => 302], + $this->url.'/found' => ['code' => 200], ], print_r($sitemap,true)); } public function testTimeout(){ $crawler=new Crawler([RequestOptions::CONNECT_TIMEOUT => 3, RequestOptions::TIMEOUT => 3]); - $crawler->crawl('http://localhost:8080/timeout'); + $crawler->crawl($this->url.'/timeout'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/timeout' => ['code' => '---'], + $this->url.'/timeout' => ['code' => '---'], ], print_r($sitemap,true)); } public function testRedirectLoop(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/redirectLoop'); + $crawler->crawl($this->url.'/redirectLoop'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/redirectLoop' => ['code' => '---'], + $this->url.'/redirectLoop' => ['code' => '---'], ], print_r($sitemap,true)); } public function testInternalServerError(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/internalServerError'); + $crawler->crawl($this->url.'/internalServerError'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/internalServerError' => ['code' => 500], + $this->url.'/internalServerError' => ['code' => 500], ], print_r($sitemap,true)); } public function testInvalidStatusCode(){ $crawler=new Crawler(); - $crawler->crawl('http://localhost:8080/invalidStatusCode'); + $crawler->crawl($this->url.'/invalidStatusCode'); $sitemap=$crawler->getResults(); $this->assertTreeContains($sitemap,[ - 'http://localhost:8080/invalidStatusCode' => ['code' => '---'], + $this->url.'/invalidStatusCode' => ['code' => '---'], ], print_r($sitemap,true)); }