Skip to content

Commit 932fdf9

Browse files
STRIPE-48: Unit and Functional tests implementation (#33932)
- Implemented unit and functional tests
1 parent bfb0040 commit 932fdf9

7 files changed

Lines changed: 631 additions & 4 deletions

File tree

src/Oro/Bundle/StripeBundle/Notification/StripeNotificationManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828

2929
public function sendNotification(string $recipientEmail, string $subject, string $message)
3030
{
31-
$message = [
31+
$notificationMessage = [
3232
'from' => $this->notificationSettings->getSender()->toString(),
3333
'toEmail' => $recipientEmail,
3434
'subject' => $subject,
@@ -37,12 +37,12 @@ public function sendNotification(string $recipientEmail, string $subject, string
3737
];
3838

3939
try {
40-
$this->messageProducer->send(SendEmailNotificationTopic::getName(), $message);
40+
$this->messageProducer->send(SendEmailNotificationTopic::getName(), $notificationMessage);
4141
} catch (\Throwable $exception) {
4242
$this->logger->critical('Failed to send stripe notification email', [
4343
'message' => $exception->getMessage(),
4444
'exception' => $exception,
45-
'emailParameters' => $message
45+
'notificationMessage' => $notificationMessage
4646
]);
4747
}
4848
}

src/Oro/Bundle/StripeBundle/Provider/EntitiesTransactionsProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function hasExpiringAuthorizationTransactions(): bool
6969
{
7070
$qb = $this->getExpiringAuthorizeTransactionsQB();
7171
$qb->resetDQLPart('select')
72-
->select('COUNT(*)');
72+
->select('COUNT(pt.id)');
7373

7474
return $qb->getQuery()->getSingleScalarResult() > 0;
7575
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Oro\Bundle\StripeBundle\Tests\Functional\DataFixtures;
4+
5+
use Doctrine\Persistence\ObjectManager;
6+
use Oro\Bundle\OrderBundle\Tests\Functional\DataFixtures\LoadOrders as BaseLoadOrders;
7+
use Oro\Bundle\OrderBundle\Tests\Functional\DataFixtures\LoadOrderUsers;
8+
use Oro\Bundle\OrderBundle\Tests\Functional\DataFixtures\LoadPaymentTermData;
9+
10+
class LoadOrders extends BaseLoadOrders
11+
{
12+
const MAIN_ORDER = 'main_order';
13+
const SUB_ORDER_1 = 'sub_order1';
14+
const SUB_ORDER_2 = 'sub_order2';
15+
const SUB_ORDER_3 = 'sub_order3';
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $orders = [
21+
self::MAIN_ORDER => [
22+
'user' => LoadOrderUsers::ORDER_USER_1,
23+
'customerUser' => self::ACCOUNT_USER,
24+
'poNumber' => '1234567890',
25+
'customerNotes' => 'Test customer user notes',
26+
'currency' => 'USD',
27+
'subtotal' => 100.00,
28+
'total' => 120.00,
29+
'paymentTerm' => LoadPaymentTermData::PAYMENT_TERM_NET_10,
30+
],
31+
self::SUB_ORDER_1 => [
32+
'user' => LoadOrderUsers::ORDER_USER_1,
33+
'customerUser' => self::ACCOUNT_USER,
34+
'poNumber' => '1234567890',
35+
'customerNotes' => 'Test customer user notes',
36+
'currency' => 'USD',
37+
'subtotal' => 40.00,
38+
'total' => 45.00,
39+
'paymentTerm' => LoadPaymentTermData::PAYMENT_TERM_NET_10,
40+
],
41+
self::SUB_ORDER_2 => [
42+
'user' => LoadOrderUsers::ORDER_USER_1,
43+
'customerUser' => self::ACCOUNT_USER,
44+
'poNumber' => '1234567890',
45+
'customerNotes' => 'Test customer user notes',
46+
'currency' => 'USD',
47+
'subtotal' => 50.00,
48+
'total' => 60.00,
49+
'paymentTerm' => LoadPaymentTermData::PAYMENT_TERM_NET_10,
50+
],
51+
self::SUB_ORDER_3 => [
52+
'user' => LoadOrderUsers::ORDER_USER_1,
53+
'customerUser' => self::ACCOUNT_USER,
54+
'poNumber' => '1234567890',
55+
'customerNotes' => 'Test customer user notes',
56+
'currency' => 'USD',
57+
'subtotal' => 10.00,
58+
'total' => 15.00,
59+
'paymentTerm' => LoadPaymentTermData::PAYMENT_TERM_NET_10,
60+
]
61+
];
62+
63+
protected function createOrder(ObjectManager $manager, $name, array $orderData)
64+
{
65+
$order = parent::createOrder($manager, $name, $orderData);
66+
67+
if ($name !== self::MAIN_ORDER) {
68+
$mainOrder = $this->getReference(self::MAIN_ORDER);
69+
$order->setParent($mainOrder);
70+
}
71+
}
72+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Oro\Bundle\StripeBundle\Tests\Functional\DataFixtures;
4+
5+
use Doctrine\Common\DataFixtures\AbstractFixture;
6+
use Doctrine\Persistence\ObjectManager;
7+
use Oro\Bundle\OrderBundle\Entity\Order;
8+
use Oro\Bundle\PaymentBundle\Entity\PaymentTransaction;
9+
use Oro\Bundle\PaymentBundle\Method\PaymentMethodInterface;
10+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
11+
use Symfony\Component\DependencyInjection\ContainerInterface;
12+
13+
class LoadPaymentTransactions extends AbstractFixture implements ContainerAwareInterface
14+
{
15+
const STRIPE_PAYMENT_METHOD = 'stripe_1';
16+
const TEST_PAYMENT_METHOD = 'test_payment_2';
17+
18+
const EXPIRED_AUTHORIZATION_TRANSACTION_1 = 'expired_authorization_transaction_1';
19+
const EXPIRED_AUTHORIZATION_TRANSACTION_2 = 'expired_authorization_transaction_2';
20+
const EXPIRED_AUTHORIZATION_FAILED_TRANSACTION = 'expired_authorization_failed_transaction';
21+
const EXPIRED_AUTHORIZATION_NOT_ACTIVE_TRANSACTION = 'expired_authorization_not_active_transaction';
22+
const ACTUAL_AUTHORIZATION_TRANSACTION = 'actual_authorization_transaction';
23+
const CAPTURE_TRANSACTION = 'capture_transaction';
24+
25+
private ?ContainerInterface $container;
26+
private static array $paymentTransactionsData = [
27+
self::EXPIRED_AUTHORIZATION_TRANSACTION_1 => [
28+
'amount' => '10.00',
29+
'currency' => 'USD',
30+
'action' => PaymentMethodInterface::AUTHORIZE,
31+
'entityIdentifier' => 1,
32+
'paymentMethod' => self::TEST_PAYMENT_METHOD,
33+
'entityClass' => Order::class,
34+
'active' => true,
35+
'successful' => true
36+
],
37+
self::EXPIRED_AUTHORIZATION_TRANSACTION_2 => [
38+
'amount' => '50.00',
39+
'currency' => 'USD',
40+
'action' => PaymentMethodInterface::AUTHORIZE,
41+
'entityIdentifier' => 2,
42+
'paymentMethod' => self::STRIPE_PAYMENT_METHOD,
43+
'entityClass' => Order::class,
44+
'active' => true,
45+
'successful' => true
46+
],
47+
self::EXPIRED_AUTHORIZATION_FAILED_TRANSACTION => [
48+
'amount' => '20.00',
49+
'currency' => 'USD',
50+
'action' => PaymentMethodInterface::AUTHORIZE,
51+
'entityIdentifier' => 3,
52+
'paymentMethod' => self::STRIPE_PAYMENT_METHOD,
53+
'entityClass' => Order::class,
54+
'active' => true,
55+
'successful' => false
56+
],
57+
self::EXPIRED_AUTHORIZATION_NOT_ACTIVE_TRANSACTION => [
58+
'amount' => '30.00',
59+
'currency' => 'USD',
60+
'action' => PaymentMethodInterface::AUTHORIZE,
61+
'entityIdentifier' => 4,
62+
'paymentMethod' => self::STRIPE_PAYMENT_METHOD,
63+
'entityClass' => Order::class,
64+
'active' => false,
65+
'successful' => true
66+
],
67+
self::ACTUAL_AUTHORIZATION_TRANSACTION => [
68+
'amount' => '50.00',
69+
'currency' => 'USD',
70+
'action' => PaymentMethodInterface::AUTHORIZE,
71+
'entityIdentifier' => 5,
72+
'paymentMethod' => self::STRIPE_PAYMENT_METHOD,
73+
'entityClass' => Order::class,
74+
'active' => true,
75+
'successful' => true
76+
],
77+
self::CAPTURE_TRANSACTION => [
78+
'amount' => '70.00',
79+
'currency' => 'USD',
80+
'action' => PaymentMethodInterface::CAPTURE,
81+
'entityIdentifier' => 7,
82+
'paymentMethod' => self::STRIPE_PAYMENT_METHOD,
83+
'entityClass' => Order::class,
84+
'active' => true,
85+
'successful' => true
86+
],
87+
];
88+
89+
public function load(ObjectManager $manager): void
90+
{
91+
$transactionExpireHours = $this->container->getParameter('oro_stripe.authorization_transaction_expiration_hours');
92+
$expireDate = (new \DateTime('now', new \DateTimeZone('UTC')))
93+
->modify(sprintf('-%d hour', $transactionExpireHours + 1));
94+
95+
foreach (self::$paymentTransactionsData as $identifier => $data) {
96+
$createdAt = clone $expireDate;
97+
98+
if (in_array($identifier, [self::CAPTURE_TRANSACTION, self::ACTUAL_AUTHORIZATION_TRANSACTION])) {
99+
$createdAt = new \DateTime('now', new \DateTimeZone('UTC'));
100+
}
101+
102+
$paymentTransaction = new PaymentTransaction();
103+
$paymentTransaction->setAmount($data['amount'])
104+
->setCurrency($data['currency'])
105+
->setAction($data['action'])
106+
->setEntityIdentifier($data['entityIdentifier'])
107+
->setPaymentMethod($data['paymentMethod'])
108+
->setEntityClass($data['entityClass'])
109+
->setActive($data['active'])
110+
->setSuccessful($data['successful'])
111+
->setCreatedAt($createdAt);
112+
113+
$manager->persist($paymentTransaction);
114+
$this->setReference($identifier, $paymentTransaction);
115+
}
116+
117+
$manager->flush();
118+
}
119+
120+
public function setContainer(ContainerInterface $container = null): void
121+
{
122+
$this->container = $container;
123+
}
124+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
3+
namespace Oro\Bundle\StripeBundle\Tests\Functional\Provider;
4+
5+
use Oro\Bundle\OrderBundle\Entity\Order;
6+
use Oro\Bundle\OrderBundle\Tests\Functional\DataFixtures\LoadOrderUsers;
7+
use Oro\Bundle\PaymentBundle\Entity\PaymentTransaction;
8+
use Oro\Bundle\StripeBundle\Provider\EntitiesTransactionsProvider;
9+
use Oro\Bundle\StripeBundle\Tests\Functional\DataFixtures\LoadOrders;
10+
use Oro\Bundle\StripeBundle\Tests\Functional\DataFixtures\LoadPaymentTransactions;
11+
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
12+
13+
class EntitiesTransactionsProviderTest extends WebTestCase
14+
{
15+
private EntitiesTransactionsProvider $provider;
16+
17+
protected function setUp(): void
18+
{
19+
$this->initClient([], $this->generateBasicAuthHeader());
20+
$this->loadFixtures([
21+
LoadOrders::class,
22+
LoadPaymentTransactions::class
23+
]);
24+
25+
$this->provider = $this->getContainer()->get('oro_stripe.provider.entities_transactions');
26+
}
27+
28+
public function testGetTransactionsForMultipleEntities()
29+
{
30+
$order = $this->getReference(LoadOrders::MAIN_ORDER);
31+
$paymentTransaction = new PaymentTransaction();
32+
$paymentTransaction->setEntityClass(Order::class)
33+
->setEntityIdentifier($order->getId())
34+
->setPaymentMethod('stripe_1');
35+
36+
$transactions = $this->provider->getTransactionsForMultipleEntities($paymentTransaction);
37+
38+
$this->assertNotEmpty($transactions);
39+
$this->assertCount(3, $transactions);
40+
}
41+
42+
public function testGetTransactionsForMultipleEntitiesWithoutSubOrders()
43+
{
44+
$order = $this->getReference(LoadOrders::SUB_ORDER_1);
45+
$paymentTransaction = new PaymentTransaction();
46+
$paymentTransaction->setEntityClass(Order::class)
47+
->setEntityIdentifier($order->getId())
48+
->setPaymentMethod('stripe_1');
49+
50+
$transactions = $this->provider->getTransactionsForMultipleEntities($paymentTransaction);
51+
52+
$this->assertNotEmpty($transactions);
53+
$this->assertCount(1, $transactions);
54+
}
55+
56+
/**
57+
* @param object|object $entity
58+
* @param $expected
59+
* @dataProvider getTestHasEntitiesData
60+
*/
61+
public function testHasEntities($entity, $expected)
62+
{
63+
if (is_string($entity)) {
64+
$entity = $this->getReference($entity);
65+
}
66+
67+
$paymentTransaction = new PaymentTransaction();
68+
$paymentTransaction->setEntityClass(get_class($entity))
69+
->setEntityIdentifier($entity->getId())
70+
->setPaymentMethod('stripe_1');
71+
72+
$this->assertEquals($expected, $this->provider->hasEntities($paymentTransaction));
73+
}
74+
75+
public function getTestHasEntitiesData(): array
76+
{
77+
return [
78+
'Order with suborders should return true' => [
79+
'entity' => LoadOrders::MAIN_ORDER,
80+
'expected' => true
81+
],
82+
'Order without suborders should return false' => [
83+
'entity' => LoadOrders::SUB_ORDER_1,
84+
'expected' => false
85+
],
86+
'Objects of other than Order types should return false' => [
87+
'entity' => LoadOrderUsers::ORDER_USER_1,
88+
'expected' => false
89+
],
90+
];
91+
}
92+
93+
public function testGetExpiringAuthorizationTransactionsWithSinglePaymentMethod()
94+
{
95+
$paymentMethods = [LoadPaymentTransactions::STRIPE_PAYMENT_METHOD];
96+
$expiredTransactions = $this->provider->getExpiringAuthorizationTransactions($paymentMethods);
97+
98+
$expiredTransactionIdentifiers = $this->collectTransactionIdentifiers($expiredTransactions);
99+
100+
$this->assertCount(1, $expiredTransactionIdentifiers);
101+
102+
$expectedExpiredTransactionId = $this->getReference(
103+
LoadPaymentTransactions::EXPIRED_AUTHORIZATION_TRANSACTION_2
104+
)->getId();
105+
106+
$this->assertContains($expectedExpiredTransactionId, $expiredTransactionIdentifiers);
107+
}
108+
109+
public function testGetExpiringAuthorizationTransactionsWithMultiplePaymentMethods()
110+
{
111+
$paymentMethods = [
112+
LoadPaymentTransactions::STRIPE_PAYMENT_METHOD,
113+
LoadPaymentTransactions::TEST_PAYMENT_METHOD,
114+
'payment_method'
115+
];
116+
117+
$expiredTransactions = $this->provider->getExpiringAuthorizationTransactions($paymentMethods);
118+
119+
$expiredTransactionIdentifiers = $this->collectTransactionIdentifiers($expiredTransactions);
120+
121+
$this->assertCount(2, $expiredTransactionIdentifiers);
122+
123+
$expectedExpiredTransactionId1 = $this->getReference(
124+
LoadPaymentTransactions::EXPIRED_AUTHORIZATION_TRANSACTION_2
125+
)->getId();
126+
$expectedExpiredTransactionId2 = $this->getReference(
127+
LoadPaymentTransactions::EXPIRED_AUTHORIZATION_TRANSACTION_1
128+
)->getId();
129+
130+
$this->assertContains($expectedExpiredTransactionId1, $expiredTransactionIdentifiers);
131+
$this->assertContains($expectedExpiredTransactionId2, $expiredTransactionIdentifiers);
132+
}
133+
134+
public function testHasExpiringAuthorizationTransactions()
135+
{
136+
$this->assertTrue($this->provider->hasExpiringAuthorizationTransactions());
137+
}
138+
139+
private function collectTransactionIdentifiers(\Iterator $iterator): array
140+
{
141+
$identifiers = [];
142+
143+
/** @var PaymentTransaction $value */
144+
foreach ($iterator as $value) {
145+
array_push($identifiers, $value->getId());
146+
}
147+
148+
return $identifiers;
149+
}
150+
}

0 commit comments

Comments
 (0)