Mock php resource type for unittesting

I need to write tests for a php class that uses the resource type created by the apache extension that we use, but let me say that it is a database class with the mysql resource type.

Obviously, I would like to make fun of these global functions, but how to fake the return type when the class checks if the resource was created normally?

The resource page in php doc even has one comment complaining about the inability of mocks to test. Is this the final answer?

let's say I have a code (chrome example):

class DB {
  function init(){
    $this->handle = mysql_connect('myserver');
    if( get_resource_type($this->handle) != 'mysql' ) return false;
      return true;
    }
}

and then tests that test success and failure by taunting mysql_connect.

+3
source share
2 answers

, . , PHP, .. Pp.

, , - , . , . .

(), , , . :

$url = 'http://example.com/';
$handle = curl_init($url);
curl_exec($handle);

$handle mockable , , curl . , :

$url = 'http://example.com/';
$curl = new Curl($url);
$curl->exec();

, PHP-:

class Curl
{
    private $handle;
    public function __construct($url) {
        $this->handle = curl_init($url);
    }
    public function exec() {
        return curl_exec($this->handle);
    }
}

. , , , , , .

API-, imap, - , IMAP . , , .

+4
0

All Articles