diff --git a/.gitignore b/.gitignore index 0ae4aa4..5105a49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.idea + /vendor .DS_Store composer.lock diff --git a/composer.json b/composer.json index 12feeeb..943d803 100644 --- a/composer.json +++ b/composer.json @@ -24,13 +24,18 @@ }, "require": { "php-http/mock-client": "^1.1", - "guzzlehttp/psr7": "^1", - "mockery/mockery": "^1", - "phpunit/phpunit": "^5.7|^6" + "php-http/guzzle6-adapter": "^1.1.1|^2" }, "require-dev": { "omnipay/common": "^3", - "symfony/http-foundation": "^3|^4" + "phpunit/phpunit": "^5.7|^6|^7|^8|^9", + "mockery/mockery": "^1", + "symfony/http-foundation": "^3|^4|^5" + }, + "suggest": { + "omnipay/common": "^3", + "phpunit/phpunit": "^6|^7|^8|^9", + "mockery/mockery": "^1" }, "extra": { "branch-alias": { diff --git a/src/GatewayTestCase.php b/src/GatewayTestCase.php index cf0240a..0de90e4 100644 --- a/src/GatewayTestCase.php +++ b/src/GatewayTestCase.php @@ -2,389 +2,14 @@ namespace Omnipay\Tests; -use Omnipay\Common\AbstractGateway; -use Omnipay\Common\Message\RequestInterface; +use PHPUnit\Framework\TestCase as PHPUnitTestCase; /** * Base Gateway Test class * * Ensures all gateways conform to consistent standards */ -abstract class GatewayTestCase extends TestCase +abstract class GatewayTestCase extends PHPUnitTestCase { - /** @var AbstractGateway */ - protected $gateway; - - public function testGetNameNotEmpty() - { - $name = $this->gateway->getName(); - $this->assertNotEmpty($name); - $this->assertInternalType('string', $name); - } - - public function testGetShortNameNotEmpty() - { - $shortName = $this->gateway->getShortName(); - $this->assertNotEmpty($shortName); - $this->assertInternalType('string', $shortName); - } - - public function testGetDefaultParametersReturnsArray() - { - $settings = $this->gateway->getDefaultParameters(); - $this->assertInternalType('array', $settings); - } - - public function testDefaultParametersHaveMatchingMethods() - { - $settings = $this->gateway->getDefaultParameters(); - foreach ($settings as $key => $default) { - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - - $this->assertTrue(method_exists($this->gateway, $getter), "Gateway must implement $getter()"); - $this->assertTrue(method_exists($this->gateway, $setter), "Gateway must implement $setter()"); - - // setter must return instance - $this->assertSame($this->gateway, $this->gateway->$setter($value)); - $this->assertSame($value, $this->gateway->$getter()); - } - } - - public function testTestMode() - { - $this->assertSame($this->gateway, $this->gateway->setTestMode(false)); - $this->assertSame(false, $this->gateway->getTestMode()); - - $this->assertSame($this->gateway, $this->gateway->setTestMode(true)); - $this->assertSame(true, $this->gateway->getTestMode()); - } - - public function testCurrency() - { - // currency is normalized to uppercase - $this->assertSame($this->gateway, $this->gateway->setCurrency('eur')); - $this->assertSame('EUR', $this->gateway->getCurrency()); - } - - public function testSupportsAuthorize() - { - $supportsAuthorize = $this->gateway->supportsAuthorize(); - $this->assertInternalType('boolean', $supportsAuthorize); - - if ($supportsAuthorize) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->authorize()); - } else { - $this->assertFalse(method_exists($this->gateway, 'authorize')); - } - } - - public function testSupportsCompleteAuthorize() - { - $supportsCompleteAuthorize = $this->gateway->supportsCompleteAuthorize(); - $this->assertInternalType('boolean', $supportsCompleteAuthorize); - - if ($supportsCompleteAuthorize) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->completeAuthorize()); - } else { - $this->assertFalse(method_exists($this->gateway, 'completeAuthorize')); - } - } - - public function testSupportsCapture() - { - $supportsCapture = $this->gateway->supportsCapture(); - $this->assertInternalType('boolean', $supportsCapture); - - if ($supportsCapture) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->capture()); - } else { - $this->assertFalse(method_exists($this->gateway, 'capture')); - } - } - - public function testSupportsPurchase() - { - $supportsPurchase = $this->gateway->supportsPurchase(); - $this->assertInternalType('boolean', $supportsPurchase); - - if ($supportsPurchase) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->purchase()); - } else { - $this->assertFalse(method_exists($this->gateway, 'purchase')); - } - } - - public function testSupportsCompletePurchase() - { - $supportsCompletePurchase = $this->gateway->supportsCompletePurchase(); - $this->assertInternalType('boolean', $supportsCompletePurchase); - - if ($supportsCompletePurchase) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->completePurchase()); - } else { - $this->assertFalse(method_exists($this->gateway, 'completePurchase')); - } - } - - public function testSupportsRefund() - { - $supportsRefund = $this->gateway->supportsRefund(); - $this->assertInternalType('boolean', $supportsRefund); - - if ($supportsRefund) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->refund()); - } else { - $this->assertFalse(method_exists($this->gateway, 'refund')); - } - } - - public function testSupportsVoid() - { - $supportsVoid = $this->gateway->supportsVoid(); - $this->assertInternalType('boolean', $supportsVoid); - - if ($supportsVoid) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->void()); - } else { - $this->assertFalse(method_exists($this->gateway, 'void')); - } - } - - public function testSupportsCreateCard() - { - $supportsCreate = $this->gateway->supportsCreateCard(); - $this->assertInternalType('boolean', $supportsCreate); - - if ($supportsCreate) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->createCard()); - } else { - $this->assertFalse(method_exists($this->gateway, 'createCard')); - } - } - - public function testSupportsDeleteCard() - { - $supportsDelete = $this->gateway->supportsDeleteCard(); - $this->assertInternalType('boolean', $supportsDelete); - - if ($supportsDelete) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->deleteCard()); - } else { - $this->assertFalse(method_exists($this->gateway, 'deleteCard')); - } - } - - public function testSupportsUpdateCard() - { - $supportsUpdate = $this->gateway->supportsUpdateCard(); - $this->assertInternalType('boolean', $supportsUpdate); - - if ($supportsUpdate) { - $this->assertInstanceOf(RequestInterface::class, $this->gateway->updateCard()); - } else { - $this->assertFalse(method_exists($this->gateway, 'updateCard')); - } - } - - /** - * @doesNotPerformAssertions - */ - public function testAuthorizeParameters() - { - if ($this->gateway->supportsAuthorize()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->authorize(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testCompleteAuthorizeParameters() - { - if ($this->gateway->supportsCompleteAuthorize()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->completeAuthorize(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testCaptureParameters() - { - if ($this->gateway->supportsCapture()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->capture(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testPurchaseParameters() - { - if ($this->gateway->supportsPurchase()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->purchase(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testCompletePurchaseParameters() - { - if ($this->gateway->supportsCompletePurchase()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->completePurchase(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testRefundParameters() - { - if ($this->gateway->supportsRefund()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->refund(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testVoidParameters() - { - if ($this->gateway->supportsVoid()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->void(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testCreateCardParameters() - { - if ($this->gateway->supportsCreateCard()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->createCard(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testDeleteCardParameters() - { - if ($this->gateway->supportsDeleteCard()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->deleteCard(); - $this->assertSame($value, $request->$getter()); - } - } - } - - /** - * @doesNotPerformAssertions - */ - public function testUpdateCardParameters() - { - if ($this->gateway->supportsUpdateCard()) { - foreach ($this->gateway->getDefaultParameters() as $key => $default) { - // set property on gateway - $getter = 'get'.ucfirst($this->camelCase($key)); - $setter = 'set'.ucfirst($this->camelCase($key)); - $value = uniqid(); - $this->gateway->$setter($value); - - // request should have matching property, with correct value - $request = $this->gateway->updateCard(); - $this->assertSame($value, $request->$getter()); - } - } - } + use Traits\HasBaseGatewayTestsTrait; } diff --git a/src/TestCase.php b/src/TestCase.php index c249afc..37af9ce 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -2,16 +2,8 @@ namespace Omnipay\Tests; -use Mockery as m; -use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; -use Omnipay\Common\Http\Client; -use Omnipay\Common\Http\ClientInterface; -use Omnipay\Common\Message\RequestInterface; +use Omnipay\Tests\Traits\HasTestUtilMethods; use PHPUnit\Framework\TestCase as PHPUnitTestCase; -use Psr\Http\Message\ResponseInterface; -use ReflectionObject; -use Http\Mock\Client as MockClient; -use Symfony\Component\HttpFoundation\Request as HttpRequest; /** * Base class for all Omnipay tests @@ -20,157 +12,5 @@ */ abstract class TestCase extends PHPUnitTestCase { - use MockeryPHPUnitIntegration; - - /** @var RequestInterface */ - private $mockRequest; - - /** @var MockClient */ - private $mockClient; - - /** @var ClientInterface */ - private $httpClient; - - /** @var HttpRequest */ - private $httpRequest; - - /** - * Converts a string to camel case - * - * @param string $str - * @return string - */ - public function camelCase($str) - { - return preg_replace_callback( - '/_([a-z])/', - function ($match) { - return strtoupper($match[1]); - }, - $str - ); - } - - - /** - * Get all of the mocked requests - * - * @return array - */ - public function getMockedRequests() - { - return $this->mockClient->getRequests(); - } - - /** - * Get a mock response for a client by mock file name - * - * @param string $path Relative path to the mock response file - * - * @return ResponseInterface - */ - public function getMockHttpResponse($path) - { - if ($path instanceof ResponseInterface) { - return $path; - } - - $ref = new ReflectionObject($this); - $dir = dirname($ref->getFileName()); - - // if mock file doesn't exist, check parent directory - if (!file_exists($dir.'/Mock/'.$path) && file_exists($dir.'/../Mock/'.$path)) { - return \GuzzleHttp\Psr7\parse_response(file_get_contents($dir.'/../Mock/'.$path)); - } - - return \GuzzleHttp\Psr7\parse_response(file_get_contents($dir.'/Mock/'.$path)); - } - - /** - * Set a mock response from a mock file on the next client request. - * - * This method assumes that mock response files are located under the - * Mock/ subdirectory of the current class. A mock response is added to the next - * request sent by the client. - * - * An array of path can be provided and the next x number of client requests are - * mocked in the order of the array where x = the array length. - * - * @param array|string $paths Path to files within the Mock folder of the service - * - * @return void returns the created mock plugin - */ - public function setMockHttpResponse($paths) - { - foreach ((array) $paths as $path) { - $this->mockClient->addResponse($this->getMockHttpResponse($path)); - } - } - - /** - * Helper method used by gateway test classes to generate a valid test credit card - */ - public function getValidCard() - { - return array( - 'firstName' => 'Example', - 'lastName' => 'User', - 'number' => '4111111111111111', - 'expiryMonth' => rand(1, 12), - 'expiryYear' => gmdate('Y') + rand(1, 5), - 'cvv' => rand(100, 999), - 'billingAddress1' => '123 Billing St', - 'billingAddress2' => 'Billsville', - 'billingCity' => 'Billstown', - 'billingPostcode' => '12345', - 'billingState' => 'CA', - 'billingCountry' => 'US', - 'billingPhone' => '(555) 123-4567', - 'shippingAddress1' => '123 Shipping St', - 'shippingAddress2' => 'Shipsville', - 'shippingCity' => 'Shipstown', - 'shippingPostcode' => '54321', - 'shippingState' => 'NY', - 'shippingCountry' => 'US', - 'shippingPhone' => '(555) 987-6543', - ); - } - - public function getMockRequest() - { - if (null === $this->mockRequest) { - $this->mockRequest = m::mock(RequestInterface::class); - } - - return $this->mockRequest; - } - - public function getMockClient() - { - if (null === $this->mockClient) { - $this->mockClient = new MockClient(); - } - - return $this->mockClient; - } - - public function getHttpClient() - { - if (null === $this->httpClient) { - $this->httpClient = new Client( - $this->getMockClient() - ); - } - - return $this->httpClient; - } - - public function getHttpRequest() - { - if (null === $this->httpRequest) { - $this->httpRequest = new HttpRequest; - } - - return $this->httpRequest; - } + use HasTestUtilMethods; } diff --git a/src/Traits/HasBaseGatewayTestsTrait.php b/src/Traits/HasBaseGatewayTestsTrait.php new file mode 100644 index 0000000..6b9aad1 --- /dev/null +++ b/src/Traits/HasBaseGatewayTestsTrait.php @@ -0,0 +1,390 @@ +gateway->getName(); + $this->assertNotEmpty($name); + $this->assertInternalType('string', $name); + } + + public function testGetShortNameNotEmpty() + { + $shortName = $this->gateway->getShortName(); + $this->assertNotEmpty($shortName); + $this->assertInternalType('string', $shortName); + } + + public function testGetDefaultParametersReturnsArray() + { + $settings = $this->gateway->getDefaultParameters(); + $this->assertInternalType('array', $settings); + } + + public function testDefaultParametersHaveMatchingMethods() + { + $settings = $this->gateway->getDefaultParameters(); + foreach ($settings as $key => $default) { + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + + $this->assertTrue(method_exists($this->gateway, $getter), "Gateway must implement $getter()"); + $this->assertTrue(method_exists($this->gateway, $setter), "Gateway must implement $setter()"); + + // setter must return instance + $this->assertSame($this->gateway, $this->gateway->$setter($value)); + $this->assertSame($value, $this->gateway->$getter()); + } + } + + public function testTestMode() + { + $this->assertSame($this->gateway, $this->gateway->setTestMode(false)); + $this->assertSame(false, $this->gateway->getTestMode()); + + $this->assertSame($this->gateway, $this->gateway->setTestMode(true)); + $this->assertSame(true, $this->gateway->getTestMode()); + } + + public function testCurrency() + { + // currency is normalized to uppercase + $this->assertSame($this->gateway, $this->gateway->setCurrency('eur')); + $this->assertSame('EUR', $this->gateway->getCurrency()); + } + + public function testSupportsAuthorize() + { + $supportsAuthorize = $this->gateway->supportsAuthorize(); + $this->assertInternalType('boolean', $supportsAuthorize); + + if ($supportsAuthorize) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->authorize()); + } else { + $this->assertFalse(method_exists($this->gateway, 'authorize')); + } + } + + public function testSupportsCompleteAuthorize() + { + $supportsCompleteAuthorize = $this->gateway->supportsCompleteAuthorize(); + $this->assertInternalType('boolean', $supportsCompleteAuthorize); + + if ($supportsCompleteAuthorize) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->completeAuthorize()); + } else { + $this->assertFalse(method_exists($this->gateway, 'completeAuthorize')); + } + } + + public function testSupportsCapture() + { + $supportsCapture = $this->gateway->supportsCapture(); + $this->assertInternalType('boolean', $supportsCapture); + + if ($supportsCapture) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->capture()); + } else { + $this->assertFalse(method_exists($this->gateway, 'capture')); + } + } + + public function testSupportsPurchase() + { + $supportsPurchase = $this->gateway->supportsPurchase(); + $this->assertInternalType('boolean', $supportsPurchase); + + if ($supportsPurchase) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->purchase()); + } else { + $this->assertFalse(method_exists($this->gateway, 'purchase')); + } + } + + public function testSupportsCompletePurchase() + { + $supportsCompletePurchase = $this->gateway->supportsCompletePurchase(); + $this->assertInternalType('boolean', $supportsCompletePurchase); + + if ($supportsCompletePurchase) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->completePurchase()); + } else { + $this->assertFalse(method_exists($this->gateway, 'completePurchase')); + } + } + + public function testSupportsRefund() + { + $supportsRefund = $this->gateway->supportsRefund(); + $this->assertInternalType('boolean', $supportsRefund); + + if ($supportsRefund) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->refund()); + } else { + $this->assertFalse(method_exists($this->gateway, 'refund')); + } + } + + public function testSupportsVoid() + { + $supportsVoid = $this->gateway->supportsVoid(); + $this->assertInternalType('boolean', $supportsVoid); + + if ($supportsVoid) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->void()); + } else { + $this->assertFalse(method_exists($this->gateway, 'void')); + } + } + + public function testSupportsCreateCard() + { + $supportsCreate = $this->gateway->supportsCreateCard(); + $this->assertInternalType('boolean', $supportsCreate); + + if ($supportsCreate) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->createCard()); + } else { + $this->assertFalse(method_exists($this->gateway, 'createCard')); + } + } + + public function testSupportsDeleteCard() + { + $supportsDelete = $this->gateway->supportsDeleteCard(); + $this->assertInternalType('boolean', $supportsDelete); + + if ($supportsDelete) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->deleteCard()); + } else { + $this->assertFalse(method_exists($this->gateway, 'deleteCard')); + } + } + + public function testSupportsUpdateCard() + { + $supportsUpdate = $this->gateway->supportsUpdateCard(); + $this->assertInternalType('boolean', $supportsUpdate); + + if ($supportsUpdate) { + $this->assertInstanceOf(RequestInterface::class, $this->gateway->updateCard()); + } else { + $this->assertFalse(method_exists($this->gateway, 'updateCard')); + } + } + + /** + * @doesNotPerformAssertions + */ + public function testAuthorizeParameters() + { + if ($this->gateway->supportsAuthorize()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->authorize(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testCompleteAuthorizeParameters() + { + if ($this->gateway->supportsCompleteAuthorize()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->completeAuthorize(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testCaptureParameters() + { + if ($this->gateway->supportsCapture()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->capture(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testPurchaseParameters() + { + if ($this->gateway->supportsPurchase()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->purchase(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testCompletePurchaseParameters() + { + if ($this->gateway->supportsCompletePurchase()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->completePurchase(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testRefundParameters() + { + if ($this->gateway->supportsRefund()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->refund(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testVoidParameters() + { + if ($this->gateway->supportsVoid()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->void(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testCreateCardParameters() + { + if ($this->gateway->supportsCreateCard()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->createCard(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testDeleteCardParameters() + { + if ($this->gateway->supportsDeleteCard()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->deleteCard(); + $this->assertSame($value, $request->$getter()); + } + } + } + + /** + * @doesNotPerformAssertions + */ + public function testUpdateCardParameters() + { + if ($this->gateway->supportsUpdateCard()) { + foreach ($this->gateway->getDefaultParameters() as $key => $default) { + // set property on gateway + $getter = 'get'.ucfirst($this->camelCase($key)); + $setter = 'set'.ucfirst($this->camelCase($key)); + $value = uniqid(); + $this->gateway->$setter($value); + + // request should have matching property, with correct value + $request = $this->gateway->updateCard(); + $this->assertSame($value, $request->$getter()); + } + } + } +} diff --git a/src/Traits/HasClientLiveTrait.php b/src/Traits/HasClientLiveTrait.php new file mode 100644 index 0000000..c6579d2 --- /dev/null +++ b/src/Traits/HasClientLiveTrait.php @@ -0,0 +1,29 @@ +httpClientLive) { + $this->httpClientLive = new Client(); + } + + return $this->httpClientLive; + } +} \ No newline at end of file diff --git a/src/Traits/HasClientTrait.php b/src/Traits/HasClientTrait.php new file mode 100644 index 0000000..9d75f33 --- /dev/null +++ b/src/Traits/HasClientTrait.php @@ -0,0 +1,58 @@ +mockClient->getRequests(); + } + + /** + * @return MockClient + */ + public function getMockClient() + { + if (null === $this->mockClient) { + $this->mockClient = new MockClient(); + } + + return $this->mockClient; + } + + /** + * @return Client|ClientInterface + */ + public function getHttpClient() + { + if (null === $this->httpClient) { + $this->httpClient = new Client( + $this->getMockClient() + ); + } + + return $this->httpClient; + } +} \ No newline at end of file diff --git a/src/Traits/HasRequestTrait.php b/src/Traits/HasRequestTrait.php new file mode 100644 index 0000000..aebba0d --- /dev/null +++ b/src/Traits/HasRequestTrait.php @@ -0,0 +1,45 @@ +mockRequest) { + $this->mockRequest = m::mock(RequestInterface::class); + } + + return $this->mockRequest; + } + + /** + * @return HttpRequest + */ + public function getHttpRequest() + { + if (null === $this->httpRequest) { + $this->httpRequest = new HttpRequest; + } + + return $this->httpRequest; + } +} \ No newline at end of file diff --git a/src/Traits/HasResponsesTrait.php b/src/Traits/HasResponsesTrait.php new file mode 100644 index 0000000..39f6d7c --- /dev/null +++ b/src/Traits/HasResponsesTrait.php @@ -0,0 +1,59 @@ +getFileName()); + + // if mock file doesn't exist, check parent directory + if (!file_exists($dir.'/Mock/'.$path) && file_exists($dir.'/../Mock/'.$path)) { + return \GuzzleHttp\Psr7\parse_response(file_get_contents($dir.'/../Mock/'.$path)); + } + + return \GuzzleHttp\Psr7\parse_response(file_get_contents($dir.'/Mock/'.$path)); + } + + /** + * Set a mock response from a mock file on the next client request. + * + * This method assumes that mock response files are located under the + * Mock/ subdirectory of the current class. A mock response is added to the next + * request sent by the client. + * + * An array of path can be provided and the next x number of client requests are + * mocked in the order of the array where x = the array length. + * + * @param array|string $paths Path to files within the Mock folder of the service + * + * @return void returns the created mock plugin + */ + public function setMockHttpResponse($paths) + { + foreach ((array) $paths as $path) { + $this->mockClient->addResponse($this->getMockHttpResponse($path)); + } + } + +} \ No newline at end of file diff --git a/src/Traits/HasTestUtilMethods.php b/src/Traits/HasTestUtilMethods.php new file mode 100644 index 0000000..d278725 --- /dev/null +++ b/src/Traits/HasTestUtilMethods.php @@ -0,0 +1,142 @@ +mockHttpRequests[] = $request; + + return $this; + } + + /** + * Get all of the mocked requests + * + * @return array + */ + public function getMockedRequests() + { + return $this->mockHttpRequests; + } + + /** + * Get a mock response for a client by mock file name + * + * @param string $path Relative path to the mock response file + * + * @return Response + */ + public function getMockHttpResponse($path) + { + if ($path instanceof Response) { + return $path; + } + + $ref = new ReflectionObject($this); + $dir = dirname($ref->getFileName()); + + // if mock file doesn't exist, check parent directory + if (!file_exists($dir.'/Mock/'.$path) && file_exists($dir.'/../Mock/'.$path)) { + return MockPlugin::getMockFile($dir.'/../Mock/'.$path); + } + + return MockPlugin::getMockFile($dir.'/Mock/'.$path); + } + + /** + * Set a mock response from a mock file on the next client request. + * + * This method assumes that mock response files are located under the + * Mock/ subdirectory of the current class. A mock response is added to the next + * request sent by the client. + * + * An array of path can be provided and the next x number of client requests are + * mocked in the order of the array where x = the array length. + * + * @param array|string $paths Path to files within the Mock folder of the service + * + * @return MockPlugin returns the created mock plugin + */ + public function setMockHttpResponse($paths) + { + $this->mockHttpRequests = []; + $that = $this; + $mock = new MockPlugin(null, true); + $this->getHttpClient()->getEventDispatcher()->removeSubscriber($mock); + $mock->getEventDispatcher()->addListener('mock.request', function (Event $event) use ($that) { + $that->addMockedHttpRequest($event['request']); + }); + + foreach ((array)$paths as $path) { + $mock->addResponse($this->getMockHttpResponse($path)); + } + + $this->getHttpClient()->getEventDispatcher()->addSubscriber($mock); + + return $mock; + } + + /** + * @return m\MockInterface + */ + public function getMockRequest() + { + if (null === $this->mockRequest) { + $this->mockRequest = m::mock(\Omnipay\Common\Message\RequestInterface::class); + } + + return $this->mockRequest; + } + + + /** + * @return Client + */ + public function getHttpClientReal() + { + if (null === $this->httpClientReal) { + $this->httpClientReal = new Client(); + } + + return $this->httpClientReal; + } + + /** + * @return HttpRequest + */ + public function getHttpRequest() + { + if (null === $this->httpRequest) { + $this->httpRequest = new HttpRequest; + } + + return $this->httpRequest; + } +} diff --git a/src/Traits/HasUtilityMethodsTrait.php b/src/Traits/HasUtilityMethodsTrait.php new file mode 100644 index 0000000..66f10e4 --- /dev/null +++ b/src/Traits/HasUtilityMethodsTrait.php @@ -0,0 +1,54 @@ + 'Example', + 'lastName' => 'User', + 'number' => '4111111111111111', + 'expiryMonth' => rand(1, 12), + 'expiryYear' => gmdate('Y') + rand(1, 5), + 'cvv' => rand(100, 999), + 'billingAddress1' => '123 Billing St', + 'billingAddress2' => 'Billsville', + 'billingCity' => 'Billstown', + 'billingPostcode' => '12345', + 'billingState' => 'CA', + 'billingCountry' => 'US', + 'billingPhone' => '(555) 123-4567', + 'shippingAddress1' => '123 Shipping St', + 'shippingAddress2' => 'Shipsville', + 'shippingCity' => 'Shipstown', + 'shippingPostcode' => '54321', + 'shippingState' => 'NY', + 'shippingCountry' => 'US', + 'shippingPhone' => '(555) 987-6543', + ]; + } +} \ No newline at end of file