-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
165 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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
253 changes: 141 additions & 112 deletions
253
tests/TestCase/Controller/Component/PayPalComponentTest.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,142 @@ | ||
<?php | ||
|
||
namespace PayPal\Test\TestCase\Controller\Component; | ||
|
||
use Cake\Controller\ComponentRegistry; | ||
use Cake\Core\Configure; | ||
use Cake\Event\Event; | ||
use Cake\TestSuite\TestCase; | ||
|
||
|
||
use PayPal\Controller\Component\PayPalComponent; | ||
use PayPal\Model\Table\PayPalPaymentsTable; | ||
|
||
/** | ||
* @var PayPalComponent PayPal | ||
*/ | ||
class PayPalComponentTest extends TestCase | ||
{ | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->Controller = $this->getMockBuilder('\Cake\Controller\Controller') | ||
->setMethods(['redirect']) | ||
->getMock(); | ||
|
||
$registry = new ComponentRegistry($this->Controller); | ||
$this->PayPal = new PayPalComponent($registry); | ||
|
||
$event = new Event('Controller.startup', $this->Controller); | ||
$this->PayPal->startup($event); | ||
|
||
$this->items = new \ReflectionProperty(PayPalComponent::class, 'items'); | ||
$this->items->setAccessible(true); | ||
} | ||
|
||
public function testInitialized() | ||
{ | ||
$this->assertEquals(Configure::read('PayPal'), $this->PayPal->config); | ||
$this->assertInstanceOf(PayPalPaymentsTable::class, $this->PayPal->PayPalPayments); | ||
} | ||
|
||
public function testAddArticle() | ||
{ | ||
$this->PayPal->AddArticle('foo', 3, 1234); | ||
$items = $this->items->getValue($this->PayPal); | ||
$this->assertEquals(1, count($items)); | ||
$this->assertEquals('foo', $items[0]->getName()); | ||
$this->assertEquals(3, $items[0]->getQuantity()); | ||
$this->assertEquals(12.34, $items[0]->getPrice()); | ||
$this->assertEquals('EUR', $items[0]->getCurrency()); | ||
$this->assertEquals(null, $items[0]->getSku()); | ||
} | ||
|
||
public function testAddArticleWithId() | ||
{ | ||
$this->PayPal->AddArticle('foo', 3, 1234, 'id'); | ||
$items = $this->items->getValue($this->PayPal); | ||
$this->assertEquals(1, count($items)); | ||
$this->assertEquals('foo', $items['id']->getName()); | ||
$this->assertEquals(3, $items['id']->getQuantity()); | ||
$this->assertEquals(12.34, $items['id']->getPrice()); | ||
$this->assertEquals('EUR', $items['id']->getCurrency()); | ||
$this->assertEquals('id', $items['id']->getSku()); | ||
} | ||
|
||
public function testPaymentRedirectCreatePaymentFails() | ||
{ | ||
$this->PayPal->PayPalPayments = $this->getMockForModel('PayPal.PayPalPayments', ['createPayment']); | ||
$this->PayPal->PayPalPayments->expects($this->once()) | ||
->method('createPayment') | ||
->with('ri', $this->anything(), 'https://ok', 'https://cancel') | ||
->will($this->throwException(new \Exception('dummy'))); | ||
$this->expectException(\Exception::class, 'dummy'); | ||
$this->PayPal->PaymentRedirect('ri', 'https://ok', 'https://cancel', 'descr'); | ||
} | ||
|
||
public function testPaymentRedirect() | ||
{ | ||
$this->PayPal->AddArticle('foo', 2, 1234, 'bar'); | ||
$this->PayPal->Shipping = 100; | ||
|
||
$this->PayPal->PayPalPayments = $this->getMockForModel('PayPal.PayPalPayments', ['createPayment']); | ||
|
||
$this->PayPal->PayPalPayments->expects($this->once()) | ||
->method('createPayment') | ||
->with('ri', $this->callback(function($payment) { | ||
$this->assertEquals(29.62 + 1, $payment->getTransactions()[0]->getAmount()->getTotal()); | ||
return true; | ||
}), 'https://ok', 'https://cancel') | ||
->will($this->returnCallback(function($remittanceIdentifier, $payment, $okUrl, $cancelUrl){ | ||
$links = new \PayPal\Api\Links(); | ||
$links | ||
->setRel('approval_url') | ||
->setHref('https://pay'); | ||
$payment->setLinks([$links]); | ||
return true; | ||
})); | ||
|
||
$this->Controller->expects($this->once()) | ||
->method('redirect') | ||
->with('https://pay'); | ||
|
||
$this->PayPal->PaymentRedirect('ri', 'https://ok', 'https://cancel', 'descr'); | ||
} | ||
|
||
public function testNeutralizeFee() | ||
{ | ||
$actual = PayPalComponent::NeutralizeFee(1234); | ||
$this->assertEquals(1314, $actual); | ||
} | ||
<?php | ||
|
||
namespace PayPal\Test\TestCase\Controller\Component; | ||
|
||
use Cake\Controller\ComponentRegistry; | ||
use Cake\Core\Configure; | ||
use Cake\Event\Event; | ||
use Cake\TestSuite\TestCase; | ||
|
||
|
||
use PayPal\Controller\Component\PayPalComponent; | ||
use PayPal\Model\Table\PayPalPaymentsTable; | ||
|
||
/** | ||
* @var PayPalComponent PayPal | ||
*/ | ||
class PayPalComponentTest extends TestCase | ||
{ | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->Controller = $this->getMockBuilder('\Cake\Controller\Controller') | ||
->setMethods(['redirect']) | ||
->getMock(); | ||
|
||
$registry = new ComponentRegistry($this->Controller); | ||
$this->PayPal = new PayPalComponent($registry); | ||
|
||
$event = new Event('Controller.startup', $this->Controller); | ||
$this->PayPal->startup($event); | ||
|
||
$this->items = new \ReflectionProperty(PayPalComponent::class, 'items'); | ||
$this->items->setAccessible(true); | ||
} | ||
|
||
public function testInitialized() | ||
{ | ||
$this->assertEquals(Configure::read('PayPal'), $this->PayPal->config); | ||
$this->assertInstanceOf(PayPalPaymentsTable::class, $this->PayPal->PayPalPayments); | ||
} | ||
|
||
public function testAddArticle() | ||
{ | ||
$this->PayPal->AddArticle('foo', 3, 1234); | ||
$items = $this->items->getValue($this->PayPal); | ||
$this->assertEquals(1, count($items)); | ||
$this->assertEquals('foo', $items[0]->getName()); | ||
$this->assertEquals(3, $items[0]->getQuantity()); | ||
$this->assertEquals(12.34, $items[0]->getPrice()); | ||
$this->assertEquals('EUR', $items[0]->getCurrency()); | ||
$this->assertEquals(null, $items[0]->getSku()); | ||
} | ||
|
||
public function testAddArticleWithId() | ||
{ | ||
$this->PayPal->AddArticle('foo', 3, 1234, 'id'); | ||
$items = $this->items->getValue($this->PayPal); | ||
$this->assertEquals(1, count($items)); | ||
$this->assertEquals('foo', $items['id']->getName()); | ||
$this->assertEquals(3, $items['id']->getQuantity()); | ||
$this->assertEquals(12.34, $items['id']->getPrice()); | ||
$this->assertEquals('EUR', $items['id']->getCurrency()); | ||
$this->assertEquals('id', $items['id']->getSku()); | ||
} | ||
|
||
public function testPaymentRedirectCreatePaymentFails() | ||
{ | ||
$this->PayPal->PayPalPayments = $this->getMockForModel('PayPal.PayPalPayments', ['createPayment']); | ||
$this->PayPal->PayPalPayments->expects($this->once()) | ||
->method('createPayment') | ||
->with('ri', $this->anything(), 'https://ok', 'https://cancel') | ||
->will($this->throwException(new \Exception('dummy'))); | ||
$this->expectException(\Exception::class, 'dummy'); | ||
$this->PayPal->PaymentRedirect('ri', 'https://ok', 'https://cancel', 'descr'); | ||
} | ||
|
||
public function testPaymentRedirect() | ||
{ | ||
$this->PayPal->AddArticle('foo', 2, 1234, 'bar'); | ||
$this->PayPal->Shipping = 100; | ||
|
||
$this->PayPal->PayPalPayments = $this->getMockForModel('PayPal.PayPalPayments', ['createPayment']); | ||
|
||
$this->PayPal->PayPalPayments->expects($this->once()) | ||
->method('createPayment') | ||
->with('ri', $this->callback(function($payment) { | ||
$this->assertEquals(29.62 + 1, $payment->getTransactions()[0]->getAmount()->getTotal()); | ||
return true; | ||
}), 'https://ok', 'https://cancel') | ||
->will($this->returnCallback(function($remittanceIdentifier, $payment, $okUrl, $cancelUrl){ | ||
$links = new \PayPal\Api\Links(); | ||
$links | ||
->setRel('approval_url') | ||
->setHref('https://pay'); | ||
$payment->setLinks([$links]); | ||
return true; | ||
})); | ||
|
||
$this->Controller->expects($this->once()) | ||
->method('redirect') | ||
->with('https://pay'); | ||
|
||
$this->PayPal->PaymentRedirect('ri', 'https://ok', 'https://cancel', 'descr'); | ||
} | ||
|
||
public function testPaymentRedirectWithTax() | ||
{ | ||
$this->PayPal->AddArticle('foo', 2, 1234, 'bar'); | ||
$this->PayPal->Shipping = 100; | ||
|
||
$this->PayPal->PayPalPayments = $this->getMockForModel('PayPal.PayPalPayments', ['createPayment']); | ||
|
||
$this->PayPal->PayPalPayments->expects($this->once()) | ||
->method('createPayment') | ||
->with('ri', $this->callback(function($payment) { | ||
$this->assertEquals(12.34 * 2 + 3.21 + 1, $payment->getTransactions()[0]->getAmount()->getTotal()); | ||
return true; | ||
}), 'https://ok', 'https://cancel') | ||
->will($this->returnCallback(function($remittanceIdentifier, $payment, $okUrl, $cancelUrl){ | ||
$links = new \PayPal\Api\Links(); | ||
$links | ||
->setRel('approval_url') | ||
->setHref('https://pay'); | ||
$payment->setLinks([$links]); | ||
return true; | ||
})); | ||
|
||
$this->Controller->expects($this->once()) | ||
->method('redirect') | ||
->with('https://pay'); | ||
|
||
$this->PayPal->PaymentRedirect('ri', 'https://ok', 'https://cancel', 'descr', 321); | ||
} | ||
|
||
public function testNeutralizeFee() | ||
{ | ||
$actual = PayPalComponent::NeutralizeFee(1234); | ||
$this->assertEquals(1314, $actual); | ||
} | ||
} |
This file contains 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