Merge pull request #103 from woocommerce/PCP-63

Pcp 63
This commit is contained in:
Emili Castells 2021-02-01 11:15:12 +01:00 committed by GitHub
commit 9434d8f293
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 193 additions and 1 deletions

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentMethod;
@ -16,7 +17,6 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -215,7 +215,11 @@ class CreateOrderEndpoint implements EndpointInterface {
$number = preg_replace( '/[^0-9]/', '', $number );
$number = substr( $number, 0, 14 );
$data['payer']['phone']['phone_number']['national_number'] = $number;
if ( empty( $data['payer']['phone']['phone_number']['national_number'] ) ) {
unset( $data['payer']['phone'] );
}
}
$payer = $this->payer_factory->from_paypal_response( json_decode( wp_json_encode( $data['payer'] ) ) );
}
return $payer;

5
patchwork.json Normal file
View file

@ -0,0 +1,5 @@
{
"redefinable-internals": [
"json_decode"
]
}

View file

@ -0,0 +1,183 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use ReflectionClass;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Repository\CartRepository;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use function Brain\Monkey\Functions\expect;
class CreateOrderEndpointTest extends TestCase
{
use MockeryPHPUnitIntegration;
/**
* @dataProvider dataForTestPhoneNumber
* @test
*
* @param $data
* @param $expectedResult
*/
public function payerVerifiesPhoneNumber($data, $expectedResult)
{
list($payer_factory, $testee) = $this->mockTestee();
$method = $this->testPrivateMethod(CreateOrderEndpoint::class, 'payer');
$dataString = wp_json_encode($expectedResult['payer']);
$dataObj = json_decode(wp_json_encode($expectedResult['payer']));
expect('wp_json_encode')->once()->with($expectedResult['payer'])
->andReturn($dataString);
expect('json_decode')->once()->with($dataString)->andReturn($dataObj);
$payer_factory->expects('from_paypal_response')->with($dataObj);
$method->invokeArgs($testee, array($data));
}
public function dataForTestPhoneNumber() : array {
return [
'emptyStringPhone' => [
[
'context' => 'none',
'payer'=>[
'name'=>['given_name'=>'testName'],
'phone'=>[
'phone_number'=>[
'national_number'=>''
]
]
]
],
[
'context' => 'none',
'payer' => [
'name' => ['given_name' => 'testName']
]
]
],
'tooLongStringPhone' => [
[
'context' => 'none',
'payer'=>[
'name'=>['given_name'=>'testName'],
'phone'=>[
'phone_number'=>[
'national_number'=>'43241341234123412341234123123412341'
]
]
]
],
[
'context' => 'none',
'payer'=>[
'name'=>['given_name'=>'testName'],
'phone'=>[
'phone_number'=>[
'national_number'=>'43241341234123'
]
]
]
]
],
'removeNonISOStringPhone' => [
[
'context' => 'none',
'payer'=>[
'name'=>['given_name'=>'testName'],
'phone'=>[
'phone_number'=>[
'national_number'=>'432a34as73737373'
]
]
]
],
[
'context' => 'none',
'payer'=>[
'name'=>['given_name'=>'testName'],
'phone'=>[
'phone_number'=>[
'national_number'=>'4323473737373'
]
]
]
]
],
'notNumbersStringPhone' => [
[
'context' => 'none',
'payer'=>[
'name'=>['given_name'=>'testName'],
'phone'=>[
'phone_number'=>[
'national_number'=>'this is_notaPhone'
]
]
]
],
[
'context' => 'none',
'payer'=>[
'name'=>['given_name'=>'testName']
]
]
]
];
}
/**
* @return array
*/
protected function mockTestee()
{
$request_data = Mockery::mock(RequestData::class);
$cart_repository = Mockery::mock(CartRepository::class);
$purchase_unit_factory = Mockery::mock(PurchaseUnitFactory::class);
$order_endpoint = Mockery::mock(OrderEndpoint::class);
$payer_factory = Mockery::mock(PayerFactory::class);
$session_handler = Mockery::mock(SessionHandler::class);
$settings = Mockery::mock(Settings::class);
$early_order_handler = Mockery::mock(EarlyOrderHandler::class);
$testee = new CreateOrderEndpoint(
$request_data,
$cart_repository,
$purchase_unit_factory,
$order_endpoint,
$payer_factory,
$session_handler,
$settings,
$early_order_handler
);
return array($payer_factory, $testee);
}
/**
* @param $class
*
* @param $method
*
* @return \ReflectionMethod
* @throws \ReflectionException
*/
protected function testPrivateMethod($class, $method)
{
$reflector = new ReflectionClass($class);
$method = $reflector->getMethod($method);
$method->setAccessible(true);
return $method;
}
}