Skip to content

Commit

Permalink
Merge pull request #165 from alexraputa/create-milestones-test
Browse files Browse the repository at this point in the history
Closes #147: Add test for `CreateMilestones` command
  • Loading branch information
Ocramius authored Oct 3, 2021
2 parents c7260cc + 6d66bd3 commit 5bdec89
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions test/unit/Application/CreateMilestonesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

namespace Laminas\AutomaticReleases\Test\Unit\Application;

use Laminas\AutomaticReleases\Application\Command\CreateMilestones;
use Laminas\AutomaticReleases\Git\Value\SemVerVersion;
use Laminas\AutomaticReleases\Github\Api\V3\CreateMilestone;
use Laminas\AutomaticReleases\Github\Api\V3\CreateMilestoneFailed;
use Laminas\AutomaticReleases\Github\Event\Factory\LoadCurrentGithubEvent;
use Laminas\AutomaticReleases\Github\Event\MilestoneClosedEvent;
use Laminas\AutomaticReleases\Github\Value\RepositoryName;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

/**
* @covers \Laminas\AutomaticReleases\Application\Command\CreateMilestones
*/
final class CreateMilestonesTest extends TestCase
{
/** @var LoadCurrentGithubEvent&MockObject */
private LoadCurrentGithubEvent $loadEvent;

/** @var CreateMilestone&MockObject */
private CreateMilestone $createMilestone;

private CreateMilestones $command;

private MilestoneClosedEvent $event;

private SemVerVersion $releaseVersion;

protected function setUp(): void
{
parent::setUp();

$this->loadEvent = $this->createMock(LoadCurrentGithubEvent::class);
$this->createMilestone = $this->createMock(CreateMilestone::class);

$this->command = new CreateMilestones(
$this->loadEvent,
$this->createMilestone
);

$this->event = MilestoneClosedEvent::fromEventJson(<<<'JSON'
{
"milestone": {
"title": "1.2.3",
"number": 123
},
"repository": {
"full_name": "foo/bar"
},
"action": "closed"
}
JSON
);

$this->releaseVersion = SemVerVersion::fromMilestoneName('1.2.3');
}

public function testCommandName(): void
{
self::assertSame('laminas:automatic-releases:create-milestones', $this->command->getName());
}

public function testWillCreate(): void
{
$this->loadEvent
->expects(self::once())
->method('__invoke')
->willReturn($this->event);

$this->createMilestone
->expects(self::exactly(3))
->method('__invoke')
->withConsecutive(
[
self::equalTo(RepositoryName::fromFullName('foo/bar')),
self::equalTo($this->releaseVersion->nextPatch()),
],
[
self::equalTo(RepositoryName::fromFullName('foo/bar')),
self::equalTo($this->releaseVersion->nextMinor()),
],
[
self::equalTo(RepositoryName::fromFullName('foo/bar')),
self::equalTo($this->releaseVersion->nextMajor()),
],
);

self::assertSame(0, $this->command->execute(new ArrayInput([]), new NullOutput()));
}

public function testWillFailed(): void
{
$this->loadEvent
->expects(self::once())
->method('__invoke')
->willReturn($this->event);

$this->createMilestone
->expects(self::exactly(3))
->method('__invoke')
->withConsecutive(
[
self::equalTo(RepositoryName::fromFullName('foo/bar')),
self::equalTo($this->releaseVersion->nextPatch()),
],
[
self::equalTo(RepositoryName::fromFullName('foo/bar')),
self::equalTo($this->releaseVersion->nextMinor()),
],
[
self::equalTo(RepositoryName::fromFullName('foo/bar')),
self::equalTo($this->releaseVersion->nextMajor()),
],
)
->willReturnOnConsecutiveCalls(
self::throwException(
CreateMilestoneFailed::forVersion($this->releaseVersion->nextPatch()->fullReleaseName()),
),
self::throwException(
CreateMilestoneFailed::forVersion($this->releaseVersion->nextMinor()->fullReleaseName()),
),
self::throwException(
CreateMilestoneFailed::forVersion($this->releaseVersion->nextMajor()->fullReleaseName()),
),
);

self::assertSame(0, $this->command->execute(new ArrayInput([]), new NullOutput()));
}
}

0 comments on commit 5bdec89

Please sign in to comment.