Skip to content

Commit c17ad1a

Browse files
committed
Commit trees
1 parent 2d5dc2f commit c17ad1a

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Command\Repository;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\CommitHandler")
9+
*/
10+
final class CommitCommand
11+
{
12+
/** @var string */
13+
private $repository;
14+
15+
/** @var string */
16+
private $message;
17+
18+
/** @var string */
19+
private $tree;
20+
21+
/** @var string[]|null */
22+
private $commit;
23+
24+
/**
25+
* @param string $repository
26+
* @param string $message
27+
* @param string $tree
28+
* @param string[]|null $commit
29+
*/
30+
public function __construct(string $repository, string $message, string $tree, ?string ...$commit)
31+
{
32+
$this->repository = $repository;
33+
$this->message = $message;
34+
$this->tree = $tree;
35+
$this->commit = $commit;
36+
}
37+
38+
/**
39+
* @return string
40+
*/
41+
public function getRepository(): string
42+
{
43+
return $this->repository;
44+
}
45+
46+
/**
47+
* @return string
48+
*/
49+
public function getMessage(): string
50+
{
51+
return $this->message;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getTree(): string
58+
{
59+
return $this->tree;
60+
}
61+
62+
/**
63+
* @return string[]|null
64+
*/
65+
public function getCommit(): ?array
66+
{
67+
return $this->commit;
68+
}
69+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\CommitCommand;
6+
use ApiClients\Client\Github\Resource\Git\CommitInterface;
7+
use ApiClients\Foundation\Hydrator\Hydrator;
8+
use ApiClients\Foundation\Transport\Service\RequestService;
9+
use ApiClients\Middleware\Json\JsonStream;
10+
use React\EventLoop\LoopInterface;
11+
use React\Promise\PromiseInterface;
12+
use RingCentral\Psr7\Request;
13+
14+
final class CommitHandler
15+
{
16+
/**
17+
* @var RequestService
18+
*/
19+
private $requestService;
20+
21+
/**
22+
* @var Hydrator
23+
*/
24+
private $hydrator;
25+
26+
/**
27+
* @var LoopInterface
28+
*/
29+
private $loop;
30+
31+
/**
32+
* @param RequestService $requestService
33+
* @param Hydrator $hydrator
34+
* @param LoopInterface $loop
35+
*/
36+
public function __construct(RequestService $requestService, Hydrator $hydrator, LoopInterface $loop)
37+
{
38+
$this->requestService = $requestService;
39+
$this->hydrator = $hydrator;
40+
$this->loop = $loop;
41+
}
42+
43+
/**
44+
* @param CommitCommand $command
45+
* @return PromiseInterface
46+
*/
47+
public function handle(CommitCommand $command): PromiseInterface
48+
{
49+
return $this->requestService->request(
50+
new Request(
51+
'POST',
52+
'repos/' . $command->getRepository() . '/git/commits',
53+
[],
54+
new JsonStream([
55+
'message' => $command->getMessage(),
56+
'tree' => $command->getTree(),
57+
'parents' => $command->getCommit(),
58+
])
59+
)
60+
)->then(function ($tree) {
61+
return $this->hydrator->hydrate(CommitInterface::HYDRATE_CLASS, $tree->getBody()->getParsedContents());
62+
});
63+
}
64+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\CommitCommand;
6+
use ApiClients\Client\Github\CommandBus\Handler\Repository\CommitHandler;
7+
use ApiClients\Client\Github\Resource\Git\CommitInterface;
8+
use ApiClients\Client\Github\Resource\Git\TreeInterface;
9+
use ApiClients\Foundation\Hydrator\Hydrator;
10+
use ApiClients\Foundation\Transport\Service\RequestService;
11+
use ApiClients\Middleware\Json\JsonStream;
12+
use ApiClients\Tools\TestUtilities\TestCase;
13+
use Prophecy\Argument;
14+
use Psr\Http\Message\RequestInterface;
15+
use React\EventLoop\Factory;
16+
use RingCentral\Psr7\Response;
17+
use function WyriHaximus\React\timedPromise;
18+
19+
/**
20+
* @internal
21+
*/
22+
final class CommitHandlerTest extends TestCase
23+
{
24+
public function provideCommands()
25+
{
26+
yield [
27+
function () {
28+
$loop = Factory::create();
29+
$command = new CommitCommand(
30+
'login/repo',
31+
'message',
32+
'ska punk metal',
33+
'foo',
34+
'bar',
35+
'baz'
36+
);
37+
$expectedJson = [
38+
'message' => 'message',
39+
'tree' => 'ska punk metal',
40+
'parents' => [
41+
'foo',
42+
'bar',
43+
'baz',
44+
],
45+
];
46+
47+
return [$loop, $command, $expectedJson];
48+
},
49+
];
50+
}
51+
52+
/**
53+
* @dataProvider provideCommands
54+
*/
55+
public function testCommand(callable $callable)
56+
{
57+
list($loop, $command, $expectedjson) = $callable();
58+
$json = [
59+
'foo' => 'bar',
60+
];
61+
$stream = null;
62+
$jsonStream = new JsonStream($json);
63+
64+
$tree = $this->prophesize(TreeInterface::class)->reveal();
65+
66+
$requestService = $this->prophesize(RequestService::class);
67+
$requestService->request(Argument::that(function (RequestInterface $request) use (&$stream) {
68+
$stream = $request->getBody()->getContents();
69+
70+
return true;
71+
}))->willReturn(timedPromise($loop, 1, new Response(
72+
200,
73+
[],
74+
$jsonStream
75+
)));
76+
77+
$hydrator = $this->prophesize(Hydrator::class);
78+
$hydrator->hydrate(CommitInterface::HYDRATE_CLASS, $json)->shouldBeCalled()->willReturn($tree);
79+
80+
$handler = new CommitHandler($requestService->reveal(), $hydrator->reveal(), $loop);
81+
82+
$result = $this->await($handler->handle($command), $loop);
83+
self::assertSame($tree, $result);
84+
self::assertSame($expectedjson, \json_decode($stream, true));
85+
}
86+
}

0 commit comments

Comments
 (0)