Skip to content

Commit

Permalink
[paypal][rest] Fixes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Mar 30, 2016
1 parent 269b0ba commit 13be306
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 54 deletions.
31 changes: 12 additions & 19 deletions Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,26 @@

namespace Payum\Paypal\Rest\Action;

use PayPal\Api\Payment;
use PayPal\Api\Payment as PaypalPayment;
use PayPal\Api\PaymentExecution;
use PayPal\Rest\ApiContext;
use Payum\Core\Action\GatewayAwareAction;
use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\ApiAwareTrait;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Capture;
use Payum\Core\Reply\HttpRedirect;

class CaptureAction extends GatewayAwareAction implements ApiAwareInterface
class CaptureAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
{
/**
* @param ApiContext
*/
protected $api;
use ApiAwareTrait;
use GatewayAwareTrait;

/**
* {@inheritDoc}
*/
public function setApi($api)
public function __construct()
{
if (false == $api instanceof ApiContext) {
throw new UnsupportedApiException('Given api is not supported. Supported api is instance of ApiContext');
}

$this->api = $api;
$this->apiClass = ApiContext::class;
}

/**
Expand All @@ -39,7 +32,7 @@ public function execute($request)
/** @var $request Capture */
RequestNotSupportedException::assertSupports($this, $request);

/** @var Payment $model */
/** @var PaypalPayment $model */
$model = $request->getModel();

if (
Expand Down Expand Up @@ -84,7 +77,7 @@ public function supports($request)
{
return
$request instanceof Capture &&
$request->getModel() instanceof Payment
$request->getModel() instanceof PaypalPayment
;
}
}
16 changes: 10 additions & 6 deletions Action/SyncAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Payum\Paypal\Rest\Action;

use PayPal\Api\Payment;
use Payum\Core\Action\GatewayAwareAction;
use PayPal\Api\Payment as PaypalPayment;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Request\Sync;

class SyncAction extends GatewayAwareAction
class SyncAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* {@inheritDoc}
*/
Expand All @@ -17,10 +21,10 @@ public function execute($request)
/** @var $request Sync */
RequestNotSupportedException::assertSupports($this, $request);

/** @var Payment $model */
/** @var PaypalPayment $model */
$model = $request->getModel();

$payment = Payment::get($model->id);
$payment = PaypalPayment::get($model->id);

$model->fromArray($payment->toArray());
}
Expand All @@ -32,7 +36,7 @@ public function supports($request)
{
return
$request instanceof Sync &&
$request->getModel() instanceof Payment
$request->getModel() instanceof PaypalPayment
;
}
}
18 changes: 8 additions & 10 deletions PaypalRestGatewayFactory.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php
namespace Payum\Paypal\Rest;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\GatewayFactory as CoreGatewayFactory;
use Payum\Core\GatewayFactory;
use Payum\Core\GatewayFactoryInterface;
use Payum\Paypal\Rest\Action\CaptureAction;
use Payum\Paypal\Rest\Action\StatusAction;
use Payum\Paypal\Rest\Action\SyncAction;
Expand All @@ -19,32 +17,32 @@ class PaypalRestGatewayFactory extends GatewayFactory
*/
protected function populateConfig(ArrayObject $config)
{
if (!class_exists('\PayPal\Api\Payment')) {
if (false == class_exists(ApiContext::class)) {
throw new \LogicException('You must install "paypal/rest-api-sdk-php" library.');
}

$config->defaults(array(
$config->defaults([
'payum.factory_name' => 'paypal_rest',
'payum.factory_title' => 'PayPal Rest',

'payum.action.capture' => new CaptureAction(),
'payum.action.sync' => new SyncAction(),
'payum.action.status' => new StatusAction(),
));
]);

if (false == $config['payum.api']) {
$config['payum.default_options'] = array(
$config['payum.default_options'] = [
'client_id' => '',
'client_secret' => '',
'config_path' => '',
);
];
$config->defaults($config['payum.default_options']);

$config['payum.required_options'] = array('client_id', 'client_secret', 'config_path');
$config['payum.required_options'] = ['client_id', 'client_secret', 'config_path'];
$config['payum.api'] = function (ArrayObject $config) {
$config->validateNotEmpty($config['payum.required_options']);

if (!defined('PP_CONFIG_PATH')) {
if (false == defined('PP_CONFIG_PATH')) {
define('PP_CONFIG_PATH', $config['config_path']);
} elseif (PP_CONFIG_PATH !== $config['config_path']) {
throw new InvalidArgumentException(sprintf('Given "config_path" is invalid. Should be equal to the defined "PP_CONFIG_PATH": %s.', PP_CONFIG_PATH));
Expand Down
10 changes: 6 additions & 4 deletions Tests/Action/CaptureActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Payum\Paypal\Rest\Tests\Action;

use PayPal\Api\Payment as PaypalPayment;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use Payum\Core\Exception\UnsupportedApiException;
use Payum\Paypal\Rest\Action\CaptureAction;
use Payum\Paypal\Rest\Model\PaymentDetails;
use Payum\Core\Request\Capture;
Expand Down Expand Up @@ -72,7 +75,7 @@ public function shouldSupportCapture()
{
$action = new CaptureAction();

$request = new Capture($this->getMock('PayPal\Api\Payment'));
$request = new Capture($this->getMock(PaypalPayment::class));

$this->assertTrue($action->supports($request));
}
Expand All @@ -84,9 +87,8 @@ public function shouldSupportApiContext()
{
$action = new CaptureAction();

$tokenMock = $this->getMockBuilder('PayPal\Auth\OAuthTokenCredential')
->disableOriginalConstructor()
->getMock();
/** @var OAuthTokenCredential $tokenMock */
$tokenMock = $this->getMock(OAuthTokenCredential::class, [], [], '', false);

$apiContext = new ApiContext($tokenMock);

Expand Down
34 changes: 19 additions & 15 deletions Tests/PaypalRestGatewayFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Payum\Paypal\Rest\Tests;

use Payum\Core\CoreGatewayFactory;
use Payum\Core\Gateway;
use Payum\Core\GatewayFactory;
use Payum\Core\GatewayFactoryInterface;
use Payum\Paypal\Rest\PaypalRestGatewayFactory;

class PaypalRestGatewayFactoryTest extends \PHPUnit_Framework_TestCase
Expand All @@ -11,9 +15,9 @@ class PaypalRestGatewayFactoryTest extends \PHPUnit_Framework_TestCase
*/
public function shouldSubClassGatewayFactory()
{
$rc = new \ReflectionClass('Payum\Paypal\Rest\PaypalRestGatewayFactory');
$rc = new \ReflectionClass(PaypalRestGatewayFactory::class);

$this->assertTrue($rc->isSubclassOf('Payum\Core\GatewayFactory'));
$this->assertTrue($rc->isSubclassOf(GatewayFactory::class));
}

/**
Expand All @@ -31,17 +35,17 @@ public function shouldCreateCoreGatewayFactoryIfNotPassed()
{
$factory = new PaypalRestGatewayFactory();

$this->assertAttributeInstanceOf('Payum\Core\CoreGatewayFactory', 'coreGatewayFactory', $factory);
$this->assertAttributeInstanceOf(CoreGatewayFactory::class, 'coreGatewayFactory', $factory);
}

/**
* @test
*/
public function shouldUseCoreGatewayFactoryPassedAsSecondArgument()
{
$coreGatewayFactory = $this->getMock('Payum\Core\GatewayFactoryInterface');
$coreGatewayFactory = $this->getMock(GatewayFactoryInterface::class);

$factory = new PaypalRestGatewayFactory(array(), $coreGatewayFactory);
$factory = new PaypalRestGatewayFactory([], $coreGatewayFactory);

$this->assertAttributeSame($coreGatewayFactory, 'coreGatewayFactory', $factory);
}
Expand All @@ -53,13 +57,13 @@ public function shouldAllowCreateGateway()
{
$factory = new PaypalRestGatewayFactory();

$gateway = $factory->create(array(
$gateway = $factory->create([
'client_id' => 'cId',
'client_secret' => 'cSecret',
'config_path' => __DIR__,
));
]);

$this->assertInstanceOf('Payum\Core\Gateway', $gateway);
$this->assertInstanceOf(Gateway::class, $gateway);

$this->assertAttributeNotEmpty('apis', $gateway);
$this->assertAttributeNotEmpty('actions', $gateway);
Expand All @@ -75,9 +79,9 @@ public function shouldAllowCreateGatewayWithCustomApi()
{
$factory = new PaypalRestGatewayFactory();

$gateway = $factory->create(array('payum.api' => new \stdClass()));
$gateway = $factory->create(['payum.api' => new \stdClass()]);

$this->assertInstanceOf('Payum\Core\Gateway', $gateway);
$this->assertInstanceOf(Gateway::class, $gateway);

$this->assertAttributeNotEmpty('apis', $gateway);
$this->assertAttributeNotEmpty('actions', $gateway);
Expand All @@ -104,10 +108,10 @@ public function shouldAllowCreateGatewayConfig()
*/
public function shouldAddDefaultConfigPassedInConstructorWhileCreatingGatewayConfig()
{
$factory = new PaypalRestGatewayFactory(array(
$factory = new PaypalRestGatewayFactory([
'foo' => 'fooVal',
'bar' => 'barVal',
));
]);

$config = $factory->createConfig();

Expand All @@ -132,7 +136,7 @@ public function shouldConfigContainDefaultOptions()
$this->assertInternalType('array', $config);

$this->assertArrayHasKey('payum.default_options', $config);
$this->assertEquals(array('client_id' => '', 'client_secret' => '', 'config_path' => ''), $config['payum.default_options']);
$this->assertEquals(['client_id' => '', 'client_secret' => '', 'config_path' => ''], $config['payum.default_options']);
}

/**
Expand Down Expand Up @@ -175,10 +179,10 @@ public function shouldThrowIfRequiredOptionsNotPassed()
public function shouldThrowIfConfigPathOptionsNotEqualPaypalPath()
{
$factory = new PaypalRestGatewayFactory();
$factory->create(array(
$factory->create([
'client_id' => 'cId',
'client_secret' => 'cSecret',
'config_path' => dirname(__DIR__),
));
]);
}
}

0 comments on commit 13be306

Please sign in to comment.