开发者

How can I create a non-stream resource for use in a test case, without requiring optional PHP extensions?

I need to create a variable of type resource, which is not a stream. This means that resources created by fopen() etc. are out. Ideally it should be possible to create the resource without the help of extensions.

I would use an SQLite in-memory database handle, but sqlite is an optional PHP extension (even though it is enabled by default) which is not guaranteed to be installed on any given system (in current Ubuntu installations, for example, the sqlite extension is NOT installed by default).

Here's an example of how the test case might be structured:

function is_stream($resource)
{
  // some code that determines whether input is stream
}

class StreamResourceTest extends PHPUnit_Framework_TestCase
{
  public function testStreamResource()
  {
    $stream = fopen(__FILE__, 'r');
    $nonStream = ???; // how can this be created?

    $this->assertTrue(is_resource($stream));
    $this->assertTrue(is_stream($stream));

    $this->assertTrue(is_resource($nonStream));
    $this->assertFalse(is_stream($nonStream开发者_运维技巧));

    fclose($stream);
  }
}


stream_context_create()Docs creates a resource that is not strictly a stream resource.

They could be considered to not require extensions since

Streams are an integral part of PHP as of version 4.3.0. No steps are required to enable them.

...although the same could possibly be said of sqlite, so whether you want to accept that as better alternative would be up to you.

Also, this page might help.


I was running over List of Resource Types Docs as well and looking for something that might not be stream related. Most resource types require an extension or at least a compile flag (if you put file-system out).

One possible candidate might be socket_create()Docs.

However as it's unclear what you're trying to achieve in testing (probably mock a resource type?), I have no clue if this is exactly what you're looking for. You might want to add your test code to the question to make your problem more clear.

Edit:

As you have clarified that you want to test that a certain value is a resource and then you want to check for the resource type:

$type = get_resource_type($var);

See get_resource_type()Docs and is_resource()Docs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