Added factory methods (#304)

* Added factory methods

* Removed urelated code

* Update test code
This commit is contained in:
Tobias Nyholm 2017-03-25 13:48:03 +01:00 committed by GitHub
parent 84a5c5dd4a
commit 1bfd40721e
6 changed files with 34 additions and 5 deletions

View File

@ -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

View File

@ -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',

View File

@ -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');

View File

@ -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,

View File

@ -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');

View File

@ -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',