<commit_before><?php

namespace Aws\Test\Glacier\Multipart;

use Aws\Glacier\Multipart\PartGenerator;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Stream\FnStream;

/**
 * @covers Aws\Glacier\Multipart\PartGenerator
 */
class PartGeneratorTest extends \PHPUnit_Framework_TestCase
{
    public function testThrowsExceptionOnBadPartSize()
    {
        $this->setExpectedException('InvalidArgumentException');
        new PartGenerator(Stream::factory(fopen(__FILE__, 'r')), [
            'part_size' => 1024 * 1024 * 5
        ]);
    }

    public function getStreamTestCases()
    {
        return [
            [$this->createStream(true), 'GuzzleHttp\Stream\LimitStream'],
            [$this->createStream(false), 'GuzzleHttp\Stream\Stream'],
        ];
    }

    /**
     * @dataProvider getStreamTestCases
     */
    public function testCanGeneratePartsForStream($source, $bodyClass)
    {
        $generator = new PartGenerator($source);
        $parts = iterator_to_array($generator);
        $this->assertCount(4, $parts);

        $part = reset($parts);
        // Has all the part data.
        $this->assertEquals(
            ['checksum', 'ContentSHA256', 'body', 'range'],
            array_keys($part)
        );
        // For 1 MB parts, the checksums should be the same.
        $this->assertEquals($part['checksum'], $part['ContentSHA256']);
        // Verify the body is of the expected stream class.
        $this->assertInstanceOf($bodyClass, $part['body']);
    }

    private function createStream($seekable)
    {
        $stream = Stream::factory(str_repeat('.', 3146752)); // 3 MB + 1 KB

        return FnStream::decorate($stream, [
            'isSeekable' => function () use ($seekable) {return $seekable;},
            'getMetadata' => function ($key = null) use ($seekable, $stream) {
                return ($seekable && $key === 'wrapper_type')
                    ? 'plainfile'
                    : $stream->getMetadata($key);
            },
            '__toString' => function () {return '[...]';},
        ]);
    }
}<commit_msg>Fixing stream issue<commit_after><?php

namespace Aws\Test\Glacier\Multipart;

use Aws\Glacier\Multipart\PartGenerator;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Stream\FnStream;

/**
 * @covers Aws\Glacier\Multipart\PartGenerator
 */
class PartGeneratorTest extends \PHPUnit_Framework_TestCase
{
    public function testThrowsExceptionOnBadPartSize()
    {
        $this->setExpectedException('InvalidArgumentException');
        new PartGenerator(Stream::factory(fopen(__FILE__, 'r')), [
            'part_size' => 1024 * 1024 * 5
        ]);
    }

    public function getStreamTestCases()
    {
        return [
            [$this->createStream(true), 'GuzzleHttp\Stream\LimitStream'],
            [$this->createStream(false), 'GuzzleHttp\Stream\Stream'],
        ];
    }

    /**
     * @dataProvider getStreamTestCases
     */
    public function testCanGeneratePartsForStream($source, $bodyClass)
    {
        $generator = new PartGenerator($source);
        $parts = iterator_to_array($generator);
        $this->assertCount(4, $parts);

        $part = reset($parts);
        // Has all the part data.
        $this->assertEquals(
            ['checksum', 'ContentSHA256', 'body', 'range'],
            array_keys($part)
        );
        // For 1 MB parts, the checksums should be the same.
        $this->assertEquals($part['checksum'], $part['ContentSHA256']);
        // Verify the body is of the expected stream class.
        $this->assertInstanceOf($bodyClass, $part['body']);
    }

    private function createStream($seekable)
    {
        $stream = Stream::factory(str_repeat('.', 3146752)); // 3 MB + 1 KB

        return FnStream::decorate($stream, [
            'seek' => function ($pos) use ($seekable, $stream) {
                    return $seekable ? $stream->seek($pos) : false;
                },
            'isSeekable' => function () use ($seekable) {return $seekable;},
            'getMetadata' => function ($key = null) use ($seekable, $stream) {
                return ($seekable && $key === 'wrapper_type')
                    ? 'plainfile'
                    : $stream->getMetadata($key);
            }
        ]);
    }
}
{'fraction_non_alphanumeric': 0.14336324988190835, 'fraction_numerical': 0.01228153046764289, 'mean_word_length': 2.4153225806451615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769625', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1183, 'n_words': 243}