Skip to content

Commit

Permalink
Commit trees
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Feb 12, 2019
1 parent 2d5dc2f commit c17ad1a
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/CommandBus/Command/Repository/CommitCommand.php
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Command\Repository;

use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;

/**
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\CommitHandler")
*/
final class CommitCommand
{
/** @var string */
private $repository;

/** @var string */
private $message;

/** @var string */
private $tree;

/** @var string[]|null */
private $commit;

/**
* @param string $repository
* @param string $message
* @param string $tree
* @param string[]|null $commit
*/
public function __construct(string $repository, string $message, string $tree, ?string ...$commit)
{
$this->repository = $repository;
$this->message = $message;
$this->tree = $tree;
$this->commit = $commit;
}

/**
* @return string
*/
public function getRepository(): string
{
return $this->repository;
}

/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}

/**
* @return string
*/
public function getTree(): string
{
return $this->tree;
}

/**
* @return string[]|null
*/
public function getCommit(): ?array
{
return $this->commit;
}
}
64 changes: 64 additions & 0 deletions src/CommandBus/Handler/Repository/CommitHandler.php
@@ -0,0 +1,64 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Handler\Repository;

use ApiClients\Client\Github\CommandBus\Command\Repository\CommitCommand;
use ApiClients\Client\Github\Resource\Git\CommitInterface;
use ApiClients\Foundation\Hydrator\Hydrator;
use ApiClients\Foundation\Transport\Service\RequestService;
use ApiClients\Middleware\Json\JsonStream;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use RingCentral\Psr7\Request;

final class CommitHandler
{
/**
* @var RequestService
*/
private $requestService;

/**
* @var Hydrator
*/
private $hydrator;

/**
* @var LoopInterface
*/
private $loop;

/**
* @param RequestService $requestService
* @param Hydrator $hydrator
* @param LoopInterface $loop
*/
public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop)
{
$this->requestService = $requestService;
$this->hydrator = $hydrator;
$this->loop = $loop;
}

/**
* @param CommitCommand $command
* @return PromiseInterface
*/
public function handle(CommitCommand $command): PromiseInterface
{
return $this->requestService->request(
new Request(
'POST',
'repos/' . $command->getRepository() . '/git/commits',
[],
new JsonStream([
'message' => $command->getMessage(),
'tree' => $command->getTree(),
'parents' => $command->getCommit(),
])
)
)->then(function ($tree) {
return $this->hydrator->hydrate(CommitInterface::HYDRATE_CLASS, $tree->getBody()->getParsedContents());
});
}
}
86 changes: 86 additions & 0 deletions tests/CommandBus/Handler/Repository/CommitHandlerTest.php
@@ -0,0 +1,86 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Github\CommandBus\Handler\Repository;

use ApiClients\Client\Github\CommandBus\Command\Repository\CommitCommand;
use ApiClients\Client\Github\CommandBus\Handler\Repository\CommitHandler;
use ApiClients\Client\Github\Resource\Git\CommitInterface;
use ApiClients\Client\Github\Resource\Git\TreeInterface;
use ApiClients\Foundation\Hydrator\Hydrator;
use ApiClients\Foundation\Transport\Service\RequestService;
use ApiClients\Middleware\Json\JsonStream;
use ApiClients\Tools\TestUtilities\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use React\EventLoop\Factory;
use RingCentral\Psr7\Response;
use function WyriHaximus\React\timedPromise;

/**
* @internal
*/
final class CommitHandlerTest extends TestCase
{
public function provideCommands()
{
yield [
function () {
$loop = Factory::create();
$command = new CommitCommand(
'login/repo',
'message',
'ska punk metal',
'foo',
'bar',
'baz'
);
$expectedJson = [
'message' => 'message',
'tree' => 'ska punk metal',
'parents' => [
'foo',
'bar',
'baz',
],
];

return [$loop, $command, $expectedJson];
},
];
}

/**
* @dataProvider provideCommands
*/
public function testCommand(callable $callable)
{
list($loop, $command, $expectedjson) = $callable();
$json = [
'foo' => 'bar',
];
$stream = null;
$jsonStream = new JsonStream($json);

$tree = $this->prophesize(TreeInterface::class)->reveal();

$requestService = $this->prophesize(RequestService::class);
$requestService->request(Argument::that(function (RequestInterface $request) use (&$stream) {
$stream = $request->getBody()->getContents();

return true;
}))->willReturn(timedPromise($loop, 1, new Response(
200,
[],
$jsonStream
)));

$hydrator = $this->prophesize(Hydrator::class);
$hydrator->hydrate(CommitInterface::HYDRATE_CLASS, $json)->shouldBeCalled()->willReturn($tree);

$handler = new CommitHandler($requestService->reveal(), $hydrator->reveal(), $loop);

$result = $this->await($handler->handle($command), $loop);
self::assertSame($tree, $result);
self::assertSame($expectedjson, \json_decode($stream, true));
}
}

0 comments on commit c17ad1a

Please sign in to comment.