Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d5dc2f
commit c17ad1a
Showing
3 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |