Skip to content

Commit

Permalink
[security] Add createCancelToken method
Browse files Browse the repository at this point in the history
- deprecate GenericTokenFactoryInterface.
  • Loading branch information
makasim committed Sep 14, 2016
1 parent 9abb861 commit c0ee20e
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Security/GenericTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ public function createRefundToken($gatewayName, $model, $afterPath = null, array
return $this->createToken($gatewayName, $model, $refundPath, [], $afterUrl);
}

/**
* {@inheritDoc}
*/
public function createCancelToken($gatewayName, $model, $afterPath = null, array $afterParameters = [])
{
$cancelPath = $this->getPath('cancel');

$afterUrl = null;
if ($afterPath) {
$afterUrl = $this->createToken($gatewayName, $model, $afterPath, $afterParameters)->getTargetUrl();
}

return $this->createToken($gatewayName, $model, $cancelPath, [], $afterUrl);
}

/**
* {@inheritDoc}
*/
Expand Down
3 changes: 3 additions & 0 deletions Security/GenericTokenFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Payum\Core\Security;

/**
* @deprecated since 1.3.7 and will be removed in 2.0
*/
interface GenericTokenFactoryInterface extends TokenFactoryInterface
{
/**
Expand Down
125 changes: 125 additions & 0 deletions Tests/Security/GenericTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,131 @@ public function shouldAllowCreateRefundTokenWithoutAfterPath()
$this->assertSame($refundToken, $actualToken);
}

/**
* @test
*
* @expectedException \Payum\Core\Exception\LogicException
* @expectedExceptionMessage The path "cancel" is not found. Possible paths are foo, bar
*/
public function throwIfCancelPathNotConfigured()
{
$gatewayName = 'theGatewayName';
$model = new \stdClass();
$afterPath = 'theAfterPath';
$afterParameters = array('after' => 'val');

$tokenFactoryMock = $this->createTokenFactoryMock();
$tokenFactoryMock
->expects($this->never())
->method('createToken')
;


$factory = new GenericTokenFactory($tokenFactoryMock, array('foo' => 'fooPath', 'bar' => 'barPath'));

$factory->createCancelToken(
$gatewayName,
$model,
$afterPath,
$afterParameters
);
}

/**
* @test
*/
public function shouldAllowCreateCancelToken()
{
$gatewayName = 'theGatewayName';
$model = new \stdClass();
$cancelPath = 'theCancelPath';
$afterPath = 'theAfterPath';
$afterUrl = 'theAfterUrl';
$afterParameters = array('after' => 'val');

$afterToken = new Token();
$afterToken->setTargetUrl($afterUrl);

$cancelToken = new Token();

$tokenFactoryMock = $this->createTokenFactoryMock();
$tokenFactoryMock
->expects($this->at(0))
->method('createToken')
->with(
$gatewayName,
$this->identicalTo($model),
$afterPath,
$afterParameters,
null,
[]
)
->willReturn($afterToken)
;
$tokenFactoryMock
->expects($this->at(1))
->method('createToken')
->with(
$gatewayName,
$this->identicalTo($model),
$cancelPath,
[],
$afterUrl,
[]
)
->willReturn($cancelToken)
;


$factory = new GenericTokenFactory($tokenFactoryMock, array(
'cancel' => $cancelPath
));

$actualToken = $factory->createCancelToken(
$gatewayName,
$model,
$afterPath,
$afterParameters
);

$this->assertSame($cancelToken, $actualToken);
}

/**
* @test
*/
public function shouldAllowCreateCancelTokenWithoutAfterPath()
{
$gatewayName = 'theGatewayName';
$model = new \stdClass();
$cancelPath = 'theCancelPath';

$cancelToken = new Token();

$tokenFactoryMock = $this->createTokenFactoryMock();
$tokenFactoryMock
->expects($this->once())
->method('createToken')
->with(
$gatewayName,
$this->identicalTo($model),
$cancelPath,
[],
null,
[]
)
->willReturn($cancelToken)
;

$factory = new GenericTokenFactory($tokenFactoryMock, array(
'cancel' => $cancelPath
));

$actualToken = $factory->createCancelToken($gatewayName, $model);

$this->assertSame($cancelToken, $actualToken);
}

/**
* @test
*
Expand Down

0 comments on commit c0ee20e

Please sign in to comment.