Skip to content

Commit a784526

Browse files
committed
Add braintree mock for testing
1 parent 411a559 commit a784526

File tree

9 files changed

+231
-62
lines changed

9 files changed

+231
-62
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestModuleBraintree\Model\Adapter;
9+
10+
use Braintree\ClientToken;
11+
use Braintree\Configuration;
12+
use Braintree\CreditCard;
13+
use Braintree\Transaction;
14+
use Magento\Braintree\Gateway\Config\Config;
15+
use Magento\Braintree\Model\Adminhtml\Source\Environment;
16+
use Magento\TestModuleBraintree\Model\MockResponseDataProvider;
17+
18+
/**
19+
* Class BraintreeAdapter mock for testing
20+
* Use \Magento\TestModuleBraintree\Model\Adapter\BraintreeAdapterFactory to create new instance of adapter.
21+
* @codeCoverageIgnore
22+
*/
23+
class BraintreeAdapter extends \Magento\Braintree\Model\Adapter\BraintreeAdapter
24+
{
25+
/**
26+
* @var Config
27+
*/
28+
private $config;
29+
30+
/**
31+
* @var MockResponseDataProvider
32+
*/
33+
private $mockResponseDataProvider;
34+
35+
/**
36+
* @param $merchantId
37+
* @param $publicKey
38+
* @param $privateKey
39+
* @param $environment
40+
* @param MockResponseDataProvider $mockResponseDataProvider
41+
*/
42+
public function __construct(
43+
$merchantId,
44+
$publicKey,
45+
$privateKey,
46+
$environment,
47+
MockResponseDataProvider $mockResponseDataProvider
48+
) {
49+
parent::__construct($merchantId, $publicKey, $privateKey, $environment);
50+
$this->mockResponseDataProvider = $mockResponseDataProvider;
51+
}
52+
53+
/**
54+
* @param string $token
55+
* @return \Braintree\Result\Successful|\Braintree\Result\Error
56+
*/
57+
public function createNonce($token)
58+
{
59+
return $this->mockResponseDataProvider->generateMockNonceResponse();
60+
}
61+
62+
/**
63+
* @param array $attributes
64+
* @return \Braintree\Result\Successful|\Braintree\Result\Error
65+
*/
66+
public function sale(array $attributes)
67+
{
68+
return $this->mockResponseDataProvider->generateMockSaleResponse($attributes);
69+
}
70+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestModuleBraintree\Model;
9+
10+
use Magento\Framework\Math\Random;
11+
12+
/**
13+
* Provide mock responses for Braintree adapter
14+
*/
15+
class MockResponseDataProvider
16+
{
17+
/**
18+
* @var Random
19+
*/
20+
private $random;
21+
22+
/**
23+
* @param Random $random
24+
*/
25+
public function __construct(
26+
Random $random
27+
) {
28+
$this->random = $random;
29+
}
30+
31+
/**
32+
* Create mock sale response for testing
33+
*
34+
* @param array $attributes
35+
* @return \Braintree\Instance
36+
*/
37+
public function generateMockSaleResponse(array $attributes): \Braintree\Instance
38+
{
39+
$transaction = $this->createTransaction($attributes);
40+
41+
return new \Braintree\Result\Successful([$transaction]);
42+
}
43+
44+
/**
45+
* Create mock nonce response for testing
46+
*
47+
* @return \Braintree\Instance
48+
*/
49+
public function generateMockNonceResponse(): \Braintree\Instance
50+
{
51+
$nonce = $this->createNonce();
52+
53+
return new \Braintree\Result\Successful($nonce, 'paymentMethodNonce');
54+
}
55+
56+
/**
57+
* Create Braintree transaction from provided request attributes
58+
*
59+
* @param array $attributes
60+
* @return \Braintree\Transaction
61+
* @throws \Magento\Framework\Exception\LocalizedException
62+
*/
63+
private function createTransaction(array $attributes): \Braintree\Transaction
64+
{
65+
$creditCardInfo = $this->generateCardDetails();
66+
return \Braintree\Transaction::factory([
67+
'amount' => $attributes['amount'],
68+
'billing' => $attributes['billing'] ?? null,
69+
'creditCard' => $creditCardInfo,
70+
'cardDetails' => new \Braintree\Transaction\CreditCardDetails($creditCardInfo),
71+
'currencyIsoCode' => 'USD',
72+
'customer' => $attributes['customer'],
73+
'cvvResponseCode' => 'M',
74+
'id' => $this->random->getRandomString(8),
75+
'options' => $attributes['options'] ?? null,
76+
'shipping' => $attributes['shipping'] ?? null,
77+
'paymentMethodNonce' => $attributes['paymentMethodNonce'],
78+
'status' => 'authorized',
79+
'type' => 'sale',
80+
]);
81+
}
82+
83+
/**
84+
* Generate fake Braintree card details
85+
*
86+
* @return array
87+
* @throws \Magento\Framework\Exception\LocalizedException
88+
*/
89+
private function generateCardDetails(): array
90+
{
91+
return [
92+
'bin' => $this->random->getRandomString(6),
93+
'cardType' => 'Visa',
94+
'expirationMonth' => '12',
95+
'expirationYear' => '2020', //TODO: make dynamic
96+
'last4' => (string) random_int(1000, 9999),
97+
'token' => $this->random->getRandomString(6),
98+
'uniqueNumberIdentifier' => $this->random->getRandomString(32),
99+
];
100+
}
101+
102+
/**
103+
* Create fake Braintree nonce
104+
*
105+
* @return \Braintree\PaymentMethodNonce
106+
* @throws \Magento\Framework\Exception\LocalizedException
107+
*/
108+
private function createNonce(): \Braintree\PaymentMethodNonce
109+
{
110+
$lastFour = (string) random_int(1000, 9999);
111+
$lastTwo = substr($lastFour, -2);
112+
return \Braintree\PaymentMethodNonce::factory([
113+
'consumed' => false,
114+
'default' => true,
115+
'description' => 'ending in ' . $lastTwo,
116+
'details' => [
117+
'bin' => $this->random->getRandomString(6),
118+
'cardType' => 'Visa',
119+
'lastFour' => $lastFour,
120+
'lastTwo' => $lastTwo,
121+
],
122+
'hasSubscription' => false,
123+
'isLocked' => false,
124+
'nonce' => $this->random->getRandomString(36),
125+
'type' => 'CreditCard'
126+
]);
127+
}
128+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\Braintree\Model\Adapter\BraintreeAdapter" type="Magento\TestModuleBraintree\Model\Adapter\BraintreeAdapter" />
10+
</config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_TestModuleBraintree" />
10+
</config>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\Framework\Component\ComponentRegistrar;
7+
8+
$registrar = new ComponentRegistrar();
9+
if ($registrar->getPath(ComponentRegistrar::MODULE, 'Magento_TestModuleBraintree') === null) {
10+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_TestModuleBraintree', __DIR__);
11+
}

dev/tests/api-functional/phpunit_graphql.xml.dist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@
4747
<const name="TESTS_MAGENTO_INSTALLATION" value="disabled"/>
4848
<!-- Magento mode for tests execution. Possible values are "default", "developer" and "production". -->
4949
<const name="TESTS_MAGENTO_MODE" value="default"/>
50-
<!-- Credentials for testing Braintree -->
51-
<!--<const name="TESTS_BRAINTREE_MERCHANT_ID" value="sandbox_merchant_id"/>-->
52-
<!--<const name="TESTS_BRAINTREE_PUBLIC_KEY" value="sandbox_public_key"/>-->
53-
<!--<const name="TESTS_BRAINTREE_PRIVATE_KEY" value="sandbox_private_key"/>-->
5450
</php>
5551

5652
<!-- Test listeners -->

dev/tests/api-functional/testsuite/Magento/GraphQl/Braintree/Customer/SetPaymentMethodTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function testPlaceOrder()
126126
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
127127
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
128128
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
129-
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/add_simple_product_qty_one.php
129+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
130130
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
131131
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
132132
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
@@ -166,7 +166,7 @@ public function testPlaceOrderSaveInVault()
166166
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
167167
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
168168
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
169-
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/add_simple_product_qty_three.php
169+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
170170
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
171171
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
172172
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php

dev/tests/integration/testsuite/Magento/GraphQl/Braintree/_files/add_simple_product_qty_one.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

dev/tests/integration/testsuite/Magento/GraphQl/Braintree/_files/add_simple_product_qty_three.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)