Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PayflowNvpTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Framework\HTTP\Adapter\CurlFactory;
13 use Magento\Paypal\Model\CartFactory;
18 use PHPUnit\Framework\MockObject_MockObject as MockObject;
19 
23 class PayflowNvpTest extends \PHPUnit\Framework\TestCase
24 {
28  private $nvpApi;
29 
33  private $objectManager;
34 
38  private $httpClient;
39 
43  protected function setUp()
44  {
45  $this->objectManager = Bootstrap::getObjectManager();
46 
48  $httpFactory = $this->getMockBuilder(CurlFactory::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->httpClient = $this->getMockBuilder(Curl::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55  $httpFactory->method('create')
56  ->willReturn($this->httpClient);
57 
58  $this->nvpApi = $this->objectManager->create(PayflowNvp::class, [
59  'curlFactory' => $httpFactory
60  ]);
61 
63  $productMetadata = $this->getMockBuilder(ProductMetadataInterface::class)
64  ->getMock();
65  $productMetadata->method('getEdition')
66  ->willReturn('');
67 
69  $config = $this->objectManager->get(Config::class);
70  $config->setMethodCode(Config::METHOD_WPP_PE_EXPRESS);
71 
72  $refObject = new \ReflectionObject($config);
73  $refProperty = $refObject->getProperty('productMetadata');
74  $refProperty->setAccessible(true);
75  $refProperty->setValue($config, $productMetadata);
76 
77  $this->nvpApi->setConfigObject($config);
78  }
79 
86  public function testRequestLineItems()
87  {
88  $quote = $this->getQuote('100000015');
90  $cartFactory = $this->objectManager->get(CartFactory::class);
91  $cart = $cartFactory->create(['salesModel' => $quote]);
92 
93  $request = 'TENDER=P&AMT=52.14&FREIGHTAMT=0.00&TAXAMT=0.00&'
94  . 'L_NAME0=Simple 1&L_QTY0=1&L_COST0=7.69&'
95  . 'L_NAME1=Simple 2&L_QTY1=2&L_COST1=9.69&'
96  . 'L_NAME2=Simple 3&L_QTY2=3&L_COST2=11.69&'
97  . 'L_NAME3=Discount&L_QTY3=1&L_COST3=-10.00&'
98  . 'TRXTYPE=A&ACTION=S&BUTTONSOURCE=Magento_Cart_';
99 
100  $this->httpClient->method('write')
101  ->with(
102  'POST',
103  'https://payflowpro.paypal.com/transaction',
104  '1.1',
105  ['PAYPAL-NVP: Y'],
106  self::equalTo($request)
107  );
108 
109  $this->httpClient->method('read')
110  ->willReturn("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nRESULT=0&RESPMSG=Approved");
111 
112  $this->nvpApi->setAmount($quote->getBaseGrandTotal());
113  $this->nvpApi->setPaypalCart($cart);
114  $this->nvpApi->setQuote($quote);
115  $this->nvpApi->setIsLineItemsEnabled(true);
116  $this->nvpApi->callSetExpressCheckout();
117  }
118 
125  private function getQuote($reservedOrderId)
126  {
128  $searchCriteriaBuilder = $this->objectManager->create(SearchCriteriaBuilder::class);
129  $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
130  ->create();
132  $quoteRepository = $this->objectManager->get(QuoteRepository::class);
134  ->getItems();
135  return array_pop($items);
136  }
137 }
$config
Definition: fraud_order.php:17
$quote
$searchCriteria
$searchCriteriaBuilder
$quoteRepository
$items