Skip to content

Commit dbb009e

Browse files
committed
Add a new changelog:current command
References #8
1 parent 65985fb commit dbb009e

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/ChangelogServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MarkWalet\Changelog\Adapters\XmlFeatureAdapter;
1010
use MarkWalet\Changelog\Adapters\XmlReleaseAdapter;
1111
use MarkWalet\Changelog\Commands\ChangelogAddCommand;
12+
use MarkWalet\Changelog\Commands\ChangelogCurrentCommand;
1213
use MarkWalet\Changelog\Commands\ChangelogGenerateCommand;
1314
use MarkWalet\Changelog\Commands\ChangelogInstallCommand;
1415
use MarkWalet\Changelog\Commands\ChangelogListCommand;
@@ -71,6 +72,7 @@ private function bootCommands(): void
7172
if ($this->app->runningInConsole()) {
7273
$this->commands([
7374
ChangelogInstallCommand::class,
75+
ChangelogCurrentCommand::class,
7476
ChangelogAddCommand::class,
7577
ChangelogListCommand::class,
7678
ChangelogUnreleasedCommand::class,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace MarkWalet\Changelog\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use MarkWalet\Changelog\Adapters\FeatureAdapter;
7+
use MarkWalet\GitState\Drivers\GitDriver;
8+
9+
class ChangelogCurrentCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'changelog:current';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Show a list of unreleased changes for the current branch.';
24+
25+
/**
26+
* Execute the command.
27+
*
28+
* @param FeatureAdapter $adapter
29+
* @param GitDriver $gitState
30+
*/
31+
public function handle(FeatureAdapter $adapter, GitDriver $gitState)
32+
{
33+
$branch = $gitState->currentBranch();
34+
$path = config('changelog.path').DIRECTORY_SEPARATOR.'unreleased'.DIRECTORY_SEPARATOR.$branch.'.xml';
35+
36+
if (! $adapter->exists($path)) {
37+
$this->warn("No changes found for $branch");
38+
39+
return;
40+
}
41+
$feature = $adapter->read($path);
42+
$this->line("Changes for $branch:");
43+
44+
foreach ($feature->changes() as $change) {
45+
$type = ucfirst($change->type());
46+
$this->line(" - $type: {$change->message()}");
47+
}
48+
}
49+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace MarkWalet\Changelog\Tests\Commands;
4+
5+
use MarkWalet\Changelog\Adapters\FeatureAdapter;
6+
use MarkWalet\Changelog\Change;
7+
use MarkWalet\Changelog\Feature;
8+
use MarkWalet\Changelog\Tests\LaravelTestCase;
9+
use MarkWalet\GitState\Drivers\GitDriver;
10+
use Mockery\MockInterface;
11+
12+
class ChangelogCurrentCommandTest extends LaravelTestCase
13+
{
14+
/**
15+
* Setup the test environment.
16+
*
17+
* @return void
18+
*/
19+
protected function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
config()->set('changelog.path', 'fake-path');
24+
$this->mock(GitDriver::class, function (MockInterface $git) {
25+
$git->expects('currentBranch')->once()->andReturn('test-branch');
26+
});
27+
}
28+
29+
/** @test */
30+
public function it_can_list_all_changes_for_all_releases()
31+
{
32+
$this->mock(FeatureAdapter::class, function (MockInterface $adapter) {
33+
$adapter->allows('exists')->andReturnUsing(function (string $branch) {
34+
return $branch === 'fake-path/unreleased/test-branch.xml';
35+
});
36+
37+
$adapter->allows('read')->andReturn(tap(new Feature(), function (Feature $feature) {
38+
$feature->add(new Change('added', 'Added a new feature.'));
39+
$feature->add(new Change('fixed', 'Fixed a bug.'));
40+
}));
41+
});
42+
43+
$this->artisan('changelog:current')
44+
->expectsOutput('Changes for test-branch:')
45+
->expectsOutput(' - Added: Added a new feature.')
46+
->expectsOutput(' - Fixed: Fixed a bug.');
47+
}
48+
49+
/** @test */
50+
public function it_shows_a_warning_when_there_are_no_unreleased_changes_found_yet(): void
51+
{
52+
config()->set('changelog.path', 'fake-path');
53+
$this->mock(FeatureAdapter::class, function (MockInterface $adapter) {
54+
$adapter->allows('exists')->andReturn(false);
55+
});
56+
57+
$this->artisan('changelog:current')
58+
->expectsOutput('No changes found for test-branch');
59+
}
60+
}

0 commit comments

Comments
 (0)