<commit_before><?php
namespace Bitworking;

class MimeparseTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Testing this protected method because it includes a lot of parsing
     * functionality that we wish to isolate from other tests.
     *
     * @covers Bitworking\Mimeparse::parseMediaRange
     * @covers Bitworking\Mimeparse::parseMimeType
     */
    public function testParseMediaRange()
    {
        $method = new \ReflectionMethod('Bitworking\Mimeparse', 'parseMediaRange');
        $method->setAccessible(true);

        $expected1 = array(
            0 => 'application',
            1 => 'xml',
            2 => array('q' => '1'),
        );

        $this->assertEquals($expected1, $method->invoke(null, 'application/xml;q=1'));
        $this->assertEquals($expected1, $method->invoke(null, 'application/xml'));
        $this->assertEquals($expected1, $method->invoke(null, 'application/xml;q='));

        $expected2 = array(
            0 => 'application',
            1 => 'xml',
            2 => array('q' => '1', 'b' => 'other'),
        );

        $this->assertEquals($expected2, $method->invoke(null, 'application/xml ; q=1;b=other'));
        $this->assertEquals($expected2, $method->invoke(null, 'application/xml ; q=2;b=other'));

        // Java URLConnection class sends an Accept header that includes a single "*"
        $this->assertEquals(array(
            0 => '*',
            1 => '*',
            2 => array('q' => '.2'),
        ), $method->invoke(null, ' *; q=.2'));
    }

    /**
     * Testing this protected method because it throws an exception that we
     * want to test.
     *
     * @covers Bitworking\Mimeparse::parseMimeType
     * @expectedException UnexpectedValueException
     * @expectedExceptionMessage malformed mime type
     */
    public function testParseMimeTypeWithMalformedMimeType()
    {
        $method = new \ReflectionMethod('Bitworking\Mimeparse', 'parseMediaRange');
        $method->setAccessible(true);

        $parsed = $method->invoke(null, 'application/;q=1');
    }

    /**
     * @covers Bitworking\Mimeparse::quality
     * @covers Bitworking\Mimeparse::qualityParsed
     * @covers Bitworking\Mimeparse::fitnessAndQualityParsed
     */
    public function testQuality()
    {
        $accept = 'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5';

        $this->assertEquals(1, Mimeparse::quality('text/html;level=1', $accept));
        $this->assertEquals(0.7, Mimeparse::quality('text/html', $accept));
        $this->assertEquals(0.3, Mimeparse::quality('text/plain', $accept));
        $this->assertEquals(0.5, Mimeparse::quality('image/jpeg', $accept));
        $this->assertEquals(0.4, Mimeparse::quality('text/html;level=2', $accept));<commit_msg>Expand test suite with expected values for the format.<commit_after><?php
namespace Bitworking;

use Bitworking\Mimeparse;

class MimeparseTest extends \PHPUnit_Framework_TestCase
{
    public function testParseMimeTypeWith()
    {
        $expected = array(
            'application',
            'xml',
            array('q' => '1'),
            'xml'
        );
        
        $this->assertEquals($expected, Mimeparse::parseMimeType('application/xml; q=1'));
    }
    
    public function testParseMimeTypeWithFormat()
    {
        $expected = array(
            'application',
            'xhtml+xml',
            array('q' => '1'),
            'xml'
        );
        
        $this->assertEquals($expected, Mimeparse::parseMimeType('application/xhtml+xml; q=1'));
    }

    public function testParseMimeTypeWithSingleWildCard()
    {
        $expected = array(
            '*',
            '*',
            array(),
            '*'
        );

        $this->assertEquals($expected, Mimeparse::parseMimeType('*'));
    }

    /**
     * @expectedException UnexpectedValueException
     * @expectedExceptionMessage malformed mime type
     */
    public function testParseMimeTypeWithMalformedMimeType()
    {
        $parsed = Mimeparse::parseMimeType('application/;q=1');
    }

    /**
     * Testing this protected method because it includes a lot of parsing
     * functionality that we wish to isolate from other tests.
     *
     * @covers Bitworking\Mimeparse::parseMediaRange
     */
    public function testParseMediaRange()
    {
        $method = new \ReflectionMethod('Bitworking\Mimeparse', 'parseMediaRange');
        $method->setAccessible(true);

        $expected1 = array(
            0 => 'application',
            1 => 'xml',
            2 => array('q' => '1'),
        );

        $this->assertEquals($expected1, $method->invoke(null, 'application/xml;q=1'));
        $this->assertEquals($expected1, $method->invoke(null, 'application/xml'));
        $this->assertEquals($expected1, $method->invoke(null, 'application/xml;q='));

        $expected2 = array(
            0 => 'application',
            1 => 'xml',
            2 => array('q' => '1', 'b' => 'other'),
        );

        $this->assertEquals($expected2, $method->invoke(null, 'application/xml ; q=1;b=other'));
        $this->assertEquals($expected2, $method->invoke(null, 'application/xml ; q=2;b=other'));

        // Java URLConnection class sends an Accept header that includes a single "*"
        $this->assertEquals(array(
            0 => '*',
            1 => '*',
            2 => array('q' => '.2'),
        ), $method->invoke(null, ' *; q=.2'));
    }

    /**
     * @covers Bitworking\Mimeparse::quality
     * @covers Bitworking\Mimeparse::qualityParsed
     * @covers Bitworking\Mimeparse::fitnessAndQualityParsed
     */
    public function testQuality()
    {
        $accept = 'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5';

        $this->assertEquals(1, Mimeparse::quality('text/html;level=1', $accept));
        $this->assertEquals(0.7, Mimeparse::quality('text/html', $accept));
        $this->assertEquals(0.3, Mimeparse::quality('text/plain', $accept));
        $this->assertEquals(0.5, Mimeparse::quality('image/jpeg', $accept));
        $this->assertEquals(0.4, Mimeparse::quality('text/html;level=2', $accept));
{'fraction_non_alphanumeric': 0.16239454899415964, 'fraction_numerical': 0.015249837767683322, 'mean_word_length': 2.645771732702543, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '5267525', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1806, 'n_words': 335}