From 1bfd40721ea935befe987fed069e1817e4c203f7 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 25 Mar 2017 13:48:03 +0100 Subject: [PATCH] Added factory methods (#304) * Added factory methods * Removed urelated code * Update test code --- src/Mailgun/Mailgun.php | 29 +++++++++++++++++++++++++ tests/Functional/FileFromMemoryTest.php | 2 +- tests/Functional/InlineFileTest.php | 2 +- tests/Functional/MockedMailgun.php | 2 +- tests/Functional/NoSamePostNameTest.php | 2 +- tests/Functional/SendMessageTest.php | 2 +- 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 9cd56c1..bccb7b5 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -92,6 +92,35 @@ class Mailgun $this->hydrator = $hydrator ?: new ModelHydrator(); } + /** + * @param HttpClientConfigurator $httpClientConfigurator + * @param Hydrator|null $hydrator + * @param RequestBuilder|null $requestBuilder + * + * @return Mailgun + */ + public static function configure( + HttpClientConfigurator $httpClientConfigurator, + Hydrator $hydrator = null, + RequestBuilder $requestBuilder = null + ) { + $httpClient = $httpClientConfigurator->createConfiguredClient(); + + return new self($httpClient, $hydrator, $requestBuilder); + } + + /** + * @param string $apiKey + * + * @return Mailgun + */ + public static function create($apiKey) + { + $httpClientConfigurator = (new HttpClientConfigurator())->setApiKey($apiKey); + + return self::configure($httpClientConfigurator); + } + /** * This function allows the sending of a fully formed message OR a custom * MIME string. If sending MIME, the string must be passed in to the 3rd diff --git a/tests/Functional/FileFromMemoryTest.php b/tests/Functional/FileFromMemoryTest.php index 548b1d2..d055783 100644 --- a/tests/Functional/FileFromMemoryTest.php +++ b/tests/Functional/FileFromMemoryTest.php @@ -44,7 +44,7 @@ class FileFromMemoryTest extends \PHPUnit_Framework_TestCase ['filePath' => './tests/TestAssets/text_file.txt', 'remoteName' => 'text_file.txt'], ]; - $mailgun = MockedMailgun::create($this, 'POST', 'domain/messages', [], $fileValidator); + $mailgun = MockedMailgun::createMock($this, 'POST', 'domain/messages', [], $fileValidator); $mailgun->sendMessage('domain', [ 'from' => 'bob@example.com', diff --git a/tests/Functional/InlineFileTest.php b/tests/Functional/InlineFileTest.php index 3023a39..ffd5636 100644 --- a/tests/Functional/InlineFileTest.php +++ b/tests/Functional/InlineFileTest.php @@ -36,7 +36,7 @@ class InlineFileTest extends \PHPUnit_Framework_TestCase }; // Create the mocked mailgun client. We use $this->assertEquals on $method, $uri and $body parameters. - $mailgun = MockedMailgun::create($this, 'POST', 'domain/messages', [], $fileValidator); + $mailgun = MockedMailgun::createMock($this, 'POST', 'domain/messages', [], $fileValidator); $builder = $mailgun->MessageBuilder(); $builder->setFromAddress('bob@example.com'); diff --git a/tests/Functional/MockedMailgun.php b/tests/Functional/MockedMailgun.php index 852b58c..7d72d1a 100644 --- a/tests/Functional/MockedMailgun.php +++ b/tests/Functional/MockedMailgun.php @@ -44,7 +44,7 @@ final class MockedMailgun extends Mailgun * @param \Closure|array $filesValidator * @param \Closure|array $headersValidator */ - public static function create( + public static function createMock( \PHPUnit_Framework_TestCase $testCase, $methodValidator, $uriValidator, diff --git a/tests/Functional/NoSamePostNameTest.php b/tests/Functional/NoSamePostNameTest.php index abf1776..dd116d1 100644 --- a/tests/Functional/NoSamePostNameTest.php +++ b/tests/Functional/NoSamePostNameTest.php @@ -28,7 +28,7 @@ class NoSamePostNameTest extends \PHPUnit_Framework_TestCase }; // Create the mocked mailgun client. We use $this->assertEquals on $method, $uri and $body parameters. - $mailgun = MockedMailgun::create($this, 'POST', 'domain/messages', [], $fileValidator); + $mailgun = MockedMailgun::createMock($this, 'POST', 'domain/messages', [], $fileValidator); $builder = $mailgun->MessageBuilder(); $builder->setFromAddress('bob@example.com'); diff --git a/tests/Functional/SendMessageTest.php b/tests/Functional/SendMessageTest.php index 6cfa815..40e8ecd 100644 --- a/tests/Functional/SendMessageTest.php +++ b/tests/Functional/SendMessageTest.php @@ -27,7 +27,7 @@ class SendMessageTest extends \PHPUnit_Framework_TestCase }; // Create the mocked mailgun client. We use $this->assertEquals on $method, $uri and $body parameters. - $mailgun = MockedMailgun::create($this, 'POST', 'domain/messages', [], $fileValidator); + $mailgun = MockedMailgun::createMock($this, 'POST', 'domain/messages', [], $fileValidator); $mailgun->sendMessage('domain', [ 'from' => 'bob@example.com',