Skip to content

Commit

Permalink
unit testing improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k committed Jan 11, 2017
1 parent 42c133c commit cc36d51
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
preset: psr2

2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache
Expand Down
33 changes: 33 additions & 0 deletions tests/Constraint/AbstractConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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 AbstractConstraintTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testAbstractConstraintWithDeprecated()
{
$expectedString = 'pretty string';
$constraint = new \ReflectionClass('\Composer\Semver\Constraint\AbstractConstraint');
$setPrettyString = $constraint->getMethod('setPrettyString');
$setPrettyString->setAccessible(true);
$setPrettyString->invoke($constraint, 'pretty string');

$getPrettyString = $constraint->getMethod('getPrettyString');
$getPrettyString->setAccessible(true);
$result = $setPrettyString->invoke($constraint);

$this->assertSame($expectedString, $result);
}
}
48 changes: 42 additions & 6 deletions tests/Constraint/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@

class ConstraintTest extends \PHPUnit_Framework_TestCase
{
protected $constraint;
protected $versionProvide;

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

protected function tearDown()
{
unset($this->constraint);
unset($this->versionProvide);
}

/**
* @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 @@ -119,16 +157,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
52 changes: 52 additions & 0 deletions tests/Constraint/EmptyConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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();
}

protected function tearDown()
{
unset($this->versionProvide);
unset($this->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);
}
}
72 changes: 63 additions & 9 deletions tests/Constraint/MultiConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,92 @@

class MultiConstraintTest extends \PHPUnit_Framework_TestCase
{
protected $multiConstraint;
protected $versionRequireStart;
protected $versionRequireEnd;


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

protected function tearDown()
{
unset($this->multiConstraint);
unset($this->versionRequireStart);
unset($this->versionRequireEnd);
}

public function testIsConjunctive()
{
$result = $this->multiConstraint->isConjunctive();

$this->assertTrue($result);
}

public function testIsDisjunctive()
{
$result = $this->multiConstraint->isDisjunctive();

$this->assertFalse($result);
}

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 @@ -58,6 +58,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 cc36d51

Please sign in to comment.