1
0
mirror of https://github.com/mosbth/cimage.git synced 2025-07-31 21:40:12 +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

6
.gitignore vendored
View File

@@ -1,4 +1,6 @@
# Cache files #
######################
# Cache files
cache/*
# Test
coverage/
coverage.clover

View File

@@ -5,7 +5,10 @@ Revision history
v0.7.0.x (latest)
-------------------------------------
*
* Adding phpdoc, fix #48.
* Adding travis, fix #15.
* Adding scrutinizer, fix #57.
v0.7.0 (2015-02-10)

16
phpunit.xml Normal file
View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
bootstrap="test/config.php">
<testsuites>
<testsuite name="all">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="coverage" charset="UTF-8" highlight="true" lowUpperBound="35" highLowerBound="70" />
<log type="coverage-clover" target="coverage.clover" />
</logging>
</phpunit>

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";