Skip to content

Commit

Permalink
First iteration of SendProposal feature
Browse files Browse the repository at this point in the history
  • Loading branch information
franiglesias committed May 5, 2024
1 parent 90059a2 commit 5bb4ce2
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/ForSendProposals/SendProposal/SendProposal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare (strict_types=1);

namespace App\ForSendProposals\SendProposal;


final readonly class SendProposal
{
public function __construct(
public string $title,
public string $description,
public string $name,
public string $email,
public string $type,
public bool $sponsored,
public string $location
)
{
}
}
14 changes: 14 additions & 0 deletions src/ForSendProposals/SendProposal/SendProposalHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare (strict_types=1);

namespace App\ForSendProposals\SendProposal;


class SendProposalHandler
{
public function __invoke(SendProposal $command): SendProposalResponse
{
throw new \RuntimeException('Implement __invoke() method.');
}
}
16 changes: 16 additions & 0 deletions src/ForSendProposals/SendProposal/SendProposalResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare (strict_types=1);

namespace App\ForSendProposals\SendProposal;


final readonly class SendProposalResponse
{
public function __construct(
public bool $success,
public string $id,
public string $title)
{
}
}
21 changes: 21 additions & 0 deletions tests/ForSendProposals/SendProposal/PayloadExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare (strict_types=1);

namespace App\Tests\ForSendProposals\SendProposal;

class PayloadExample
{
public static function wellFormedWithTitle(string $title): array
{
return [
'title' => $title,
'description' => 'A description or abstract of the proposal',
'author' => 'Fran Iglesias',
'email' => 'fran.iglesias@example.com',
'type' => 'talk',
'sponsored' => true,
'location' => 'Vigo, Galicia',
];
}
}
67 changes: 67 additions & 0 deletions tests/ForSendProposals/SendProposal/SendProposalControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare (strict_types=1);

namespace App\Tests\ForSendProposals\SendProposal;

use App\ForSendProposals\SendProposal\SendProposal;
use App\ForSendProposals\SendProposal\SendProposalController;
use App\ForSendProposals\SendProposal\SendProposalHandler;
use App\ForSendProposals\SendProposal\SendProposalResponse;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertStringContainsString;

final class SendProposalControllerTest extends TestCase
{
/** @test */
public function should_accept_well_formed_proposal(): void
{
$payload = PayloadExample::wellFormedWithTitle('Proposal Title');

$request = Request::create(
'/api/proposals',
'POST',
[],
[],
[],
['CONTENT-TYPE' => 'json/application'],
json_encode($payload)
);
$handler = $this->createMock(SendProposalHandler::class);

$command = new SendProposal(
'Proposal Title',
'A description or abstract of the proposal',
'Fran Iglesias',
'fran.iglesias@example.com',
'talk',
true,
'Vigo, Galicia',
);

$expected = new SendProposalResponse(
true,
'proposal-id',
'Proposal Title'
);

$handler
->method('__invoke')
->with($command)
->willReturn($expected);

$controller = new SendProposalController($handler);
$response = ($controller)($request);

assertEquals(202, $response->getStatusCode());
assertEquals('https://localhost/api/proposals/proposal-id', $response->headers->get("Location"));

$content = json_decode($response->getContent(), true);
assertStringContainsString(
'Your proposal titled "Proposal Title" was registered.',
$content['message']
);
}
}

0 comments on commit 5bb4ce2

Please sign in to comment.