allow testing on custom host/port

This commit is contained in:
James 2020-09-30 14:48:11 +01:00
parent dbe1481e5c
commit b9f9de56c9

View File

@ -6,188 +6,200 @@ use GuzzleHttp\RequestOptions;
class CrawlerTest extends TestCase{ 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(){ public function testFullSite(){
echo $this->url;
$crawler=new Crawler([RequestOptions::CONNECT_TIMEOUT => 3, RequestOptions::TIMEOUT => 3]); $crawler=new Crawler([RequestOptions::CONNECT_TIMEOUT => 3, RequestOptions::TIMEOUT => 3]);
$crawler->crawl('http://localhost:8080/'); $crawler->crawl($this->url);
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://example.com/' => ['code' => 200], 'http://example.com/' => ['code' => 200],
'http://localhost:8080/deeplink1' => ['code' => 200], $this->url.'/deeplink1' => ['code' => 200],
'http://localhost:8080/deeplink2' => ['code' => 200], $this->url.'/deeplink2' => ['code' => 200],
'http://localhost:8080/deeplink3' => ['code' => 200], $this->url.'/deeplink3' => ['code' => 200],
'http://localhost:8080/externalLink' => ['code' => 200], $this->url.'/externalLink' => ['code' => 200],
'http://localhost:8080/found' => ['code' => 200], $this->url.'/found' => ['code' => 200],
'http://localhost:8080/interlinked1' => ['code' => 200], $this->url.'/interlinked1' => ['code' => 200],
'http://localhost:8080/interlinked2' => ['code' => 200], $this->url.'/interlinked2' => ['code' => 200],
'http://localhost:8080/interlinked3' => ['code' => 200], $this->url.'/interlinked3' => ['code' => 200],
'http://localhost:8080/internalServerError' => ['code' => 500], $this->url.'/internalServerError' => ['code' => 500],
'http://localhost:8080/invalidStatusCode' => ['code' => '---'], $this->url.'/invalidStatusCode' => ['code' => '---'],
'http://localhost:8080/notFound' => ['code' => 404], $this->url.'/notFound' => ['code' => 404],
'http://localhost:8080/redirect1' => ['code' => 302], $this->url.'/redirect1' => ['code' => 302],
'http://localhost:8080/redirect2' => ['code' => 302], $this->url.'/redirect2' => ['code' => 302],
'http://localhost:8080/redirectLoop' => ['code' => '---'], $this->url.'/redirectLoop' => ['code' => '---'],
'http://localhost:8080/redirectToFound' => ['code' => 302 ], $this->url.'/redirectToFound' => ['code' => 302 ],
'http://localhost:8080/redirectToNotFound' => ['code' => 302 ], $this->url.'/redirectToNotFound' => ['code' => 302 ],
'http://localhost:8080/redirectToRedirectToNotFound' => ['code' => 302], $this->url.'/redirectToRedirectToNotFound' => ['code' => 302],
'http://localhost:8080/timeout' => ['code' => '---'], $this->url.'/timeout' => ['code' => '---'],
'http://localhost:8080/twoRedirectsToSameLocation' => ['code' => 200], $this->url.'/twoRedirectsToSameLocation' => ['code' => 200],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testFound(){ public function testFound(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/found'); $crawler->crawl($this->url.'/found');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/found' => ['code' => 200], $this->url.'/found' => ['code' => 200],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testNotFound(){ public function testNotFound(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/notFound'); $crawler->crawl($this->url.'/notFound');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/notFound' => ['code' => 404], $this->url.'/notFound' => ['code' => 404],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testExternalLink(){ public function testExternalLink(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/externalLink'); $crawler->crawl($this->url.'/externalLink');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/externalLink' => ['code' => 200], $this->url.'/externalLink' => ['code' => 200],
'http://example.com/' => ['code' => 200], 'http://example.com/' => ['code' => 200],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testDeeplink(){ public function testDeeplink(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/deeplink1'); $crawler->crawl($this->url.'/deeplink1');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/deeplink1' => ['code' => 200], $this->url.'/deeplink1' => ['code' => 200],
'http://localhost:8080/deeplink2' => ['code' => 200], $this->url.'/deeplink2' => ['code' => 200],
'http://localhost:8080/deeplink3' => ['code' => 200], $this->url.'/deeplink3' => ['code' => 200],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testInterlinked(){ public function testInterlinked(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/interlinked1'); $crawler->crawl($this->url.'/interlinked1');
$crawler->crawl('http://localhost:8080/interlinked4'); //this ensures the order or results for the URL tracking test PART2 $crawler->crawl($this->url.'/interlinked4'); //this ensures the order or results for the URL tracking test PART2
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/interlinked1' => ['code' => 200 , 'foundOn' => [ $this->url.'/interlinked1' => ['code' => 200 , 'foundOn' => [
'http://localhost:8080/interlinked1' => 1, $this->url.'/interlinked1' => 1,
'http://localhost:8080/interlinked2' => 1, $this->url.'/interlinked2' => 1,
'http://localhost:8080/interlinked3' => 1, $this->url.'/interlinked3' => 1,
'http://localhost:8080/interlinked4' => 1, $this->url.'/interlinked4' => 1,
]], ]],
'http://localhost:8080/interlinked2' => ['code' => 200 , 'foundOn' => [ $this->url.'/interlinked2' => ['code' => 200 , 'foundOn' => [
'http://localhost:8080/interlinked1' => 1, $this->url.'/interlinked1' => 1,
'http://localhost:8080/interlinked2' => 1, $this->url.'/interlinked2' => 1,
'http://localhost:8080/interlinked3' => 1, $this->url.'/interlinked3' => 1,
'http://localhost:8080/interlinked4' => 1, $this->url.'/interlinked4' => 1,
]], ]],
'http://localhost:8080/interlinked3' => ['code' => 200 , 'foundOn' => [ $this->url.'/interlinked3' => ['code' => 200 , 'foundOn' => [
'http://localhost:8080/interlinked1' => 1, $this->url.'/interlinked1' => 1,
'http://localhost:8080/interlinked2' => 1, $this->url.'/interlinked2' => 1,
'http://localhost:8080/interlinked3' => 1, $this->url.'/interlinked3' => 1,
'http://localhost:8080/interlinked4' => 1, $this->url.'/interlinked4' => 1,
]], ]],
'http://localhost:8080/found' => ['code' => 200 , 'foundOn' => [ $this->url.'/found' => ['code' => 200 , 'foundOn' => [
'http://localhost:8080/interlinked1' => 1, $this->url.'/interlinked1' => 1,
'http://localhost:8080/interlinked2' => 1, $this->url.'/interlinked2' => 1,
'http://localhost:8080/interlinked3' => 1, $this->url.'/interlinked3' => 1,
'http://localhost:8080/interlinked4' => 1, $this->url.'/interlinked4' => 1,
]], ]],
'http://localhost:8080/redirectToFound' => ['code' => 302 , 'foundOn' => [ $this->url.'/redirectToFound' => ['code' => 302 , 'foundOn' => [
'http://localhost:8080/interlinked1' => 1, $this->url.'/interlinked1' => 1,
'http://localhost:8080/interlinked2' => 1, $this->url.'/interlinked2' => 1,
'http://localhost:8080/interlinked3' => 1, $this->url.'/interlinked3' => 1,
'http://localhost:8080/interlinked4' => 1, $this->url.'/interlinked4' => 1,
]], ]],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testRedirectToFound(){ public function testRedirectToFound(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/redirectToFound'); $crawler->crawl($this->url.'/redirectToFound');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/redirectToFound' => ['code' => 302], $this->url.'/redirectToFound' => ['code' => 302],
'http://localhost:8080/found' => ['code' => 200 ], $this->url.'/found' => ['code' => 200 ],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testRedirectToNotFound(){ public function testRedirectToNotFound(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/redirectToNotFound'); $crawler->crawl($this->url.'/redirectToNotFound');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/redirectToNotFound' => ['code' => 302], $this->url.'/redirectToNotFound' => ['code' => 302],
'http://localhost:8080/notFound' => ['code' => 404 ], $this->url.'/notFound' => ['code' => 404 ],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testRedirectToRedirectToNotFound(){ public function testRedirectToRedirectToNotFound(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/redirectToRedirectToNotFound'); $crawler->crawl($this->url.'/redirectToRedirectToNotFound');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/redirectToRedirectToNotFound' => ['code' => 302], $this->url.'/redirectToRedirectToNotFound' => ['code' => 302],
'http://localhost:8080/redirectToNotFound' => ['code' => 302], $this->url.'/redirectToNotFound' => ['code' => 302],
'http://localhost:8080/notFound' => ['code' => 404], $this->url.'/notFound' => ['code' => 404],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testTwoRedirectsToSameLocation(){ public function testTwoRedirectsToSameLocation(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/twoRedirectsToSameLocation'); $crawler->crawl($this->url.'/twoRedirectsToSameLocation');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/twoRedirectsToSameLocation' => ['code' => 200], $this->url.'/twoRedirectsToSameLocation' => ['code' => 200],
'http://localhost:8080/redirect1' => ['code' => 302], $this->url.'/redirect1' => ['code' => 302],
'http://localhost:8080/redirect2' => ['code' => 302], $this->url.'/redirect2' => ['code' => 302],
'http://localhost:8080/found' => ['code' => 200], $this->url.'/found' => ['code' => 200],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testTimeout(){ public function testTimeout(){
$crawler=new Crawler([RequestOptions::CONNECT_TIMEOUT => 3, RequestOptions::TIMEOUT => 3]); $crawler=new Crawler([RequestOptions::CONNECT_TIMEOUT => 3, RequestOptions::TIMEOUT => 3]);
$crawler->crawl('http://localhost:8080/timeout'); $crawler->crawl($this->url.'/timeout');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/timeout' => ['code' => '---'], $this->url.'/timeout' => ['code' => '---'],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testRedirectLoop(){ public function testRedirectLoop(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/redirectLoop'); $crawler->crawl($this->url.'/redirectLoop');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/redirectLoop' => ['code' => '---'], $this->url.'/redirectLoop' => ['code' => '---'],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testInternalServerError(){ public function testInternalServerError(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/internalServerError'); $crawler->crawl($this->url.'/internalServerError');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/internalServerError' => ['code' => 500], $this->url.'/internalServerError' => ['code' => 500],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }
public function testInvalidStatusCode(){ public function testInvalidStatusCode(){
$crawler=new Crawler(); $crawler=new Crawler();
$crawler->crawl('http://localhost:8080/invalidStatusCode'); $crawler->crawl($this->url.'/invalidStatusCode');
$sitemap=$crawler->getResults(); $sitemap=$crawler->getResults();
$this->assertTreeContains($sitemap,[ $this->assertTreeContains($sitemap,[
'http://localhost:8080/invalidStatusCode' => ['code' => '---'], $this->url.'/invalidStatusCode' => ['code' => '---'],
], print_r($sitemap,true)); ], print_r($sitemap,true));
} }