introduced property based unit tests using the eris library, found an unhandled error case (empty string) in the Vizhash library and handled it

This commit is contained in:
El RIDO 2017-02-26 11:21:52 +01:00
parent db307c3a77
commit 59adfc1962
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
4 changed files with 38 additions and 4 deletions

View File

@ -24,7 +24,8 @@
},
"require-dev": {
"codacy/coverage": "dev-master",
"codeclimate/php-test-reporter": "dev-master"
"codeclimate/php-test-reporter": "dev-master",
"giorgiosironi/eris": "dev-master"
},
"autoload": {
"psr-4": {

View File

@ -80,7 +80,7 @@ class Vizhash16x16
*/
public function generate($text)
{
if (!function_exists('gd_info')) {
if (!function_exists('gd_info') || strlen($text) < 1) {
return '';
}

View File

@ -6,11 +6,12 @@ and its dependencies:
* phpunit
* php-gd
* php-sqlite3
* php-xdebug (for code coverage reports)
* php-curl (optional, for codeclimate test reporter)
* php-xdebug (optional, for code coverage reports)
Example for Debian and Ubuntu:
```console
$ sudo apt install phpunit php-gd php-sqlite php-xdebug
$ sudo apt install phpunit php-gd php-sqlite php-curl php-xdebug
```
To run the tests, just change into this directory and run phpunit:

View File

@ -2,9 +2,12 @@
use PrivateBin\Persistence\ServerSalt;
use PrivateBin\Vizhash16x16;
use Eris\Generator;
class Vizhash16x16Test extends PHPUnit_Framework_TestCase
{
use Eris\TestTrait;
private $_file;
private $_path;
@ -27,6 +30,35 @@ class Vizhash16x16Test extends PHPUnit_Framework_TestCase
Helper::rmDir($this->_path);
}
public function testVizhashGeneratesPngs()
{
$this->forAll(
Generator\string(),
Generator\string()
)->then(
function ($string1, $string2)
{
$vz = new Vizhash16x16();
$pngdata = $vz->generate($string1);
if (empty($string1))
{
$this->assertEquals($pngdata, '');
} else {
$this->assertNotEquals($pngdata, '');
file_put_contents($this->_file, $pngdata);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$this->assertEquals('image/png', $finfo->file($this->_file));
if ($string1 !== $string2)
{
$this->assertNotEquals($pngdata, $string2);
}
}
$this->assertEquals($pngdata, $vz->generate($string1));
}
);
}
public function testVizhashGeneratesUniquePngsPerIp()
{
$vz = new Vizhash16x16();