Created
January 3, 2018 08:01
-
-
Save lastday154/1613503e563a7df7c00c68d50fa1c852 to your computer and use it in GitHub Desktop.
unitTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Created by PhpStorm. | |
| * User: Zalora | |
| * Date: 4/4/17 | |
| * Time: 6:14 PM | |
| */ | |
| class Payment_Model_Adyen_Method_CreditCardTest extends PHPUnit_Framework_TestCase | |
| { | |
| /** @var Payment_Model_Adyen_Method_CreditCard | PHPUnit_Framework_MockObject_MockObject */ | |
| private $model; | |
| private $config; | |
| /** @var Transfer_Sales_Order */ | |
| private $transferSalesOrder; | |
| /** @var Bob_Payment_Adyen_CreditCard_JsonApiClient | PHPUnit_Framework_MockObject_MockObject */ | |
| private $jsonClient; | |
| public function setup() | |
| { | |
| $this->initSetup(); | |
| } | |
| public function initSetup(array $args = []) | |
| { | |
| if (!$this->config) { | |
| $config = new Bob_Payment_Adyen_CreditCard_Configuration(); | |
| $config->setCurrency('HKD') | |
| ->setRedirectUrl('http://zalora.tld/redirecturl') | |
| ->setMerchantAccount('zaloraHK') | |
| ->setRedirectUrlQuicksilver('http://doraemon.com/redirecturl'); | |
| $this->config = $config; | |
| } | |
| $this->model = $this->getMockBuilder('Payment_Model_Adyen_Method_CreditCard') | |
| ->setConstructorArgs([$this->config]) | |
| ->setMethods([ | |
| 'getProject', | |
| 'getCustomerService', | |
| 'getShopperReference', | |
| 'savePaymentData', | |
| 'getRegionService', | |
| 'getCityModel', | |
| 'getThirdlevelService', | |
| 'getDefaultCity', | |
| 'getCountryCode', | |
| 'refund', | |
| 'getTotalPaidPrice' | |
| ]) | |
| ->getMock(); | |
| $this->model->expects($this->any())->method('getProject') | |
| ->will($this->returnValue($args['project'] ?? 'hkfas')); | |
| $this->model->expects($this->any())->method('getDefaultCity') | |
| ->will($this->returnValue('HongKong')); | |
| $this->model->expects($this->any())->method('getCountryCode') | |
| ->will($this->returnValue('HK')); | |
| $regionService = $this->getMockBuilder('Customer_Service_Region') | |
| ->disableOriginalConstructor() | |
| ->setMethods(['findRegionById']) | |
| ->getMock(); | |
| $regionService->expects($this->any())->method('findRegionById') | |
| ->will($this->returnValue(new Transfer_Customer_Address_Region(['name' => 'region1']))); | |
| $this->model->expects($this->any())->method('getRegionService') | |
| ->will($this->returnValue($regionService)); | |
| $cityModel = $this->getMockBuilder('Customer_Model_City') | |
| ->disableOriginalConstructor() | |
| ->setMethods(['getCityById']) | |
| ->getMock(); | |
| $cityModel->expects($this->any())->method('getCityById') | |
| ->will($this->returnValue(new Transfer_Customer_Address_City(['name' => 'city1']))); | |
| $this->model->expects($this->any())->method('getCityModel') | |
| ->will($this->returnValue($cityModel)); | |
| $thirdService = $this->getMockBuilder('Customer_Service_AddressThirdLevel') | |
| ->disableOriginalConstructor() | |
| ->setMethods(['getAddressThirdLevelNameFromId', 'getPostcodeFromId']) | |
| ->getMock(); | |
| $thirdService->expects($this->any())->method('getAddressThirdLevelNameFromId') | |
| ->will($this->returnValue('third1')); | |
| $thirdService->expects($this->any())->method('getPostcodeFromId') | |
| ->will($this->returnValue('123456')); | |
| $this->model->expects($this->any())->method('getThirdlevelService') | |
| ->will($this->returnValue($thirdService)); | |
| $getShopperReferenceCallback = function($customer) { | |
| return 'SR-HK-' . $customer->getIdCustomer(); | |
| }; | |
| $this->model->expects($this->any())->method('getShopperReference') | |
| ->will($this->returnCallback($getShopperReferenceCallback)); | |
| $customerService = $this->getMockBuilder('Customer_Service_Customer') | |
| ->disableOriginalConstructor() | |
| ->setMethods(['edit']) | |
| ->getMock(); | |
| $this->model->expects($this->any())->method('getCustomerService') | |
| ->will($this->returnValue($customerService)); | |
| $this->transferSalesOrder = new Transfer_Sales_Order([ | |
| 'grand_total' => 300.00 | |
| ]); | |
| $item = new Transfer_Sales_Order_Item([ | |
| 'paid_price' => 100.00 | |
| ]); | |
| $items = new Transfer_Sales_Order_ItemCollection([$item, $item, $item]); | |
| $this->transferSalesOrder->setItemCollection($items); | |
| $this->jsonClient = $this->getMockBuilder('Bob_Payment_Adyen_CreditCard_JsonApiClient') | |
| ->setConstructorArgs([$this->config]) | |
| ->setMethods([ | |
| 'refund' | |
| ]) | |
| ->getMock(); | |
| $this->model->setClient($this->jsonClient); | |
| } | |
| public function testGetAmount() | |
| { | |
| $method = $this->getMethod('_getAmount'); | |
| $expected = new Bob_Payment_Adyen_Common_Data_Amount(); | |
| $expected->setCurrency('HKD')->setValue(10000); | |
| $this->assertEquals($expected, $method->invoke($this->model, 100)); | |
| $this->initSetup(['project' => 'idfas']); | |
| $expected->setCurrency('HKD')->setValue(100); | |
| $this->assertEquals($expected, $method->invoke($this->model, 100)); | |
| } | |
| public function testSetRecurring() | |
| { | |
| $method = $this->getMethod('setRecurring'); | |
| $request = new Bob_Payment_Adyen_Recurring_Data_CreditCard_PaymentRequest(); | |
| $customer = new Transfer_Customer([ | |
| 'id_customer' => 101, | |
| ]); | |
| $order = new Transfer_Sales_Order([ | |
| 'customer' => $customer, | |
| 'customer_email' => '[email protected]' | |
| ]); | |
| $expected = new Bob_Payment_Adyen_Recurring_Data_CreditCard_PaymentRequest(); | |
| $this->assertEquals($expected, $method->invoke($this->model, $order, $request)); | |
| $order->setIsRecurring(true); | |
| $order->setNewRecurring(true); | |
| $recurring = new Bob_Payment_Adyen_Recurring_Data_Recurring(); | |
| $recurring->setContract(Bob_Payment_Adyen_Recurring_Data_Recurring::CONTRACT_RECURRING); | |
| $expected->setRecurring($recurring) | |
| ->setShopperReference('SR-HK-101') | |
| ->setShopperEmail($order->getCustomerEmail()); | |
| $this->assertEquals($expected, $method->invoke($this->model, $order, $request)); | |
| $order->setNewRecurring(false); | |
| $expected->setShopperInteraction(Bob_Payment_Adyen_Recurring_Helper::SHOPPER_INTERACTION_RECURRING); | |
| $this->assertEquals($expected, $method->invoke($this->model, $order, $request)); | |
| } | |
| public function testGetCardReferenceByParameterCollection() | |
| { | |
| $method = $this->getMethod('_getCardReferenceByParameterCollection'); | |
| $collection = new Transfer_Sales_Order_Payment_ParameterCollection(); | |
| $this->assertNull($method->invoke($this->model, $collection)); | |
| $this->assertEquals('cardid001', $method->invoke($this->model, $this->getParameterCollection())); | |
| } | |
| public function testGetCardByParameterCollection() | |
| { | |
| $method = $this->getMethod('_getCardByParameterCollection'); | |
| $collection = new Transfer_Sales_Order_Payment_ParameterCollection(); | |
| $this->assertEquals(['card.encrypted.json' => ''], $method->invoke($this->model, $collection)); | |
| $this->assertEquals( | |
| ['card.encrypted.json' => 'encrypteddatastring'], | |
| $method->invoke($this->model, $this->getParameterCollection()) | |
| ); | |
| } | |
| public function testSetFraudDecision() | |
| { | |
| $method = $this->getMethod('setFraudDecision'); | |
| $order = new Transfer_Sales_Order(); | |
| $result = new Bob_Payment_Adyen_Common_Data_PaymentResult(); | |
| //$method->invoke($this->model, $order, $result); | |
| //$this->assertEquals('ACCEPT', $order->getFraudDecision()->decision); | |
| $result->setAdditionalData(['fraudResultType' => Payment_Model_Adyen_Method_CreditCard::FRAUD_RESULT_AMBER]); | |
| } | |
| public function testCreateRedirectForm() | |
| { | |
| $result = new Bob_Payment_Adyen_Common_Data_PaymentResult(); | |
| $result->setIssuerUrl('http://issuerurl'); | |
| $result->setPaRequest('parequest'); | |
| $result->setMd('themd'); | |
| $form = new Transfer_Sales_RedirectForm(); | |
| $form->setAction('http://issuerurl'); | |
| $collection = new Transfer_Sales_RedirectForm_FieldCollection(); | |
| $collection->addItem(new Transfer_Sales_RedirectForm_Field(['name' => 'PaReq', 'val' => 'parequest'])); | |
| $collection->addItem(new Transfer_Sales_RedirectForm_Field(['name' => 'MD', 'val' => 'themd'])); | |
| $collection->addItem(new Transfer_Sales_RedirectForm_Field(['name' => 'TermUrl', 'val' => 'http://zalora.tld/redirecturl'])); | |
| $form->setFormFields($collection); | |
| $form->setAutoPost(true); | |
| $form->setMethod("post"); | |
| $this->assertEquals($form, $this->model->createRedirectForm($result, "Desktop")); | |
| } | |
| public function testCreateRedirectFormQuicksilver() | |
| { | |
| $result = new Bob_Payment_Adyen_Common_Data_PaymentResult(); | |
| $result->setIssuerUrl('http://issuerurl'); | |
| $result->setPaRequest('parequest'); | |
| $result->setMd('themd'); | |
| $form = new Transfer_Sales_RedirectForm(); | |
| $form->setAction('http://issuerurl'); | |
| $collection = new Transfer_Sales_RedirectForm_FieldCollection(); | |
| $collection->addItem(new Transfer_Sales_RedirectForm_Field(['name' => 'PaReq', 'val' => 'parequest'])); | |
| $collection->addItem(new Transfer_Sales_RedirectForm_Field(['name' => 'MD', 'val' => 'themd'])); | |
| $collection->addItem(new Transfer_Sales_RedirectForm_Field(['name' => 'TermUrl', 'val' => $this->config->getRedirectUrlQuicksilver() ])); | |
| $form->setFormFields($collection); | |
| $form->setAutoPost(true); | |
| $form->setMethod("post"); | |
| $this->assertEquals($form, $this->model->createRedirectForm($result, "quicksilver")); | |
| } | |
| public function testGetAVSBillingAddress() | |
| { | |
| $method = $this->getMethod('getAVSBillingAddress'); | |
| $address = new Transfer_Sales_Order_Address([ | |
| 'postcode' => '1234', | |
| 'fk_customer_address_region' => '11', | |
| 'city' => 'city1', | |
| 'fk_customer_address_city' => '22', | |
| 'fk_customer_address_third_level' => '33', | |
| 'address1' => 'add 1', | |
| 'address2' => 'add 2' | |
| ]); | |
| $order = new Transfer_Sales_Order([ | |
| 'address_billing' => $address | |
| ]); | |
| $expected = new Bob_Payment_Adyen_CreditCard_Data_Address(); | |
| $expected->setCity('city1')->setStreet('add 1, add 2, third1, city1, region1') | |
| ->setHouseNumberOrName('add 1') | |
| ->setStateOrProvince('region1') | |
| ->setCountry('HK') | |
| ->setPostalCode('123456'); | |
| $this->assertEquals($expected, $method->invoke($this->model, $order)); | |
| } | |
| private function getParameterCollection() | |
| { | |
| $collection = new Transfer_Sales_Order_Payment_ParameterCollection(); | |
| $param = new Transfer_Sales_Order_Payment_Parameter([ | |
| 'name' => 'card_id', | |
| 'parameter_value' => 'cardid001' | |
| ]); | |
| $collection->addItem($param); | |
| $param = new Transfer_Sales_Order_Payment_Parameter([ | |
| 'name' => 'adyen-encrypted-data', | |
| 'parameter_value' => 'encrypteddatastring' | |
| ]); | |
| $collection->addItem($param); | |
| return $collection; | |
| } | |
| private function getMethod($methodName) | |
| { | |
| $reflectionClass = new ReflectionClass('Payment_Model_Adyen_Method_CreditCard'); | |
| $method = $reflectionClass->getMethod($methodName); | |
| $method->setAccessible(true); | |
| return $method; | |
| } | |
| private function setProperty($propertyName, $value) | |
| { | |
| $reflectionClass = new ReflectionClass($this->model); | |
| $property = $reflectionClass->getProperty($propertyName); | |
| $property->setAccessible(true); | |
| $property->setValue($this->model, $value); | |
| } | |
| public function testRefundFail() | |
| { | |
| // Arrange | |
| $expected = false; | |
| $amount = 90.00; | |
| $this->setPaymentReponse("403"); | |
| // Action | |
| $actual = $this->model->refund($this->transferSalesOrder, $amount); | |
| // Assert | |
| $this->assertEquals($expected, $actual); | |
| } | |
| public function testRefundSuccess() | |
| { | |
| // Arrange | |
| $expected = true; | |
| $amount = 300.00; | |
| $this->setPaymentReponse(Payment_Model_Adyen_Method_CreditCard::REFUND_RECEIVED); | |
| $this->model | |
| ->expects($this->any()) | |
| ->method('getTotalPaidPrice') | |
| ->willReturn($amount); | |
| // Action | |
| $method = $this->getMethod('refund'); | |
| $actual = $method->invokeArgs($this->model, [$this->transferSalesOrder, $amount]); | |
| // Assert | |
| $this->assertEquals($expected, $actual); | |
| } | |
| public function testRefundAmountGreaterThanTotalPaidPrice() | |
| { | |
| // Arrange | |
| $expected = false; | |
| $amount = 400.00; | |
| $this->setPaymentReponse(Payment_Model_Adyen_Method_CreditCard::REFUND_RECEIVED); | |
| $this->model | |
| ->expects($this->any()) | |
| ->method('getTotalPaidPrice') | |
| ->willReturn(300.00); | |
| // Action | |
| $method = $this->getMethod('refund'); | |
| $actual = $method->invokeArgs($this->model, [$this->transferSalesOrder, $amount]); | |
| // Assert | |
| $this->assertEquals($expected, $actual); | |
| } | |
| public function testRefundGreaterThanTotalAmount() | |
| { | |
| // Arrange | |
| $expected = false; | |
| $amount = 110.00; | |
| // Action | |
| $method = $this->getMethod('refund'); | |
| $actual = $method->invokeArgs($this->model, [$this->transferSalesOrder, $amount]); | |
| // Assert | |
| $this->assertEquals($expected, $actual); | |
| } | |
| public function testWithoutRefundAmount() | |
| { | |
| $expected = false; | |
| // Action | |
| $method = $this->getMethod('refund'); | |
| $actual = $method->invokeArgs($this->model, [$this->transferSalesOrder]); | |
| // Assert | |
| $this->assertEquals($expected, $actual); | |
| } | |
| public function testWithRefundAmountZero() | |
| { | |
| $expected = false; | |
| $amount = 0; | |
| // Action | |
| $method = $this->getMethod('refund'); | |
| $actual = $method->invokeArgs($this->model, [$this->transferSalesOrder, $amount]); | |
| // Assert | |
| $this->assertEquals($expected, $actual); | |
| } | |
| public function testGetTotalPaidPrice() | |
| { | |
| // Arrange | |
| $expected = 300.00; | |
| //set order transfer | |
| $method = $this->getMethod('getTotalPaidPrice'); | |
| $actual = $method->invokeArgs($this->model, [$this->transferSalesOrder]); | |
| // Assert | |
| $this->assertEquals($expected, $actual); | |
| } | |
| /** | |
| * @param string $response | |
| */ | |
| protected function setPaymentReponse(string $response) | |
| { | |
| $paymentResult = new Bob_Payment_Adyen_Common_Data_PaymentResult(); | |
| $paymentResult->setResponse($response); | |
| $response = new Bob_Payment_Adyen_Common_Message_RefundResult(); | |
| $response->setPaymentResult($paymentResult); | |
| $this->jsonClient | |
| ->expects($this->any()) | |
| ->method('refund') | |
| ->willReturn($response); | |
| } | |
| public function testGetAdditionalData() | |
| { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment