Skip to content

Commit

Permalink
MAG2-2 Final checkin of the unit tests
Browse files Browse the repository at this point in the history
All testable classes now have unittests
  • Loading branch information
FatchipRobert committed May 10, 2017
1 parent 3f974fd commit 0d9234b
Show file tree
Hide file tree
Showing 18 changed files with 1,760 additions and 12 deletions.
11 changes: 4 additions & 7 deletions Model/Api/Request/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,20 @@ protected function validateParameters()

/**
* Send the previously prepared request, log request and response into the database and return the response
*
* @param bool $blOnlyGetUrl
* @return array
*/
protected function send($blOnlyGetUrl = false)
protected function send()
{
if (!$this->validateParameters()) {// all base parameters existing?
return ["errormessage" => "Payone API Setup Data not complete (API-URL, MID, AID, PortalID, Key, Mode)"];
}

$sRequestUrl = $this->apiHelper->getRequestUrl($this->getParameters(), $this->sApiUrl);
if ($blOnlyGetUrl === true) {// sometimes you only need the request url
return $sRequestUrl;
}

$aResponse = $this->apiHelper->sendApiRequest($sRequestUrl); // send request to PAYONE
$this->apiLog->addApiLogEntry($this, $aResponse, $aResponse['status']); // log request to db

return $aResponse;
}
}
3 changes: 0 additions & 3 deletions Model/Methods/PayoneMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ abstract class PayoneMethod extends BaseMethod
*/
public function getClearingtype()
{
if ($this->sClearingtype === false) {
throw new LocalizedException(__('Clearingtype not implemented for this payment method'));
}
return $this->sClearingtype;
}

Expand Down
127 changes: 127 additions & 0 deletions Test/Unit/Model/Api/InvoiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

/**
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
*
* PHP version 5
*
* @category Payone
* @package Payone_Magento2_Plugin
* @author FATCHIP GmbH <[email protected]>
* @copyright 2003 - 2017 Payone GmbH
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
* @link http://www.payone.de
*/

namespace Payone\Core\Test\Unit\Model\Api;

use Payone\Core\Model\Api\Invoice as ClassToTest;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Payone\Core\Helper\Toolkit;
use Payone\Core\Model\Api\Request\Authorization;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Item;

class InvoiceTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ClassToTest
*/
private $classToTest;

/**
* @var Toolkit
*/
private $toolkitHelper;

protected function setUp()
{
$objectManager = new ObjectManager($this);

$this->toolkitHelper = $this->getMockBuilder(Toolkit::class)->disableOriginalConstructor()->getMock();
$this->toolkitHelper->method('getInvoiceAppendix')->willReturn('invoice appendix');
$this->toolkitHelper->method('getConfigParam')->willReturn('sku');
$this->toolkitHelper->expects($this->any())
->method('formatNumber')
->willReturnMap([
[100, 2, '100.00'],
[5, 2, '5.00'],
[-5, 2, '-5.00'],
[90, 2, '90.00'],
[105, 2, '105.00'],
]);

$this->classToTest = $objectManager->getObject(ClassToTest::class, [
'toolkitHelper' => $this->toolkitHelper
]);
}

/**
* Returns item mock object
*
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function getItemMock()
{
$item = $this->getMockBuilder(Item::class)->disableOriginalConstructor()->getMock();
$item->method('isDummy')->willReturn(false);
$item->method('getProductId')->willReturn('12345');
$item->method('getQtyOrdered')->willReturn('1');
$item->method('getSku')->willReturn('test_123');
$item->method('getPriceInclTax')->willReturn('100');
$item->method('getName')->willReturn('Test product');
$item->method('getTaxPercent')->willReturn('19');
return $item;
}

public function testAddProductInfo()
{
$authorization = $this->getMockBuilder(Authorization::class)->disableOriginalConstructor()->getMock();

$items = [$this->getItemMock()];

$expected = 90;

$order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
$order->method('getAllItems')->willReturn($items);
$order->method('getBaseShippingInclTax')->willReturn(-5);
$order->method('getBaseDiscountAmount')->willReturn(-5);
$order->method('getCouponCode')->willReturn('test');
$order->method('getGrandTotal')->willReturn($expected);

$result = $this->classToTest->addProductInfo($authorization, $order, false);
$this->assertEquals($expected, $result);
}

public function testAddProductInfoSurcharge()
{
$authorization = $this->getMockBuilder(Authorization::class)->disableOriginalConstructor()->getMock();

$items = [$this->getItemMock()];

$expected = 100;

$order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
$order->method('getAllItems')->willReturn($items);
$order->method('getBaseShippingInclTax')->willReturn(5);
$order->method('getBaseDiscountAmount')->willReturn(-5);
$order->method('getCouponCode')->willReturn('test');
$order->method('getGrandTotal')->willReturn($expected);

$positions = ['12345' => ['amount' => '1']];

$result = $this->classToTest->addProductInfo($authorization, $order, $positions);
$this->assertEquals($expected, $result);
}
}
204 changes: 204 additions & 0 deletions Test/Unit/Model/Api/Request/AddresscheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

/**
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
*
* PHP version 5
*
* @category Payone
* @package Payone_Magento2_Plugin
* @author FATCHIP GmbH <[email protected]>
* @copyright 2003 - 2017 Payone GmbH
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License
* @link http://www.payone.de
*/

