<commit_before><?php

use ZBateson\MailMimeParser\MimePart;

/**
 * Description of MimePartTest
 *
 * @group MimePart
 * @author <NAME>
 */
class MimePartTest extends PHPUnit_Framework_TestCase
{
    private $mockHeaderFactory;
    
    public function setUp()
    {
        $this->mockHeaderFactory = $this->getMockBuilder('ZBateson\MailMimeParser\Header\HeaderFactory')
            ->disableOriginalConstructor()
            ->setMethods(['newInstance'])
            ->getMock();
    }
    
    public function tearDown()
    {
        unset($this->mockHeaderFactory);
    }
    
    protected function getMockedParameterHeader($name, $value)
    {
        $header = $this->getMockBuilder('ZBateson\MailMimeParser\Header\ParameterHeader')
            ->disableOriginalConstructor()
            ->setMethods(['getValue', 'getName', 'hasParameter'])
            ->getMock();
        $header->method('getName')->willReturn($name);
        $header->method('getValue')->willReturn($value);
        $header->method('hasParameter')->willReturn(true);
        return $header;
    }

    public function testAttachContentResourceHandle()
    {
        $hf = $this->mockHeaderFactory;
        $part = new MimePart($hf);

        $this->assertFalse($part->hasContent());
        $res = fopen('php://memory', 'rw');

        $part->attachContentResourceHandle($res);
        $this->assertTrue($part->hasContent());
        $this->assertSame($res, $part->getContentResourceHandle());
    }

    public function testSetRawHeader()
    {
        $hf = $this->mockHeaderFactory;
        $firstHeader = $this->getMockedParameterHeader('First-Header', 'Value');
        $secondHeader = $this->getMockedParameterHeader('Second-Header', 'Second Value');
        
        $hf->expects($this->exactly(2))
            ->method('newInstance')
            ->withConsecutive(
                [$firstHeader->getName(), $firstHeader->getValue()],
                [$secondHeader->getName(), $secondHeader->getValue()]
            )
            ->willReturnOnConsecutiveCalls($firstHeader, $secondHeader);
        
        $part = new MimePart($hf);
        $part->setRawHeader($firstHeader->getName(), $firstHeader->getValue());
        $part->setRawHeader($secondHeader->getName(), $secondHeader->getValue());
        $this->assertSame($firstHeader, $part->getHeader($firstHeader->getName()));
        $this->assertSame($secondHeader, $part->getHeader($secondHeader->getName()));
        $this->assertEquals($firstHeader->getValue(), $part->getHeaderValue($firstHeader->getName()));
        $this->assertEquals($secondHeader->getValue(), $part->getHeaderValue($secondHeader->getName()));
        $this->assertCount(2, $part->getHeaders());
    }
    
    public function testHeaderCaseInsensitive()
    {
        $hf = $this->mockHeaderFactory;
        $firstHeader = $this->getMockedParameterHeader('First-Header', 'Value');
        $secondHeader = $this->getMockedParameterHeader('Second-Header', 'Second Value');
        $thirdHeader = $this->getMockedParameterHeader('FIRST-header', 'Third Value');
        
        $hf->expects($this->exactly(3))
            ->method('newInstance')
            ->withConsecutive(
                [$firstHeader->getName(), $firstHeader->getValue()],
                [$secondHeader->getName(), $secondHeader->getValue()],
                [$thirdHeader->getName(), $thirdHeader->getValue()]
            )
            ->willReturnOnConsecutiveCalls($firstHeader, $secondHeader, $thirdHeader);
        
        $part = new MimePart($hf);
        $part->setRawHeader($firstHeader->getName(), $firstHeader->getValue());
        $part->setRawHeader($secondHeader->getName(), $secondHeader->getValue());
        $part->setRawHeader($thirdHeader->getName(), $thirdHeader->getValue());
        
        $this->assertSame($thirdHeader, $part->getHeader($firstHeader->getName()));
        $this->assertSame($secondHeader, $part->getHeader($secondHeader->getName()));
        $this->assertCount(2, $part->getHeaders());
    }
    
    public function testParent()
    {
        $hf = $this->mockHeaderFactory;
        $part = new MimePart($hf);
        $parent = new MimePart($hf);
        $part->setParent($parent);
        $this->assertSame($parent, $part->getParent());
    }
}<commit_msg>Cleanup MimePartTest<commit_after><?php

use ZBateson\MailMimeParser\MimePart;

/**
 * Description of MimePartTest
 *
 * @group MimePart
 * @author <NAME>
 */
class MimePartTest extends PHPUnit_Framework_TestCase
{
    private $mockHeaderFactory;
    
    public function setUp()
    {
        $this->mockHeaderFactory = $this->getMockBuilder('ZBateson\MailMimeParser\Header\HeaderFactory')
            ->disableOriginalConstructor()
            ->setMethods(['newInstance'])
            ->getMock();
    }
    
    public function tearDown()
    {
        unset($this->mockHeaderFactory);
    }
    
    protected function getMockedParameterHeader($name, $value, $parameterValue = null)
    {
        $header = $this->getMockBuilder('ZBateson\MailMimeParser\Header\ParameterHeader')
            ->disableOriginalConstructor()
            ->setMethods(['getValue', 'getName', 'getValueFor', 'hasParameter'])
            ->getMock();
        $header->method('getName')->willReturn($name);
        $header->method('getValue')->willReturn($value);
        $header->method('getValueFor')->willReturn($parameterValue);
        $header->method('hasParameter')->willReturn(true);
        return $header;
    }
    
    public function testAttachContentResourceHandle()
    {
        $hf = $this->mockHeaderFactory;
        $part = new MimePart($hf);
        $res = fopen('php://memory', 'rw');
        $part->attachContentResourceHandle($res);
        $this->assertSame($res, $part->getContentResourceHandle());
    }
    
    public function testHasContent()
    {
        $hf = $this->mockHeaderFactory;
        $part = new MimePart($hf);

        $this->assertFalse($part->hasContent());
        $res = fopen('php://memory', 'rw');
        $part->attachContentResourceHandle($res);
        $this->assertTrue($part->hasContent());
    }

    public function testSetRawHeader()
    {
        $hf = $this->mockHeaderFactory;
        $firstHeader = $this->getMockedParameterHeader('First-Header', 'Value');
        $secondHeader = $this->getMockedParameterHeader('Second-Header', 'Second Value');
        
        $hf->expects($this->exactly(2))
            ->method('newInstance')
            ->withConsecutive(
                [$firstHeader->getName(), $firstHeader->getValue()],
                [$secondHeader->getName(), $secondHeader->getValue()]
            )
            ->willReturnOnConsecutiveCalls($firstHeader, $secondHeader);
        
        $part = new MimePart($hf);
        $part->setRawHeader($firstHeader->getName(), $firstHeader->getValue());
        $part->setRawHeader($secondHeader->getName(), $secondHeader->getValue());
        $this->assertSame($firstHeader, $part->getHeader($firstHeader->getName()));
        $this->assertSame($secondHeader, $part->getHeader($secondHeader->getName()));
        $this->assertEquals($firstHeader->getValue(), $part->getHeaderValue($firstHeader->getName()));
        $this->assertEquals($secondHeader->getValue(), $part->getHeaderValue($secondHeader->getName()));
        $this->assertCount(2, $part->getHeaders());
        $this->assertEquals(['first-header' => $firstHeader, 'second-header' => $secondHeader], $part->getHeaders());
    }
    
    public function testHeaderCaseInsensitive()
    {
        $hf = $this->mockHeaderFactory;
        $firstHeader = $this->getMockedParameterHeader('First-Header', 'Value');
        $secondHeader = $this->getMockedParameterHeader('Second-Header', 'Second Value');
        $thirdHeader = $this->getMockedParameterHeader('FIRST-header', 'Third Value');
        
        $hf->expects($this->exactly(3))
            ->method('newInstance')
            ->withConsecutive(
                [$firstHeader->getName(), $firstHeader->getValue()],
                [$secondHeader->getName(), $secondHeader->getValue()],
                [$thirdHeader->getName(), $thirdHeader->getValue()]
            )
            ->willReturnOnConsecutiveCalls($firstHeader, $secondHeader, $thirdHeader);
        
        $part = new MimePart($hf);
        $part->setRawHeader($firstHeader->getName(), $firstHeader->getValue());
        $part->setRawHeader($secondHeader->getName(), $secondHeader->getValue());
        $part->setRawHeader($thirdHeader->getName(), $thirdHeader->getValue());
        
        $this->assertSame($thirdHeader, $part->getHeader('first-header'));
        $this->assertSame($secondHeader, $part->getHeader('second-header'));
    }
    
    public function testParent()
    {
        $hf = $this->mockHeaderFactory;
        $part = new MimePart($hf);
        $parent = new MimePart($hf);
        $part->setParent($parent);
        $this->assertSame($parent, $part->getParent());
    }
    
    public function testGetHeaderParameter()
    {
        $hf = $this->mockHeaderFactory;
        $header = $this->getMockedParameterHeader('First-Header', 'Value', 'param-value');
        $hf->expects($this->exactly(1))
            ->method('newInstance')
            ->withConsecutive(
                [$header->getName(), $header->getValue()]
            )
            ->willReturnOnConsecutiveCalls($header);
        $part = new MimePart($hf);
        $part->setRawHeader($header->getName(), $header->getValue());
        
        $this->assertEquals('param-value', $part->getHeaderParameter('first-header', 'param'));
    }
}
{'fraction_non_alphanumeric': 0.16791588590464293, 'fraction_numerical': 0.0008328128253175099, 'mean_word_length': 2.9764072847682117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 211, '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': '5380046', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2582, 'n_words': 353}