Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stream wrapper (tweaked #193) #230

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 86 additions & 1 deletion src/VCR/LibraryHooks/StreamWrapperHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use VCR\Util\Assertion;
use VCR\Util\HttpUtil;
use VCR\Util\StreamHelper;
use ArrayAccess;

/**
* Library hook for streamWrapper functions using stream_wrapper_register().
*/
class StreamWrapperHook implements LibraryHook
class StreamWrapperHook implements LibraryHook, ArrayAccess
{
/**
* @var \Closure Callback which will be executed when a request is intercepted.
Expand Down Expand Up @@ -154,6 +155,18 @@ public function stream_eof()
return $this->position >= strlen($this->response->getBody());
}

/**
* This method is called to set options on the stream.
*
* @link http://php.net/manual/en/streamwrapper.stream-set-option.php
* @return boolean Returns TRUE on success or FALSE on failure.
* If option is not implemented, FALSE should be returned.
*/
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}

/**
* Retrieve information about a file resource.
*
Expand Down Expand Up @@ -231,4 +244,76 @@ public function stream_metadata($path, $option, $var)
{
return false;
}

/**
* Emulate accessing headers inside the wrapper data.
*
* This allows libraries that consume the stream API to access headers.
*
* @link http://php.net/manual/en/context.http.php
* @param string $key The key to read. Only 'headers' is implemented.
*
* @return array|bool Either an array of headers or false
*/
public function offsetGet($key)
{
if ($key === 'headers') {
$httpHeader = sprintf(
'HTTP/%s %s %s',
$this->response->getHttpVersion(),
$this->response->getStatusCode(),
$this->response->getStatusMessage()
);
$headers = array($httpHeader);
foreach ($this->response->getHeaders() as $header => $value) {
$headers[] = "{$header}: {$value}";
}
return $headers;
}
return false;
}

/**
* Part of the ArrayAccess Interface.
*
* Not implemented
*
* @param string $key The key to set.
* @param mixed $value The value to set
*
* @return bool Always false
*/
public function offsetSet($key, $value)
{
return false;
}

/**
* Check if a metadata key exists.
*
* @param string $key The key to check.
*
* @return bool True if the key exists, false otherwise
*/
public function offsetExists($key)
{
if ($key === 'headers') {
return true;
}
return false;
}

/**
* Part of the ArrayAccess Interface.
*
* Not implemented
*
* @param string $key The key to unset
*
* @return bool Always false
*/
public function offsetUnset($key)
{
return false;
}
}
1 change: 1 addition & 0 deletions src/VCR/Util/Assertion.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace VCR\Util;

use Assert\Assertion as BaseAssertion;
Expand Down
1 change: 1 addition & 0 deletions src/VCR/Util/HttpClient.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace VCR\Util;

use VCR\Request;
Expand Down
20 changes: 20 additions & 0 deletions tests/VCR/LibraryHooks/StreamWrapperHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@ public function testSeek()
// invalid whence
$this->assertFalse($hook->stream_seek(0, -1));
}

public function testReadHeaders()
{
$hook = new StreamWrapperHook();
$hook->enable(function ($request) {
return new Response(
array('code' => 200, 'http_version' => '1.1', 'message' => 'OK'),
array('Content-Type' => 'text/plain'),
'A Test'
);
});
$hook->stream_open('http://example.com', 'r', 0, $openedPath);

$this->assertArrayHasKey('headers', $hook);
$expected = array(
'HTTP/1.1 200 OK',
'Content-Type: text/plain'
);
$this->assertEquals($expected, $hook['headers']);
}
}
1 change: 1 addition & 0 deletions tests/VCR/Util/StreamHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace VCR\Util;

use VCR\Request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace VCR\Example;

/**
Expand Down