namespace Payone\Core\Test\Unit\Model\Api\Request;

use Magento\Quote\Model\Quote\Address;
use Payone\Core\Model\Api\Request\Addresscheck as ClassToTest;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Payone\Core\Helper\Api;
use Payone\Core\Model\ResourceModel\CheckedAddresses;
use Payone\Core\Helper\Shop;

class AddresscheckTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ClassToTest
*/
private $classToTest;

/**
* @var Api|\PHPUnit_Framework_MockObject_MockObject
*/
private $apiHelper;

/**
* @var CheckedAddresses|\PHPUnit_Framework_MockObject_MockObject
*/
private $addressesChecked;

/**
* @var Shop|\PHPUnit_Framework_MockObject_MockObject
*/
private $shopHelper;

protected function setUp()
{
$objectManager = new ObjectManager($this);

$this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock();

$this->addressesChecked = $this->getMockBuilder(CheckedAddresses::class)->disableOriginalConstructor()->getMock();
$this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock();

$this->classToTest = $objectManager->getObject(ClassToTest::class, [
'apiHelper' => $this->apiHelper,
'shopHelper' => $this->shopHelper,
'addressesChecked' => $this->addressesChecked
]);
}

public function testSendRequestTrueNotEnabled()
{
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();

$this->shopHelper->method('getConfigParam')->willReturn(false);

$result = $this->classToTest->sendRequest($address, true);
$this->assertTrue($result);
}

public function testSendRequestTrueNoBilling()
{
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();

$this->shopHelper->expects($this->any())
->method('getConfigParam')
->willReturnMap([
['enabled', 'address_check', 'payone_protect', null, true],
['check_billing', 'address_check', 'payone_protect', null, 'NO']
]);

$result = $this->classToTest->sendRequest($address, true);
$this->assertTrue($result);
}

public function testSendRequestTrueNoShipping()
{
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();

$this->shopHelper->expects($this->any())
->method('getConfigParam')
->willReturnMap([
['enabled', 'address_check', 'payone_protect', null, true],
['check_shipping', 'address_check', 'payone_protect', null, 'NO']
]);

$result = $this->classToTest->sendRequest($address, false);
$this->assertTrue($result);
}

public function testSendRequestInvalidTypePE()
{
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
$address->method('getCountryId')->willReturn('FR');

$this->shopHelper->expects($this->any())
->method('getConfigParam')
->willReturnMap([
['enabled', 'address_check', 'payone_protect', null, true],
['check_shipping', 'address_check', 'payone_protect', null, 'PE']
]);

$result = $this->classToTest->sendRequest($address, false);
$expected = ['wrongCountry' => true];
$this->assertEquals($expected, $result);
}

public function testSendRequestInvalidTypeBA()
{
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
$address->method('getCountryId')->willReturn('XY');

$this->shopHelper->expects($this->any())
->method('getConfigParam')
->willReturnMap([
['enabled', 'address_check', 'payone_protect', null, true],
['check_shipping', 'address_check', 'payone_protect', null, 'BA']
]);

$result = $this->classToTest->sendRequest($address, false);
$expected = ['wrongCountry' => true];
$this->assertEquals($expected, $result);
}

public function testSendRequestNotChecked()
{
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
$address->method('getCountryId')->willReturn('DE');
$address->method('getFirstname')->willReturn('Paul');
$address->method('getLastname')->willReturn('Paytest');
$address->method('getCompany')->willReturn('Testcompany Ltd.');
$address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
$address->method('getPostcode')->willReturn('12345');
$address->method('getCity')->willReturn('Berlin');
$address->method('getRegionCode')->willReturn('Berlin');

$this->shopHelper->expects($this->any())
->method('getConfigParam')
->willReturnMap([
['enabled', 'address_check', 'payone_protect', null, true],
['check_shipping', 'address_check', 'payone_protect', null, 'PE'],
['mode', 'address_check', 'payone_protect', null, 'test'],
['aid', 'global', 'payone_general', null, 'PE']
]);

$this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(false);

$response = ['status' => 'VALID'];
$this->apiHelper->method('sendApiRequest')->willReturn($response);

$result = $this->classToTest->sendRequest($address, false);
$this->assertEquals($response, $result);
}

public function testSendRequestChecked()
{
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock();
$address->method('getCountryId')->willReturn('DE');
$address->method('getFirstname')->willReturn('Paul');
$address->method('getLastname')->willReturn('Paytest');
$address->method('getCompany')->willReturn('Testcompany Ltd.');
$address->method('getStreet')->willReturn(['Teststr. 5', '1st floor']);
$address->method('getPostcode')->willReturn('12345');
$address->method('getCity')->willReturn('Berlin');
$address->method('getRegionCode')->willReturn('Berlin');

$this->shopHelper->expects($this->any())
->method('getConfigParam')
->willReturnMap([
['enabled', 'address_check', 'payone_protect', null, true],
['check_shipping', 'address_check', 'payone_protect', null, 'PE'],
['mode', 'address_check', 'payone_protect', null, 'test'],
['aid', 'global', 'payone_general', null, 'PE']
]);

$this->addressesChecked->method('wasAddressCheckedBefore')->willReturn(true);

$result = $this->classToTest->sendRequest($address, false);
$this->assertTrue($result);
}
}
Loading

0 comments on commit 0d9234b

Please sign in to comment.