-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BB-22354: Stripe - Invalid processing of unsupported events
- Loading branch information
1 parent
6e5b815
commit 5ee4fdd
Showing
3 changed files
with
175 additions
and
17 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Oro\Bundle\StripeBundle\Model; | ||
|
||
/** | ||
* Used for unsupported events handling | ||
*/ | ||
class UnsupportedResponse extends AbstractResponseObject implements ResponseObjectInterface | ||
{ | ||
public function getStatus(): string | ||
{ | ||
return 'failed'; | ||
} | ||
|
||
public function getIdentifier(): string | ||
{ | ||
return 'null'; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return []; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/Oro/Bundle/StripeBundle/Tests/Unit/Event/StripeSDKEventFactoryTest.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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Oro\Bundle\StripeBundle\Tests\Unit\Event; | ||
|
||
use Oro\Bundle\StripeBundle\Event\StripeSDKEventFactory; | ||
use Oro\Bundle\StripeBundle\Method\Config\Provider\StripePaymentConfigsProvider; | ||
use Oro\Bundle\StripeBundle\Method\Config\StripePaymentConfig; | ||
use Oro\Bundle\StripeBundle\Model\PaymentIntentResponse; | ||
use Oro\Bundle\StripeBundle\Model\UnsupportedResponse; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Stripe\Event; | ||
use Stripe\PaymentIntent; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class StripeSDKEventFactoryTest extends TestCase | ||
{ | ||
private MockObject|StripePaymentConfigsProvider $paymentConfigsProvider; | ||
private MockObject|StripeSDKEventFactory $eventFactory; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->paymentConfigsProvider = $this->createMock(StripePaymentConfigsProvider::class); | ||
$this->eventFactory = $this->getMockBuilder(StripeSDKEventFactory::class) | ||
->setConstructorArgs([$this->paymentConfigsProvider]) | ||
->disableOriginalClone() | ||
->disableArgumentCloning() | ||
->disallowMockingUnknownTypes() | ||
->onlyMethods(['constructEvent']) | ||
->getMock(); | ||
} | ||
|
||
public function testInvalidStripeEventException() | ||
{ | ||
$request = Request::createFromGlobals(); | ||
$config = $this->createMock(StripePaymentConfig::class); | ||
$this->paymentConfigsProvider->expects(self::once()) | ||
->method('getConfigs') | ||
->willReturn([$config]); | ||
|
||
$this->eventFactory->expects(self::once()) | ||
->method('constructEvent') | ||
->with($request, $config) | ||
->willReturn(null); | ||
|
||
self::expectException(\LogicException::class); | ||
self::expectExceptionMessage('There are no any configured Stripe payment methods available to handle event'); | ||
|
||
$this->eventFactory->createEventFromRequest($request); | ||
} | ||
|
||
public function testNoStripePaymentConfigsException() | ||
{ | ||
$request = Request::createFromGlobals(); | ||
$this->paymentConfigsProvider->expects(self::once()) | ||
->method('getConfigs') | ||
->willReturn([]); | ||
|
||
$this->eventFactory->expects(self::never()) | ||
->method('constructEvent'); | ||
|
||
self::expectException(\LogicException::class); | ||
self::expectExceptionMessage('There are no any configured Stripe payment methods available to handle event'); | ||
|
||
$this->eventFactory->createEventFromRequest($request); | ||
} | ||
|
||
public function testSupportedEvent() | ||
{ | ||
$request = Request::createFromGlobals(); | ||
$config = $this->createMock(StripePaymentConfig::class); | ||
$this->paymentConfigsProvider->expects(self::once()) | ||
->method('getConfigs') | ||
->willReturn([$config]); | ||
|
||
$stripeEventValues = [ | ||
'data' => (object) ['object' => new PaymentIntent()], | ||
'type' => 'customer.created', | ||
]; | ||
$stripeEvent = $this->createMock(Event::class); | ||
$stripeEvent->expects(self::exactly(2)) | ||
->method('__get') | ||
->willReturnCallback(function ($name) use ($stripeEventValues) { | ||
return $stripeEventValues[$name]; | ||
}); | ||
|
||
$this->eventFactory->expects(self::once()) | ||
->method('constructEvent') | ||
->with($request, $config) | ||
->willReturn($stripeEvent); | ||
|
||
$event = $this->eventFactory->createEventFromRequest($request); | ||
|
||
self::assertInstanceOf(PaymentIntentResponse::class, $event->getData()); | ||
} | ||
public function testUnsupportedEvent() | ||
{ | ||
$request = Request::createFromGlobals(); | ||
$config = $this->createMock(StripePaymentConfig::class); | ||
$this->paymentConfigsProvider->expects(self::once()) | ||
->method('getConfigs') | ||
->willReturn([$config]); | ||
|
||
$stripeEventValues = [ | ||
'data' => (object) ['object' => new \stdClass()], | ||
'type' => 'customer.created', | ||
]; | ||
$stripeEvent = $this->createMock(Event::class); | ||
$stripeEvent->expects(self::exactly(2)) | ||
->method('__get') | ||
->willReturnCallback(function ($name) use ($stripeEventValues) { | ||
return $stripeEventValues[$name]; | ||
}); | ||
|
||
$this->eventFactory->expects(self::once()) | ||
->method('constructEvent') | ||
->with($request, $config) | ||
->willReturn($stripeEvent); | ||
|
||
$event = $this->eventFactory->createEventFromRequest($request); | ||
|
||
self::assertInstanceOf(UnsupportedResponse::class, $event->getData()); | ||
} | ||
} |