Selaa lähdekoodia

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

El RIDO 9 vuotta sitten
vanhempi
sitoutus
59adfc1962
4 muutettua tiedostoa jossa 38 lisäystä ja 4 poistoa
  1. 2 1
      composer.json
  2. 1 1
      lib/Vizhash16x16.php
  3. 3 2
      tst/README.md
  4. 32 0
      tst/Vizhash16x16Test.php

+ 2 - 1
composer.json

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

+ 1 - 1
lib/Vizhash16x16.php

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

+ 3 - 2
tst/README.md

@@ -6,11 +6,12 @@ and its dependencies:
 * phpunit
 * phpunit
 * php-gd
 * php-gd
 * php-sqlite3
 * 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:
 Example for Debian and Ubuntu:
 ```console
 ```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:
 To run the tests, just change into this directory and run phpunit:

+ 32 - 0
tst/Vizhash16x16Test.php

@@ -2,9 +2,12 @@
 
 
 use PrivateBin\Persistence\ServerSalt;
 use PrivateBin\Persistence\ServerSalt;
 use PrivateBin\Vizhash16x16;
 use PrivateBin\Vizhash16x16;
+use Eris\Generator;
 
 
 class Vizhash16x16Test extends PHPUnit_Framework_TestCase
 class Vizhash16x16Test extends PHPUnit_Framework_TestCase
 {
 {
+    use Eris\TestTrait;
+
     private $_file;
     private $_file;
 
 
     private $_path;
     private $_path;
@@ -27,6 +30,35 @@ class Vizhash16x16Test extends PHPUnit_Framework_TestCase
         Helper::rmDir($this->_path);
         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()
     public function testVizhashGeneratesUniquePngsPerIp()
     {
     {
         $vz      = new Vizhash16x16();
         $vz      = new Vizhash16x16();