Skip to content

Commit b5d8d75

Browse files
committed
Unit test in progress...
1 parent c043117 commit b5d8d75

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

src/Symfony/Component/HttpFoundation/Tests/File/FileTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\File\File;
1515
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
16+
use Symfony\Component\HttpFoundation\File\Tests\Fixtures\PaddingStreamWrapper;
1617

1718
class FileTest extends \PHPUnit_Framework_TestCase
1819
{
@@ -28,6 +29,24 @@ public function testGetMimeTypeUsesMimeTypeGuessers()
2829
$this->assertEquals('image/gif', $file->getMimeType());
2930
}
3031

32+
public function testGetSizeWithStreamFilter()
33+
{
34+
// @todo Where should these fixture classes go so this isn't necessary?
35+
require_once 'Fixtures/PaddingStreamWrapper.php';
36+
require_once 'Fixtures/PaddingStreamFilter.php';
37+
38+
stream_wrapper_register('pad', PaddingStreamWrapper::class);
39+
$path = 'pad://test';
40+
41+
$file = new File($path);
42+
43+
ob_start();
44+
$filtered_size = readfile($path);
45+
ob_end_clean();
46+
47+
$this->assertEquals($filtered_size, $file->getSize());
48+
}
49+
3150
public function testGuessExtensionWithoutGuesser()
3251
{
3352
$file = new File(__DIR__.'/Fixtures/directory/.empty');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpFoundation\File\Tests\Fixtures;
4+
5+
class PaddingStreamFilter extends \php_user_filter
6+
{
7+
const NAME = 'pad';
8+
9+
const PADDING = ' ';
10+
11+
public function filter($in, $out, &$consumed, $closing)
12+
{
13+
while ($bucket = stream_bucket_make_writeable($in)) {
14+
$bucket->data .= self::PADDING;
15+
$consumed += $bucket->datalen;
16+
stream_bucket_append($out, $bucket);
17+
}
18+
19+
return PSFS_PASS_ON;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Symfony\Component\HttpFoundation\File\Tests\Fixtures;
4+
5+
class PaddingStreamWrapper
6+
{
7+
protected $file;
8+
9+
public function stream_open($path, $mode, $options, &$opened_path)
10+
{
11+
$this->file = fopen('php://temp', 'r+');
12+
13+
$path_parts = explode('://', $path);
14+
$file_contents = end($path_parts);
15+
fwrite($this->file, $file_contents);
16+
fseek($this->file, 0);
17+
18+
stream_filter_register(PaddingStreamFilter::NAME, PaddingStreamFilter::class);
19+
stream_filter_append($this->file, PaddingStreamFilter::NAME, STREAM_FILTER_READ);
20+
21+
return (bool) $this->file;
22+
}
23+
24+
public function stream_read($count)
25+
{
26+
return fread($this->file, $count);
27+
}
28+
29+
public function stream_write($data)
30+
{
31+
return fwrite($this->file, $data);
32+
}
33+
34+
public function stream_tell()
35+
{
36+
return ftell($this->file);
37+
}
38+
39+
public function stream_eof()
40+
{
41+
return feof($this->file);
42+
}
43+
44+
public function stream_seek($offset, $whence)
45+
{
46+
return fseek($this->file, $offset, $whence);
47+
}
48+
49+
public function stream_stat()
50+
{
51+
return fstat($this->file);
52+
}
53+
54+
public function url_stat($path, $flags)
55+
{
56+
$file = fopen($path, 'r');
57+
$stat = fstat($file);
58+
fclose($file);
59+
60+
return $stat;
61+
}
62+
}

0 commit comments

Comments
 (0)