Skip to content

Commit

Permalink
[EngCom] Public Pull Requests - 2.2-develop
Browse files Browse the repository at this point in the history
 - merged latest code from mainline branch
  • Loading branch information
magento-engcom-team authored Dec 28, 2017
2 parents 97def3a + 5d572a0 commit 3322464
Show file tree
Hide file tree
Showing 24 changed files with 839 additions and 430 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Braintree\Gateway\Validator;

use Braintree\Error\ErrorCollection;
use Braintree\Error\Validation;
use Braintree\Result\Error;
use Braintree\Result\Successful;

/**
* Processes errors codes from Braintree response.
*/
class ErrorCodeValidator
{
/**
* Invokes validation.
*
* @param Successful|Error $response
* @return array
*/
public function __invoke($response)
{
if (!$response instanceof Error) {
return [true, [__('Transaction is successful.')]];
}

return [false, $this->getErrorCodes($response->errors)];
}

/**
* Retrieves list of error codes from Braintree response.
*
* @param ErrorCollection $collection
* @return array
*/
private function getErrorCodes(ErrorCollection $collection)
{
$result = [];
/** @var Validation $error */
foreach ($collection->deepAll() as $error) {
$result[] = $error->code;
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ class GeneralResponseValidator extends AbstractValidator
*/
protected $subjectReader;

/**
* @var ErrorCodeValidator
*/
private $errorCodeValidator;

/**
* Constructor
*
* @param ResultInterfaceFactory $resultFactory
* @param SubjectReader $subjectReader
* @param ErrorCodeValidator $errorCodeValidator
*/
public function __construct(ResultInterfaceFactory $resultFactory, SubjectReader $subjectReader)
{
public function __construct(
ResultInterfaceFactory $resultFactory,
SubjectReader $subjectReader,
ErrorCodeValidator $errorCodeValidator
) {
parent::__construct($resultFactory);
$this->subjectReader = $subjectReader;
$this->errorCodeValidator = $errorCodeValidator;
}

/**
Expand Down Expand Up @@ -62,9 +72,10 @@ protected function getResponseValidators()
function ($response) {
return [
property_exists($response, 'success') && $response->success === true,
[__('Braintree error response.')]
[$response->message ?? __('Braintree error response.')]
];
}
},
$this->errorCodeValidator
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
*/
namespace Magento\Braintree\Test\Unit\Gateway\Validator;

use Braintree\Transaction;
use Braintree\Result\Error;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Braintree\Gateway\Validator\ErrorCodeValidator;
use Magento\Braintree\Gateway\Validator\GeneralResponseValidator;
use Magento\Framework\Phrase;
use Magento\Payment\Gateway\Validator\ResultInterface;
use Magento\Payment\Gateway\Validator\Result;
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
use Magento\Braintree\Gateway\Validator\GeneralResponseValidator;
use Magento\Braintree\Gateway\SubjectReader;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -20,14 +22,9 @@ class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
private $responseValidator;

/**
* @var ResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultInterfaceFactoryMock;

/**
* @var SubjectReader|\PHPUnit_Framework_MockObject_MockObject
* @var ResultInterfaceFactory|MockObject
*/
private $subjectReaderMock;
private $resultInterfaceFactory;

/**
* Set up
Expand All @@ -36,23 +33,20 @@ class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
*/
protected function setUp()
{
$this->resultInterfaceFactoryMock = $this->getMockBuilder(
\Magento\Payment\Gateway\Validator\ResultInterfaceFactory::class
)->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
$this->resultInterfaceFactory = $this->getMockBuilder(ResultInterfaceFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->responseValidator = new GeneralResponseValidator(
$this->resultInterfaceFactoryMock,
$this->subjectReaderMock
$this->resultInterfaceFactory,
new SubjectReader(),
new ErrorCodeValidator()
);
}

/**
* Run test for validate method
* Checks a case when the validator processes successful and failed transactions.
*
* @param array $validationSubject
* @param bool $isValid
Expand All @@ -61,45 +55,52 @@ protected function setUp()
*
* @dataProvider dataProviderTestValidate
*/
public function testValidate(array $validationSubject, $isValid, $messages)
public function testValidate(array $validationSubject, bool $isValid, $messages)
{
/** @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject $resultMock */
$resultMock = $this->createMock(ResultInterface::class);
$result = new Result($isValid, $messages);

$this->subjectReaderMock->expects(self::once())
->method('readResponseObject')
->with($validationSubject)
->willReturn($validationSubject['response']['object']);

$this->resultInterfaceFactoryMock->expects(self::once())
->method('create')
$this->resultInterfaceFactory->method('create')
->with([
'isValid' => $isValid,
'failsDescription' => $messages
])
->willReturn($resultMock);
->willReturn($result);

$actualMock = $this->responseValidator->validate($validationSubject);
$actual = $this->responseValidator->validate($validationSubject);

self::assertEquals($resultMock, $actualMock);
self::assertEquals($result, $actual);
}

/**
* Gets variations for different type of response.
*
* @return array
*/
public function dataProviderTestValidate()
{
$successTrue = new \stdClass();
$successTrue->success = true;
$successTransaction = new \stdClass();
$successTransaction->success = true;

$failureTransaction = new \stdClass();
$failureTransaction->success = false;
$failureTransaction->message = 'Transaction was failed.';

$successFalse = new \stdClass();
$successFalse->success = false;
$errors = [
'errors' => [
[
'code' => 81804,
'attribute' => 'base',
'message' => 'Cannot process transaction.'
]
]
];
$errorTransaction = new Error(['errors' => $errors]);

return [
[
'validationSubject' => [
'response' => [
'object' => $successTrue
'object' => $successTransaction
],
],
'isValid' => true,
Expand All @@ -108,12 +109,24 @@ public function dataProviderTestValidate()
[
'validationSubject' => [
'response' => [
'object' => $successFalse
'object' => $failureTransaction
]
],
'isValid' => false,
[
__('Transaction was failed.')
]
],
[
'validationSubject' => [
'response' => [
'object' => $errorTransaction
]
],
'isValid' => false,
[
__('Braintree error response.')
__('Braintree error response.'),
81804
]
]
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
*/
namespace Magento\Braintree\Test\Unit\Gateway\Validator;

use Braintree\Transaction;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Braintree\Gateway\Validator\ErrorCodeValidator;
use Magento\Braintree\Gateway\Validator\PaymentNonceResponseValidator;
use Magento\Payment\Gateway\Validator\ResultInterface;
use Magento\Payment\Gateway\Validator\Result;
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
use Magento\Braintree\Gateway\SubjectReader;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Class PaymentNonceResponseValidatorTest
*/
class PaymentNonceResponseValidatorTest extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -22,35 +20,24 @@ class PaymentNonceResponseValidatorTest extends \PHPUnit\Framework\TestCase
private $validator;

/**
* @var ResultInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
* @var ResultInterfaceFactory|MockObject
*/
private $resultInterfaceFactory;

/**
* @var SubjectReader|\PHPUnit_Framework_MockObject_MockObject
*/
private $subjectReader;

protected function setUp()
{
$this->resultInterfaceFactory = $this->getMockBuilder(ResultInterfaceFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->subjectReader = $this->getMockBuilder(SubjectReader::class)
->disableOriginalConstructor()
->setMethods(['readResponseObject'])
->getMock();

$this->validator = new PaymentNonceResponseValidator(
$this->resultInterfaceFactory,
$this->subjectReader
new SubjectReader(),
new ErrorCodeValidator()
);
}

/**
* @covers \Magento\Braintree\Gateway\Validator\PaymentNonceResponseValidator::validate
*/
public function testFailedValidate()
{
$obj = new \stdClass();
Expand All @@ -61,23 +48,12 @@ public function testFailedValidate()
]
];

$this->subjectReader->expects(static::once())
->method('readResponseObject')
->willReturn($obj);

$result = $this->createMock(ResultInterface::class);
$this->resultInterfaceFactory->expects(self::once())
->method('create')
->with([
'isValid' => false,
'failsDescription' => [
__('Payment method nonce can\'t be retrieved.')
]
])
$result = new Result(false, [__('Payment method nonce can\'t be retrieved.')]);
$this->resultInterfaceFactory->method('create')
->willReturn($result);

$actual = $this->validator->validate($subject);
static::assertEquals($result, $actual);
self::assertEquals($result, $actual);
}

public function testValidateSuccess()
Expand All @@ -93,20 +69,11 @@ public function testValidateSuccess()
]
];

$this->subjectReader->expects(static::once())
->method('readResponseObject')
->willReturn($obj);

$result = $this->createMock(ResultInterface::class);
$this->resultInterfaceFactory->expects(self::once())
->method('create')
->with([
'isValid' => true,
'failsDescription' => []
])
$result = new Result(true);
$this->resultInterfaceFactory->method('create')
->willReturn($result);

$actual = $this->validator->validate($subject);
static::assertEquals($result, $actual);
self::assertEquals($result, $actual);
}
}
Loading

0 comments on commit 3322464

Please sign in to comment.