Skip to content

Commit

Permalink
Merge pull request #5 from pauci/feature/json-serializable
Browse files Browse the repository at this point in the history
Implement JsonSerializable interface
  • Loading branch information
nikolaposa authored Jun 11, 2016
2 parents 4af06cc + d982cda commit ed1e187
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ nbproject
.project
vendor
phpunit.xml
composer.lock
composer.lock
.idea
55 changes: 32 additions & 23 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Version;

use JsonSerializable;
use Version\Metadata\PreRelease;
use Version\Metadata\Build;
use Version\Exception\InvalidVersionElementException;
Expand All @@ -19,7 +20,7 @@
/**
* @author Nikola Posa <posa.nikola@gmail.com>
*/
final class Version
final class Version implements JsonSerializable
{
/**
* @var int
Expand Down Expand Up @@ -232,28 +233,6 @@ public function isBuild()
return !$this->build->isEmpty();
}

/**
* @return string
*/
public function getVersionString()
{
return
$this->major
. '.' . $this->minor
. '.' . $this->patch
. ($this->isPreRelease() ? '-' . (string) $this->preRelease : '')
. ($this->isBuild() ? '+' . (string) $this->build : '')
;
}

/**
* @return string
*/
public function __toString()
{
return $this->getVersionString();
}

/**
* @param self|string $version
* @return int (1 if $this > $version, -1 if $this < $version, 0 if equal)
Expand Down Expand Up @@ -379,4 +358,34 @@ public function withBuild($build)
{
return self::fromAllElements($this->major, $this->minor, $this->patch, $this->preRelease, $build);
}

/**
* @return string
*/
public function getVersionString()
{
return
$this->major
. '.' . $this->minor
. '.' . $this->patch
. ($this->isPreRelease() ? '-' . (string) $this->preRelease : '')
. ($this->isBuild() ? '+' . (string) $this->build : '')
;
}

/**
* @return string
*/
public function __toString()
{
return $this->getVersionString();
}

/**
* @return string
*/
public function jsonSerialize()
{
return $this->getVersionString();
}
}
44 changes: 26 additions & 18 deletions tests/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,6 @@ public static function versionStrings()
];
}

/**
* @dataProvider printedVersionStrings
*/
public function testVersionToStringConversion($output, Version $version)
{
$this->assertEquals($output, (string) $version);
}

public static function printedVersionStrings()
{
return [
['2.1.0', Version::fromPatch(2, 1, 0)],
['1.0.0+20150919', Version::fromString('1.0.0+20150919')],
['1.0.0+exp.sha.5114f85', Version::fromString('1.0.0+exp.sha.5114f85')],
['1.0.0-alpha.1+exp.sha.5114f85', Version::fromString('1.0.0-alpha.1+exp.sha.5114f85')],
];
}

public function testCreationFailsInCaseOfInvalidMajorVersion()
{
$this->setExpectedException(InvalidVersionElementException::class);
Expand Down Expand Up @@ -155,4 +137,30 @@ public function testCreationFromStringFailsInCaseInvalidCorePart()

Version::fromString('1.5.2.4.4');
}

/**
* @dataProvider printedVersionStrings
*/
public function testVersionToStringConversion($output, Version $version)
{
$this->assertEquals($output, (string) $version);
}

/**
* @dataProvider printedVersionStrings
*/
public function testJsonSerialization($output, Version $version)
{
$this->assertEquals('"' . $output . '"', json_encode($version));
}

public static function printedVersionStrings()
{
return [
['2.1.0', Version::fromPatch(2, 1, 0)],
['1.0.0+20150919', Version::fromString('1.0.0+20150919')],
['1.0.0+exp.sha.5114f85', Version::fromString('1.0.0+exp.sha.5114f85')],
['1.0.0-alpha.1+exp.sha.5114f85', Version::fromString('1.0.0-alpha.1+exp.sha.5114f85')],
];
}
}

0 comments on commit ed1e187

Please sign in to comment.