-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from xavierleune/fix/igbinary_serialize
Fix #148 igbinary_serialize cannot serialize resource(stream)
- Loading branch information
Showing
5 changed files
with
199 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Kevinrob\GuzzleCache; | ||
|
||
/** | ||
* | ||
* This object is only meant to provide a callable to `GuzzleHttp\Psr7\PumpStream`. | ||
* | ||
* @internal don't use it in your project. | ||
*/ | ||
class BodyStore | ||
{ | ||
private $body; | ||
|
||
private $read = 0; | ||
|
||
private $toRead; | ||
|
||
public function __construct(string $body) | ||
{ | ||
$this->body = $body; | ||
$this->toRead = mb_strlen($this->body); | ||
} | ||
|
||
/** | ||
* @param int $length | ||
* @return false|string | ||
*/ | ||
public function __invoke(int $length) | ||
{ | ||
if ($this->toRead <= 0) { | ||
return false; | ||
} | ||
|
||
$length = min($length, $this->toRead); | ||
|
||
$body = mb_substr( | ||
$this->body, | ||
$this->read, | ||
$length | ||
); | ||
$this->toRead -= $length; | ||
$this->read += $length; | ||
return $body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Kevinrob\GuzzleCache\Tests; | ||
|
||
use http\Message\Body; | ||
use Kevinrob\GuzzleCache\BodyStore; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BodyStoreTest extends TestCase | ||
{ | ||
public function testBodyStoreReturnsAllContentIfAskedLengthIsGreaterThanAvailable() | ||
{ | ||
$str = 'Not so long'; | ||
$bodyStore = new BodyStore($str); | ||
$this->assertEquals($str, $bodyStore(PHP_INT_MAX)); | ||
} | ||
|
||
public function testBodyStoreReturnsFalseIsAllHasBeenRead() | ||
{ | ||
$str = 'Not so long'; | ||
$bodyStore = new BodyStore($str); | ||
$bodyStore(PHP_INT_MAX); | ||
$this->assertFalse($bodyStore(1)); | ||
} | ||
|
||
public function testBodyStoreCanReadAllContentWhenIteratedEnough() | ||
{ | ||
$originalString = <<<EOF | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | ||
EOF; | ||
$bodyStore = new BodyStore($originalString); | ||
|
||
$got = ''; | ||
while ($str = $bodyStore(1)) { | ||
$got .= $str; | ||
} | ||
$this->assertEquals($originalString, $got); | ||
} | ||
|
||
public function testBodyStoreReturnsEmptyStringWhenAsking0() | ||
{ | ||
$str = 'Not so long'; | ||
$bodyStore = new BodyStore($str); | ||
$this->assertEquals('', $bodyStore(0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters