Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #147: Add test for CreateMilestones command #165

Merged
merged 3 commits into from
Oct 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
alexraputa marked this conversation as resolved.
Show resolved Hide resolved
{
/** @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()));
}
}