Skip to content

Commit

Permalink
Merge pull request #12 from ergebnis/feature/version-collector
Browse files Browse the repository at this point in the history
Enhancement: Implement `Markdown\VersionCollector`
  • Loading branch information
localheinz committed Mar 29, 2024
2 parents d074fbc + bd82cef commit 9e9e5e8
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 73 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`88cf670...main`][88cf670...main].

### Added

- Added `Markdown\VersionCollector` ([#12]), by [@localheinz]

[88cf670...main]: https://github.com/ergebnis/keep-a-changelog/compare/88cf670...main

[#12]: https://github.com/ergebnis/keep-a-changelog/pull/12

[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"symbol-whitelist": [
"self",
"array",
"string"
]
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"security": "https://github.com/ergebnis/keep-a-changelog/blob/main/.github/SECURITY.md"
},
"require": {
"php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0"
"php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0",
"ergebnis/version": "^1.1.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.28.3",
Expand Down
75 changes: 73 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 0 additions & 34 deletions src/Example.php

This file was deleted.

47 changes: 47 additions & 0 deletions src/Markdown/VersionCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/keep-a-changelog
*/

namespace Ergebnis\KeepAChangelog\Markdown;

use Ergebnis\Version;

final class VersionCollector
{
/**
* @return list<Version\Version>
*/
public function collect(string $content): array
{
$versions = [];

foreach (\explode("\n", $content) as $line) {
if (0 === \preg_match('/^## \[`(?P<version>[^`]+)`]/', $line, $matches)) {
continue;
}

try {
$version = Version\Version::fromString($matches['version']);
} catch (Version\Exception\InvalidVersion $exception) {
continue;
}

$versions[] = $version;
}

\usort($versions, static function (Version\Version $a, Version\Version $b): int {
return $a->compare($b);
});

return $versions;
}
}
51 changes: 51 additions & 0 deletions test/Fixture/Markdown/VersionCollector/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

For a full diff see [`1.1.0...main`][1.1.0...main].

## [`1.1.0`][1.1.0]

For a full diff see [`1.0.0...1.1.0`][1.0.0...1.1.0].

### Added

- Added support for PHP 8.0 ([#77]), by [@localheinz]
- Added support for PHP 7.4 ([#78]), by [@localheinz]

## [`Not-a-version`][1.0.0]

## [`1.0.0`][1.0.0]

For a full diff see [`64ced12...1.0.0`][64ced12...1.0.0].

### Added

- Added `Version` as a value object ([#1]), by [@localheinz]
- Added `Major` as a value object ([#3]), by [@localheinz]
- Added `Minor` as a value object ([#4]), by [@localheinz]
- Added `Patch` as a value object ([#5]), by [@localheinz]
- Added `PreRelease` as a value object ([#8]), by [@localheinz]
- Added `BuildMetaData` as a value object ([#9]), by [@localheinz]

## [`Also-not-a-version`]

[1.0.0]: https://github.com/ergebnis/version/releases/tag/1.0.0

[64ced12...1.0.0]: https://github.com/ergebnis/version/compare/64ced12...1.0.0
[1.0.0...1.1.0]: https://github.com/ergebnis/version/compare/1.0.0...1.1.0
[1.1.0...main]: https://github.com/ergebnis/version/compare/1.1.0...main

[#1]: https://github.com/ergebnis/version/pull/1
[#3]: https://github.com/ergebnis/version/pull/3
[#4]: https://github.com/ergebnis/version/pull/4
[#5]: https://github.com/ergebnis/version/pull/5
[#8]: https://github.com/ergebnis/version/pull/8
[#9]: https://github.com/ergebnis/version/pull/9
[#77]: https://github.com/ergebnis/version/pull/77

[@localheinz]: https://github.com/localheinz
35 changes: 0 additions & 35 deletions test/Unit/ExampleTest.php

This file was deleted.

65 changes: 65 additions & 0 deletions test/Unit/Markdown/VersionCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/keep-a-changelog
*/

namespace Ergebnis\KeepAChangelog\Test\Unit\Markdown;

use Ergebnis\KeepAChangelog\Markdown;
use Ergebnis\KeepAChangelog\Test;
use Ergebnis\Version;
use PHPUnit\Framework;

/**
* @covers \Ergebnis\KeepAChangelog\Markdown\VersionCollector
*/
final class VersionCollectorTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testCollectReturnsEmptyArrayWhenContentIsEmptyString(): void
{
$content = '';

$versionCollector = new Markdown\VersionCollector();

$collected = $versionCollector->collect($content);

self::assertSame([], $collected);
}

public function testCollectReturnsEmptyArrayWhenContentDoesNotContainAnyVersions(): void
{
$content = self::faker()->realText();

$versionCollector = new Markdown\VersionCollector();

$collected = $versionCollector->collect($content);

self::assertSame([], $collected);
}

public function testCollectArrayWithVersionWhenContentContainsVersions(): void
{
$content = \file_get_contents(__DIR__ . '/../../Fixture/Markdown/VersionCollector/CHANGELOG.md');

$versionCollector = new Markdown\VersionCollector();

$collected = $versionCollector->collect($content);

$expected = [
Version\Version::fromString('1.0.0'),
Version\Version::fromString('1.1.0'),
];

self::assertEquals($expected, $collected);
}
}

0 comments on commit 9e9e5e8

Please sign in to comment.