This commit is contained in:
James 2019-09-18 09:58:46 +01:00
commit 4c7995a935
7 changed files with 1879 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/vendor/
/report.xml

19
composer.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "jhodges/phpunit-base",
"type": "library",
"authors": [
{
"name": "James",
"email": "inbox.dev@jhodges.co.uk"
}
],
"require": {
"phpunit/phpunit-selenium": "^4.1",
"facebook/webdriver": "^1.7"
},
"autoload": {
"psr-4": {
"JHodges\\PHPUnitBase\\": "src/"
}
}
}

1715
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

23
phpunit.xml Normal file
View File

@ -0,0 +1,23 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
>
<logging>
<!--log type="coverage-html" target="report/report" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="report/coverage.xml"/>
<log type="coverage-php" target="report/coverage.serialized"/>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/-->
<log type="junit" target="report/logfile.xml"/>
<log type="testdox-html" target="report/testdox.html"/>
<log type="testdox-text" target="report/testdox.txt"/>
</logging>
<php>
<env name="SeleniumBrowserUrl" value="https://example.com"/>
<env name="SeleniumBrowser" value="firefox"/>
<env name="SeleniumWindowSize" value="1024,768"/>
<env name="SeleniumScreenshotPath" value="/tmp/"/>
</php>
</phpunit>

24
src/BrowserTest.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace JHodges\PHPUnitBase;
abstract class BrowserTest extends PHPUnit_Extensions_Selenium2TestCase{
public function setUp(){
$this->setHost('localhost');
$this->setPort(4444);
$this->setDesiredCapabilities(['moz:firefoxOptions'=>['args'=>['-headless','screenshot','window-size='.getenv('SeleniumWindowSize')]]]);
$this->setBrowserUrl(getenv('SeleniumBrowserUrl'));
$this->setBrowser(getenv('SeleniumBrowser'));
}
public function tearDown(){
if ($this->hasFailed()) {
if($path=getenv('SeleniumScreenshotPath')){
$filedata = $this->currentScreenshot();
file_put_contents($path.$this->getName().'.png', $filedata);
}
}
$this->stop();
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace JHodges\PHPUnitBase;
abstract class DomainRedirectTest extends UrlRedirectTest{
/**
* @return string the source domain
**/
abstract protected function getSourceDomain();
/**
* @return string the destination URL
**/
abstract protected function getDestinationURL();
/**
* Test source domain with HTTP:// redirects correctly to the destination URL
**/
public function testHttp(){
$this->assertUrlRedirect('http://'.$this->getSourceDomain().'',$this->getDestinationURL());
}
/**
* Test source domain with HTTPS:// redirects correctly to the destination URL
**/
public function testHttps(){
$this->assertUrlRedirect('https://'.$this->getSourceDomain().'',$this->getDestinationURL());
}
/**
* Test source domain with HTTP://WWW. redirects correctly to the destination URL
**/
public function testHttpWww(){
$this->assertUrlRedirect('http://www.'.$this->getSourceDomain().'',$this->getDestinationURL());
}
/**
* Test source domain with HTTPS://www. redirects correctly to the destination URL
**/
public function testHttpsWww(){
$this->assertUrlRedirect('https://www.'.$this->getSourceDomain().'',$this->getDestinationURL());
}
/**
* Test source domain with HTTP:// and a trailing slash redirects correctly to the destination URL
**/
public function testHttpSlash(){
$this->assertUrlRedirect('http://'.$this->getSourceDomain().'/',$this->getDestinationURL());
}
/**
* Test source domain with HTTPS:// and a trailing slash redirects correctly to the destination URL
**/
public function testHttpsSlash(){
$this->assertUrlRedirect('https://'.$this->getSourceDomain().'/',$this->getDestinationURL());
}
/**
* Test source domain with HTTP://www. and a trailing slash redirects correctly to the destination URL
**/
public function testHttpWwwSlash(){
$this->assertUrlRedirect('http://www.'.$this->getSourceDomain().'/',$this->getDestinationURL());
}
/**
* Test source domain with HTTPS://www. and a trailing slash redirects correctly to the destination URL
**/
public function testHttpsWwwSlash(){
$this->assertUrlRedirect('https://www.'.$this->getSourceDomain().'/',$this->getDestinationURL());
}
}

24
src/UrlRedirectTest.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace JHodges\PHPUnitBase;
use PHPUnit\Framework\TestCase;
abstract class UrlRedirectTest extends TestCase{
/**
* Test $from redirects to $to
* @param string $from source URL
* @param string $to dest URL
**/
public function assertUrlRedirect($from,$to){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $from);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$html = curl_exec($ch);
$redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
$this->assertEquals($to,$redirectedUrl,"From: {$from}");
}
}