Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from fabiothiroki/adapter
Add adapter
- Loading branch information
Showing
5 changed files
with
87 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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace app\structural\adapter; | ||
|
||
interface EmailSenderAdapter | ||
{ | ||
public function sendEmail(string $recipient, string $content): void; | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace app\structural\adapter; | ||
|
||
class ThirdPartyEmailClient | ||
{ | ||
public function __construct( | ||
private string $apiKey, | ||
private string $region, | ||
) { | ||
} | ||
|
||
public function sendEmail( | ||
string $recipient, | ||
string $content, | ||
): void { | ||
echo sprintf("Using apiKey %s and region %s", $this->apiKey, $this->region); | ||
echo sprintf("Sending email to %s and content %s", $recipient, $content); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace app\structural\adapter; | ||
|
||
final class ThirdPartyEmailSenderAdapter implements EmailSenderAdapter | ||
{ | ||
public function __construct(private ThirdPartyEmailClient $emailClient) | ||
{ | ||
} | ||
|
||
public function sendEmail(string $recipient, string $content): void | ||
{ | ||
$this->emailClient->sendEmail($recipient, $content); | ||
} | ||
} |
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,3 @@ | ||
[![](https://mermaid.ink/img/eyJjb2RlIjoic2VxdWVuY2VEaWFncmFtXG4gICAgU2lnblVwU2VydmljZS0tPj5UaGlyZFBhcnR5RW1haWxDbGllbnQ6IEkgd2FudCB0byBzZW5kIGFuIGVtYWlsIVxuICAgIFRoaXJkUGFydHlFbWFpbENsaWVudC0tPj5TaWduVXBTZXJ2aWNlOiBDb29sLCBnaXZlIG1lIGZpcnN0IGFuIGFwaUtleSBhbmQgYSByZWdpb24hXG4gICAgU2lnblVwU2VydmljZS0tPj5UaGlyZFBhcnR5RW1haWxDbGllbnQ6IEJ1dCBJIGp1c3QgaGF2ZSB0aGUgcmVjaXBpZW50IGFuZCBjb250ZW50ID0vXG4gICAgICAgICAgICAiLCJtZXJtYWlkIjp7InRoZW1lIjoiZGVmYXVsdCJ9LCJ1cGRhdGVFZGl0b3IiOmZhbHNlLCJhdXRvU3luYyI6dHJ1ZSwidXBkYXRlRGlhZ3JhbSI6ZmFsc2V9)](https://mermaid-js.github.io/mermaid-live-editor/edit/#eyJjb2RlIjoic2VxdWVuY2VEaWFncmFtXG4gICAgU2lnblVwU2VydmljZS0tPj5UaGlyZFBhcnR5RW1haWxDbGllbnQ6IEkgd2FudCB0byBzZW5kIGFuIGVtYWlsIVxuICAgIFRoaXJkUGFydHlFbWFpbENsaWVudC0tPj5TaWduVXBTZXJ2aWNlOiBDb29sLCBnaXZlIG1lIGZpcnN0IGFuIGFwaUtleSBhbmQgYSByZWdpb24hXG4gICAgU2lnblVwU2VydmljZS0tPj5UaGlyZFBhcnR5RW1haWxDbGllbnQ6IEJ1dCBJIGp1c3QgaGF2ZSB0aGUgcmVjaXBpZW50IGFuZCBjb250ZW50ID0vXG4gICAgICAgICAgICAiLCJtZXJtYWlkIjoie1xuICBcInRoZW1lXCI6IFwiZGVmYXVsdFwi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xTZW5kZXJBZGFwdGVyOiBPaCwgdGhhdCdzIGV4YWN0bHkgd2hhdCBJIGhhdmUhXG4gICAgVGhpcmRQYXJ0eUVtYWlsU2VuZGVyQWRhcHRlci0tPj5UaGlyZFBhcnR5RW1haWxDbGllbnQ6IFBsZWFzZSBzZW5kIHRoaXMgZW1haWxcbiAgICAgICAgICAgICIsIm1lcm1haWQiOiJ7XG4gIFwidGhlbWVcIjogXCJkZWZhdWx0XCJcbn0iLCJ1cGRhdGVFZGl0b3IiOmZhbHNlLCJhdXRvU3luYyI6dHJ1ZSwidXBkYXRlRGlhZ3JhbSI6ZmFsc2V9) |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace structural\adapter; | ||
|
||
use app\structural\adapter\ThirdPartyEmailSenderAdapter; | ||
use app\structural\adapter\ThirdPartyEmailClient; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AdapterTest extends TestCase | ||
{ | ||
public function testThirdPartyEmailSenderAdapterAdapterUsesCorrectClient(): void | ||
{ | ||
$emailClient = $this->createMock(ThirdPartyEmailClient::class); | ||
|
||
$emailAdapter = new ThirdPartyEmailSenderAdapter($emailClient); | ||
|
||
$emailClient->expects($this->once()) | ||
->method('sendEmail'); | ||
|
||
$emailAdapter->sendEmail( | ||
'email@email.com', | ||
'I love design patterns'); | ||
} | ||
|
||
public function testThirdPartyClient(): void | ||
{ | ||
$emailClient = new ThirdPartyEmailClient('apiKey', 'region'); | ||
$emailClient->sendEmail('email@email.com', 'I love design patterns'); | ||
|
||
$this->expectOutputRegex('/Using apiKey apiKey and region region/'); | ||
$this->expectOutputRegex('/Sending email to email@email.com and content I love design patterns/'); | ||
} | ||
} |