Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k authored and Seldaek committed Jan 14, 2020
2 parents c6bea70 + 2c00546 commit 7321d51
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 15 deletions.
42 changes: 36 additions & 6 deletions tests/Constraint/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,38 @@

class ConstraintTest extends TestCase
{
protected $constraint;
protected $versionProvide;

protected function setUp()
{
$this->constraint = new Constraint('==', '1');
$this->versionProvide = new Constraint('==', 'dev-foo');
}

/**
* @expectedException \InvalidArgumentException
*/
public function testVersionCompareInvalidArgumentException()
{
$result = $this->constraint->versionCompare('1.1', '1.2', null);
}

public function testGetPrettyString()
{
$expectedString = 'pretty-string';
$this->constraint->setPrettyString($expectedString);
$result = $this->constraint->getPrettyString();

$this->assertSame($expectedString, $result);

$expectedVersion = '== 1';
$this->constraint->setPrettyString(null);
$result = $this->constraint->getPrettyString();

$this->assertSame($expectedVersion, $result);
}

public static function successfulVersionMatches()
{
return array(
Expand Down Expand Up @@ -121,16 +153,14 @@ public function testInverseMatchingOtherConstraints()
public function testComparableBranches()
{
$versionRequire = new Constraint('>', '0.12');
$versionProvide = new Constraint('==', 'dev-foo');

$this->assertFalse($versionRequire->matches($versionProvide));
$this->assertFalse($versionRequire->matchSpecific($versionProvide, true));
$this->assertFalse($versionRequire->matches($this->versionProvide));
$this->assertFalse($versionRequire->matchSpecific($this->versionProvide, true));

$versionRequire = new Constraint('<', '0.12');
$versionProvide = new Constraint('==', 'dev-foo');

$this->assertFalse($versionRequire->matches($versionProvide));
$this->assertTrue($versionRequire->matchSpecific($versionProvide, true));
$this->assertFalse($versionRequire->matches($this->versionProvide));
$this->assertTrue($versionRequire->matchSpecific($this->versionProvide, true));
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/Constraint/EmptyConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of composer/semver.
*
* (c) Composer <https://github.com/composer>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Composer\Semver\Constraint;

class EmptyConstraintTest extends \PHPUnit_Framework_TestCase
{
protected $versionProvide;
protected $emptyConstraint;

protected function setUp()
{
$this->versionProvide = new Constraint('==', '1.1');
$this->emptyConstraint = new EmptyConstraint();
}

public function testMatches()
{
$result = $this->emptyConstraint->matches($this->versionProvide);

$this->assertTrue($result);
}

public function testGetPrettyString()
{
$expectedString = 'pretty-string';
$this->emptyConstraint->setPrettyString($expectedString);
$result = $this->emptyConstraint->getPrettyString();

$this->assertSame($expectedString, $result);

$expectedString = '[]';
$this->emptyConstraint->setPrettyString(null);
$result = $this->emptyConstraint->getPrettyString();

$this->assertSame($expectedString, $result);
}
}
62 changes: 53 additions & 9 deletions tests/Constraint/MultiConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,82 @@

class MultiConstraintTest extends TestCase
{
protected $versionRequireStart;
protected $versionRequireEnd;

protected function setUp()
{
$this->versionRequireStart = new Constraint('>', '1.0');
$this->versionRequireEnd = new Constraint('<', '1.2');
}

public function testIsConjunctive()
{
$multiConstraint = new MultiConstraint(array(), true);
$this->assertTrue($multiConstraint->isConjunctive());
$this->assertFalse($multiConstraint->isDisjunctive());
}

public function testIsDisjunctive()
{
$multiConstraint = new MultiConstraint(array(), false);
$this->assertFalse($multiConstraint->isConjunctive());
$this->assertTrue($multiConstraint->isDisjunctive());
}

public function testMultiVersionMatchSucceeds()
{
$versionRequireStart = new Constraint('>', '1.0');
$versionRequireEnd = new Constraint('<', '1.2');
$versionProvide = new Constraint('==', '1.1');

$multiRequire = new MultiConstraint(array($versionRequireStart, $versionRequireEnd));
$multiRequire = new MultiConstraint(array($this->versionRequireStart, $this->versionRequireEnd));

$this->assertTrue($multiRequire->matches($versionProvide));
}

public function testMultiVersionProvidedMatchSucceeds()
{
$versionRequireStart = new Constraint('>', '1.0');
$versionRequireEnd = new Constraint('<', '1.2');
$versionProvideStart = new Constraint('>=', '1.1');
$versionProvideEnd = new Constraint('<', '2.0');

$multiRequire = new MultiConstraint(array($versionRequireStart, $versionRequireEnd));
$multiRequire = new MultiConstraint(array($this->versionRequireStart, $this->versionRequireEnd));
$multiProvide = new MultiConstraint(array($versionProvideStart, $versionProvideEnd));

$this->assertTrue($multiRequire->matches($multiProvide));
}

public function testMultiVersionMatchSucceedsInsideForeachLoop()
{
$versionProvideStart = new Constraint('>', '1.0');
$versionProvideEnd = new Constraint('<', '1.2');

$multiRequire = new MultiConstraint(array($this->versionRequireStart, $this->versionRequireEnd), false);
$multiProvide = new MultiConstraint(array($versionProvideStart, $versionProvideEnd), false);

$this->assertTrue($multiRequire->matches($multiProvide));
}

public function testMultiVersionMatchFails()
{
$versionRequireStart = new Constraint('>', '1.0');
$versionRequireEnd = new Constraint('<', '1.2');
$versionProvide = new Constraint('==', '1.2');

$multiRequire = new MultiConstraint(array($versionRequireStart, $versionRequireEnd));
$multiRequire = new MultiConstraint(array($this->versionRequireStart, $this->versionRequireEnd));

$this->assertFalse($multiRequire->matches($versionProvide));
}

public function testGetPrettyString()
{
$multiConstraint = new MultiConstraint(array());
$expectedString = 'pretty-string';
$multiConstraint->setPrettyString($expectedString);
$result = $multiConstraint->getPrettyString();

$this->assertSame($expectedString, $result);

$expectedString = '[]';
$multiConstraint->setPrettyString(null);
$result = $multiConstraint->getPrettyString();

$this->assertSame($expectedString, $result);
}
}
16 changes: 16 additions & 0 deletions tests/SemverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ public function testSort(array $versions, array $sorted, array $rsorted)
$this->assertEquals($rsorted, Semver::rsort($versions));
}

public function testUsortShouldInitialVersionParserClass()
{
$versions = array('1.0', '2.0', '2.1');
$semver = new \ReflectionClass('\Composer\Semver\Semver');
$versionParserProperty = $semver->getProperty('versionParser');
$versionParserProperty->setAccessible(true);
$versionParserProperty = $versionParserProperty->setValue(null);

$manipulateVersionStringMethod = $semver->getMethod('usort');
$manipulateVersionStringMethod->setAccessible(true);
$result = $manipulateVersionStringMethod->invoke(new Semver(), $versions, 1);

$this->assertInternalType('array', $result);
$this->assertCount(3, $versions);
}

/**
* @return array
*/
Expand Down
Loading

0 comments on commit 7321d51

Please sign in to comment.