1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-07-31 05:30:09 +02:00

Adding unittest with phpunit #84, fix #13

This commit is contained in:
Mikael Roos
2015-03-04 11:32:11 +01:00
parent f37189c378
commit 2c03cde379
6 changed files with 322 additions and 2 deletions

View File

@@ -0,0 +1,165 @@
<?php
/**
* A testclass
*
*/
class CImage_RemoteDownloadTest extends \PHPUnit_Framework_TestCase
{
/*
* remote_whitelist
*/
private $remote_whitelist = [
'\.facebook\.com$',
'^(?:images|photos-[a-z])\.ak\.instagram\.com$',
'\.google\.com$',
];
/**
* Provider for valid remote sources.
*
* @return array
*/
public function providerValidRemoteSource()
{
return [
[
"http://dbwebb.se/img.jpg",
"https://dbwebb.se/img.jpg",
],
];
}
/**
* Provider for invalid remote sources.
*
* @return array
*/
public function providerInvalidRemoteSource()
{
return [
[
"ftp://dbwebb.se/img.jpg",
"dbwebb.se/img.jpg",
"img.jpg",
],
];
}
/**
* Test
*
* @return void
*
* @dataProvider providerValidRemoteSource
*/
public function testAllowRemoteDownloadDefaultPatternValid($source)
{
$img = new CImage();
$img->setRemoteDownload(true);
$res = $img->isRemoteSource($source);
$this->assertTrue($res, "Should be a valid remote source: '$source'.");
}
/**
* Test
*
* @return void
*
* @dataProvider providerInvalidRemoteSource
*/
public function testAllowRemoteDownloadDefaultPatternInvalid($source)
{
$img = new CImage();
$img->setRemoteDownload(true);
$res = $img->isRemoteSource($source);
$this->assertFalse($res, "Should not be a valid remote source: '$source'.");
}
/**
* Provider for hostname matching the whitelist.
*
* @return array
*/
public function providerHostnameMatch()
{
return [
[
"any.facebook.com",
"images.ak.instagram.com",
"google.com",
],
];
}
/**
* Test
*
* @param string $hostname matches the whitelist
*
* @return void
*
* @dataProvider providerHostnameMatch
*
*/
public function testRemoteHostWhitelistMatch($hostname)
{
$img = new CImage();
$img->setRemoteHostWhitelist($this->remote_whitelist);
$res = $img->isRemoteSourceOnWhitelist("http://$hostname/img.jpg");
$this->assertTrue($res, "Should be a valid hostname on the whitelist: '$hostname'.");
}
/**
* Provider for hostname not matching the whitelist.
*
* @return array
*/
public function providerHostnameNoMatch()
{
return [
[
"example.com",
".com",
"img.jpg",
],
];
}
/**
* Test
*
* @param string $hostname not matching the whitelist
*
* @return void
*
* @dataProvider providerHostnameNoMatch
*
*/
public function testRemoteHostWhitelistNoMatch($hostname)
{
$img = new CImage();
$img->setRemoteHostWhitelist($this->remote_whitelist);
$res = $img->isRemoteSourceOnWhitelist("http://$hostname/img.jpg");
$this->assertFalse($res, "Should not be a valid hostname on the whitelist: '$hostname'.");
}
}

128
test/CWhitelistTest.php Normal file
View File

@@ -0,0 +1,128 @@
<?php
/**
* A testclass
*
*/
class CWhitelistTest extends \PHPUnit_Framework_TestCase
{
/*
* remote_whitelist
*/
private $remote_whitelist = [
'\.facebook\.com$',
'^(?:images|photos-[a-z])\.ak\.instagram\.com$',
'\.google\.com$',
];
/**
* Provider for hostname matching the whitelist.
*
* @return array
*/
public function providerHostnameMatch()
{
return [
[
"any.facebook.com",
"images.ak.instagram.com",
"google.com",
],
];
}
/**
* Provider for hostname not matching the whitelist.
*
* @return array
*/
public function providerHostnameNoMatch()
{
return [
[
"example.com",
".com",
"img.jpg",
],
];
}
/**
* Test
*
* @param string $hostname matches the whitelist
*
* @return void
*
* @dataProvider providerHostnameMatch
*
*/
public function testRemoteHostWhitelistMatch($hostname)
{
$whitelist = new CWhitelist();
$whitelist->set($this->remote_whitelist);
$res = $whitelist->check($hostname);
$this->assertTrue($res, "Should be a valid hostname on the whitelist: '$hostname'.");
}
/**
* Test
*
* @param string $hostname not matching the whitelist
*
* @return void
*
* @dataProvider providerHostnameNoMatch
*
*/
public function testRemoteHostWhitelistNoMatch($hostname)
{
$whitelist = new CWhitelist();
$whitelist->set($this->remote_whitelist);
$res = $whitelist->check($hostname);
$this->assertFalse($res, "Should not be a valid hostname on the whitelist: '$hostname'.");
}
/**
* Test
*
* @expectedException Exception
*
* @return void
*
*/
public function testInvalidFormat()
{
$whitelist = new CWhitelist();
$whitelist->set("should fail");
}
/**
* Test
*
* @return void
*
*/
public function testCheckEmpty()
{
$whitelist = new CWhitelist();
$whitelist->set($this->remote_whitelist);
$hostname = "";
$res = $whitelist->check($hostname);
$this->assertFalse($res, "Should not be a valid hostname on the whitelist: '$hostname'.");
}
}

6
test/config.php Normal file
View File

@@ -0,0 +1,6 @@
<?php
/**
* Get all configuration details to be able to execute the test suite.
*
*/
require __DIR__ . "/../autoload.php";