<commit_before><?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license.
 */

namespace RoaveTest\SecurityAdvisories;

use PHPUnit_Framework_TestCase;
use Roave\SecurityAdvisories\Advisory;
use Roave\SecurityAdvisories\Component;
use Roave\SecurityAdvisories\VersionConstraint;

/**
 * Tests for {@see \Roave\SecurityAdvisories\VersionConstraint}
 *
 * @covers \Roave\SecurityAdvisories\VersionConstraint
 */
class VersionConstraintTest extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider closedRangesProvider
     *
     * @param string $stringConstraint
     */
    public function testFromRange($stringConstraint)
    {
        $constraint = VersionConstraint::fromString($stringConstraint);

        $this->assertInstanceOf(VersionConstraint::class, $constraint);
        $this->assertTrue($constraint->isSimpleRangeString());
        $this->assertSame($stringConstraint, $constraint->getConstraintString());
        $this->assertRegExp('/(\d+.)*\d+/', $constraint->getLowerBound());
        $this->assertRegExp('/(\d+.)*\d+/', $constraint->getUpperBound());

        $this->assertSame((bool) preg_match('/>=/', $stringConstraint), $constraint->isLowerBoundIncluded());
        $this->assertSame((bool) preg_match('/<=/',$stringConstraint), $constraint->isUpperBoundIncluded());
    }

    /**
     * @dataProvider complexRangesProvider
     *
     * @param string $stringConstraint
     */
    public function testFromRangeWithComplexRanges($stringConstraint)
    {
        $constraint = VersionConstraint::fromString($stringConstraint);

        $this->assertInstanceOf(VersionConstraint::class, $constraint);
        $this->assertFalse($constraint->isSimpleRangeString());
        $this->assertSame($stringConstraint, $constraint->getConstraintString());
    }

    public function testContainsWithMatchingRanges()
    {
        $constraint1 = VersionConstraint::fromString('>1.2.3,<4.5.6');
        $constraint2 = VersionConstraint::fromString('>1.2.4,<4.5.5');

        $this->assertTrue($constraint1->contains($constraint2));
        $this->assertFalse($constraint2->contains($constraint1));
    }

    public function testCannotCompareComplexRanges()
    {
        $constraint1 = VersionConstraint::fromString('1|2');
        $constraint2 = VersionConstraint::fromString('1|2|3');

        $this->assertFalse($constraint1->contains($constraint2));
        $this->assertFalse($constraint2->contains($constraint1));
    }

    /**
     * @dataProvider rangesForComparisonProvider
     *
     * @param string $constraintString1
     * @param string $constraintString2
     * @param bool   $constraint1ContainsConstraint2
     * @param bool   $constraint2ContainsConstraint1
     *
     * @return void
     */
    public function testContainsWithRanges(
        $constraintString1,
        $constraintString2,
        $constraint1ContainsConstraint2,
        $constraint2ContainsConstraint1
    ) {
        $constraint1 = VersionConstraint::fromString($constraintString1);
        $constraint2 = VersionConstraint::fromString($constraintString2);

        $this->assertSame($constraint1ContainsConstraint2, $constraint1->contains($constraint2));
        $this->assertSame($constraint2ContainsConstraint1, $constraint2->contains($constraint1));
    }

    /**
     * @return string[][]
     */
    public function closedRangesProvider()
    {
        $matchedRanges = [
            ['>1.2.3,<4.5.6'],
            ['>=1.2.3,<4.5.6'],
            ['>1.2.3,<=4.5.6'],
            ['>=1.2.3,<=4.5.6'],
            ['>  1.2.3  , < 4.5.6'],
            ['>=  1.2.3  , <4.5.6'],
            ['> 1.2.3 , <=4.5.6'],
            ['>=1.2.3, <=4.5.6'],
            ['>11.22.33,<44.55.66'],
            ['>11.22.33.44.55.66.77,<44.55.66.77.88.99.1010'],
            ['>1,<2'],
        ];

        return array_combine(
            array_map(
                function (array $entry) {
                    return $entry[0];
                },
                $matchedRanges
            ),
            $matchedRanges
        );
    }

    /**
     * @return string[][]
     */
    public function complexRangesProvider()
    {
        return $this->dataProviderFirstValueAsProviderKey([
            ['>1.2.3,<4.5.6,<7.8.9'],
            ['1.2.3|4.5.6'],
            ['1'],
            ['1|2'],
            ['<1,<2'],
            ['>1,>2'],
            ['~2'],
        ]);
    }

    /**
     * @return string[][]|bool[][]
     *
     *  - range1
     *  - range2
     *  - range1 contains range2
     *  - range2 contains range1
     */
    public function rangesForComparisonProvider()
    {
        return $this->dataProviderFirstValueAsProviderKey([
            ['>1,<2', '>1,<2', true, true],
            ['>1,<2', '>1.1,<2', true, false],
            ['>1,<2', '>3,<4', false, false],
            ['>1.1,<2.1', '>1.2,<2', true, false],
            ['>100,<200', '>1.0.0,<2.0.0', false, false],
            ['>1.10,<2', '>1.100,<2', true, false],
            ['>1,<2.10', '>1,<2.100', false, true],
            ['>1.0,<2', '>1,<2', true, true],
            ['>1,<2', '>1,<2.0', true, true],
        ]);
    }

    /**
     * @param mixed[][] $entries
     *
     * @return mixed[][]
     */
    private function dataProviderFirstValueAsProviderKey(array $entries)
    {
        return array_combine(
            array_map(
                function (array $entry) {
                    return $entry[0];
                },
                $entries
            ),
            $entries
        );
    }
}
<commit_msg>Testing with ranges with multiple dots after the major version<commit_after><?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license.
 */

