|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Oro\Bundle\StripeBundle\Tests\Unit\Event; |
| 4 | + |
| 5 | +use Oro\Bundle\StripeBundle\Event\StripeSDKEventFactory; |
| 6 | +use Oro\Bundle\StripeBundle\Method\Config\Provider\StripePaymentConfigsProvider; |
| 7 | +use Oro\Bundle\StripeBundle\Method\Config\StripePaymentConfig; |
| 8 | +use Oro\Bundle\StripeBundle\Model\PaymentIntentResponse; |
| 9 | +use Oro\Bundle\StripeBundle\Model\UnsupportedResponse; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Stripe\Event; |
| 13 | +use Stripe\PaymentIntent; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | + |
| 16 | +class StripeSDKEventFactoryTest extends TestCase |
| 17 | +{ |
| 18 | + private MockObject|StripePaymentConfigsProvider $paymentConfigsProvider; |
| 19 | + private MockObject|StripeSDKEventFactory $eventFactory; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->paymentConfigsProvider = $this->createMock(StripePaymentConfigsProvider::class); |
| 24 | + $this->eventFactory = $this->getMockBuilder(StripeSDKEventFactory::class) |
| 25 | + ->setConstructorArgs([$this->paymentConfigsProvider]) |
| 26 | + ->disableOriginalClone() |
| 27 | + ->disableArgumentCloning() |
| 28 | + ->disallowMockingUnknownTypes() |
| 29 | + ->onlyMethods(['constructEvent']) |
| 30 | + ->getMock(); |
| 31 | + } |
| 32 | + |
| 33 | + public function testInvalidStripeEventException() |
| 34 | + { |
| 35 | + $request = Request::createFromGlobals(); |
| 36 | + $config = $this->createMock(StripePaymentConfig::class); |
| 37 | + $this->paymentConfigsProvider->expects(self::once()) |
| 38 | + ->method('getConfigs') |
| 39 | + ->willReturn([$config]); |
| 40 | + |
| 41 | + $this->eventFactory->expects(self::once()) |
| 42 | + ->method('constructEvent') |
| 43 | + ->with($request, $config) |
| 44 | + ->willReturn(null); |
| 45 | + |
| 46 | + self::expectException(\LogicException::class); |
| 47 | + self::expectExceptionMessage('There are no any configured Stripe payment methods available to handle event'); |
| 48 | + |
| 49 | + $this->eventFactory->createEventFromRequest($request); |
| 50 | + } |
| 51 | + |
| 52 | + public function testNoStripePaymentConfigsException() |
| 53 | + { |
| 54 | + $request = Request::createFromGlobals(); |
| 55 | + $this->paymentConfigsProvider->expects(self::once()) |
| 56 | + ->method('getConfigs') |
| 57 | + ->willReturn([]); |
| 58 | + |
| 59 | + $this->eventFactory->expects(self::never()) |
| 60 | + ->method('constructEvent'); |
| 61 | + |
| 62 | + self::expectException(\LogicException::class); |
| 63 | + self::expectExceptionMessage('There are no any configured Stripe payment methods available to handle event'); |
| 64 | + |
| 65 | + $this->eventFactory->createEventFromRequest($request); |
| 66 | + } |
| 67 | + |
| 68 | + public function testSupportedEvent() |
| 69 | + { |
| 70 | + $request = Request::createFromGlobals(); |
| 71 | + $config = $this->createMock(StripePaymentConfig::class); |
| 72 | + $this->paymentConfigsProvider->expects(self::once()) |
| 73 | + ->method('getConfigs') |
| 74 | + ->willReturn([$config]); |
| 75 | + |
| 76 | + $stripeEventValues = [ |
| 77 | + 'data' => (object) ['object' => new PaymentIntent()], |
| 78 | + 'type' => 'customer.created', |
| 79 | + ]; |
| 80 | + $stripeEvent = $this->createMock(Event::class); |
| 81 | + $stripeEvent->expects(self::exactly(2)) |
| 82 | + ->method('__get') |
| 83 | + ->willReturnCallback(function ($name) use ($stripeEventValues) { |
| 84 | + return $stripeEventValues[$name]; |
| 85 | + }); |
| 86 | + |
| 87 | + $this->eventFactory->expects(self::once()) |
| 88 | + ->method('constructEvent') |
| 89 | + ->with($request, $config) |
| 90 | + ->willReturn($stripeEvent); |
| 91 | + |
| 92 | + $event = $this->eventFactory->createEventFromRequest($request); |
| 93 | + |
| 94 | + self::assertInstanceOf(PaymentIntentResponse::class, $event->getData()); |
| 95 | + } |
| 96 | + public function testUnsupportedEvent() |
| 97 | + { |
| 98 | + $request = Request::createFromGlobals(); |
| 99 | + $config = $this->createMock(StripePaymentConfig::class); |
| 100 | + $this->paymentConfigsProvider->expects(self::once()) |
| 101 | + ->method('getConfigs') |
| 102 | + ->willReturn([$config]); |
| 103 | + |
| 104 | + $stripeEventValues = [ |
| 105 | + 'data' => (object) ['object' => new \stdClass()], |
| 106 | + 'type' => 'customer.created', |
| 107 | + ]; |
| 108 | + $stripeEvent = $this->createMock(Event::class); |
| 109 | + $stripeEvent->expects(self::exactly(2)) |
| 110 | + ->method('__get') |
| 111 | + ->willReturnCallback(function ($name) use ($stripeEventValues) { |
| 112 | + return $stripeEventValues[$name]; |
| 113 | + }); |
| 114 | + |
| 115 | + $this->eventFactory->expects(self::once()) |
| 116 | + ->method('constructEvent') |
| 117 | + ->with($request, $config) |
| 118 | + ->willReturn($stripeEvent); |
| 119 | + |
| 120 | + $event = $this->eventFactory->createEventFromRequest($request); |
| 121 | + |
| 122 | + self::assertInstanceOf(UnsupportedResponse::class, $event->getData()); |
| 123 | + } |
| 124 | +} |
0 commit comments