namespace RoaveTest\SecurityAdvisories;

use PHPUnit_Framework_TestCase;
use Roave\SecurityAdvisories\Advisory;
use Roave\SecurityAdvisories\Component;
use Roave\SecurityAdvisories\VersionConstraint;

/**
 * Tests for {@see \Roave\SecurityAdvisories\VersionConstraint}
 *
 * @covers \Roave\SecurityAdvisories\VersionConstraint
 */
class VersionConstraintTest extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider closedRangesProvider
     *
     * @param string $stringConstraint
     */
    public function testFromRange($stringConstraint)
    {
        $constraint = VersionConstraint::fromString($stringConstraint);

        $this->assertInstanceOf(VersionConstraint::class, $constraint);
        $this->assertTrue($constraint->isSimpleRangeString());
        $this->assertSame($stringConstraint, $constraint->getConstraintString());
        $this->assertRegExp('/(\d+.)*\d+/', $constraint->getLowerBound());
        $this->assertRegExp('/(\d+.)*\d+/', $constraint->getUpperBound());

        $this->assertSame((bool) preg_match('/>=/', $stringConstraint), $constraint->isLowerBoundIncluded());
        $this->assertSame((bool) preg_match('/<=/',$stringConstraint), $constraint->isUpperBoundIncluded());
    }

    /**
     * @dataProvider complexRangesProvider
     *
     * @param string $stringConstraint
     */
    public function testFromRangeWithComplexRanges($stringConstraint)
    {
        $constraint = VersionConstraint::fromString($stringConstraint);

        $this->assertInstanceOf(VersionConstraint::class, $constraint);
        $this->assertFalse($constraint->isSimpleRangeString());
        $this->assertSame($stringConstraint, $constraint->getConstraintString());
    }

    public function testContainsWithMatchingRanges()
    {
        $constraint1 = VersionConstraint::fromString('>1.2.3,<4.5.6');
        $constraint2 = VersionConstraint::fromString('>1.2.4,<4.5.5');

        $this->assertTrue($constraint1->contains($constraint2));
        $this->assertFalse($constraint2->contains($constraint1));
    }

    public function testCannotCompareComplexRanges()
    {
        $constraint1 = VersionConstraint::fromString('1|2');
        $constraint2 = VersionConstraint::fromString('1|2|3');

        $this->assertFalse($constraint1->contains($constraint2));
        $this->assertFalse($constraint2->contains($constraint1));
    }

    /**
     * @dataProvider rangesForComparisonProvider
     *
     * @param string $constraintString1
     * @param string $constraintString2
     * @param bool   $constraint1ContainsConstraint2
     * @param bool   $constraint2ContainsConstraint1
     *
     * @return void
     */
    public function testContainsWithRanges(
        $constraintString1,
        $constraintString2,
        $constraint1ContainsConstraint2,
        $constraint2ContainsConstraint1
    ) {
        $constraint1 = VersionConstraint::fromString($constraintString1);
        $constraint2 = VersionConstraint::fromString($constraintString2);

        $this->assertSame($constraint1ContainsConstraint2, $constraint1->contains($constraint2));
        $this->assertSame($constraint2ContainsConstraint1, $constraint2->contains($constraint1));
    }

    /**
     * @return string[][]
     */
    public function closedRangesProvider()
    {
        $matchedRanges = [
            ['>1.2.3,<4.5.6'],
            ['>=1.2.3,<4.5.6'],
            ['>1.2.3,<=4.5.6'],
            ['>=1.2.3,<=4.5.6'],
            ['>  1.2.3  , < 4.5.6'],
            ['>=  1.2.3  , <4.5.6'],
            ['> 1.2.3 , <=4.5.6'],
            ['>=1.2.3, <=4.5.6'],
            ['>11.22.33,<44.55.66'],
            ['>11.22.33.44.55.66.77,<44.55.66.77.88.99.1010'],
            ['>1,<2'],
        ];

        return array_combine(
            array_map(
                function (array $entry) {
                    return $entry[0];
                },
                $matchedRanges
            ),
            $matchedRanges
        );
    }

    /**
     * @return string[][]
     */
    public function complexRangesProvider()
    {
        return $this->dataProviderFirstValueAsProviderKey([
            ['>1.2.3,<4.5.6,<7.8.9'],
            ['1.2.3|4.5.6'],
            ['1'],
            ['1|2'],
            ['<1,<2'],
            ['>1,>2'],
            ['~2'],
        ]);
    }

    /**
     * @return string[][]|bool[][]
     *
     *  - range1
     *  - range2
     *  - range1 contains range2
     *  - range2 contains range1
     */
    public function rangesForComparisonProvider()
    {
        return $this->dataProviderFirstValueAsProviderKey([
            ['>1,<2', '>1,<2', true, true],
            ['>1,<2', '>1.1,<2', true, false],
            ['>1,<2', '>3,<4', false, false],
            ['>1.1,<2.1', '>1.2,<2', true, false],
            ['>100,<200', '>1.0.0,<2.0.0', false, false],
            ['>1.10,<2', '>1.100,<2', true, false],
            ['>1,<2.10', '>1,<2.100', false, true],
            ['>1.0,<2', '>1,<2', true, true],
            ['>1,<2.0', '>1,<2', true, true],
            ['>1.0.0,<2', '>1,<2', true, true],
            ['>1,<2.0.0', '>1,<2', true, true],
        ]);
    }

    /**
     * @param mixed[][] $entries
     *
     * @return mixed[][]
     */
    private function dataProviderFirstValueAsProviderKey(array $entries)
    {
        return array_combine(
            array_map(
                function (array $entry) {
                    return $entry[0];
                },
                $entries
            ),
            $entries
        );
    }
}

{'fraction_non_alphanumeric': 0.15488267637264772, 'fraction_numerical': 0.03748160768218075, 'mean_word_length': 2.5092391304347825, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '681742', 'n_tokens_mistral': 3975, 'n_tokens_neox': 3763, 'n_words': 735}