Add woo filter helper tests for order status

MAILPOET-5083
This commit is contained in:
John Oleksowicz
2023-04-07 16:54:24 -05:00
committed by Aschepikov
parent 096ab74ee3
commit 4576dd289f
2 changed files with 87 additions and 4 deletions

View File

@@ -214,19 +214,24 @@ class IntegrationTester extends \Codeception\Actor {
$filterEntity = new DynamicSegmentFilterEntity($segment, $data);
$this->entityManager->persist($filterEntity);
$segment->addDynamicFilter($filterEntity);
$queryBuilder = $filter->apply($this->getSubscribersQueryBuilder(), $filterEntity);
return $this->getSubscriberEmailsFromQueryBuilder($queryBuilder);
}
/**
* @param QueryBuilder $queryBuilder
* @return string[] - array of subscriber emails
*/
public function getSubscriberEmailsFromQueryBuilder(QueryBuilder $queryBuilder): array {
$statement = $queryBuilder->execute();
$results = $statement instanceof Statement ? $statement->fetchAllAssociative() : [];
$emails = array_map(function($row) {
return array_map(function($row) {
$subscriber = $this->entityManager->find(SubscriberEntity::class, $row['inner_subscriber_id']);
if (!$subscriber instanceof SubscriberEntity) {
throw new \Exception('this is for PhpStan');
}
return $subscriber->getEmail();
}, $results);
return $emails;
}
public function getSubscribersQueryBuilder(): QueryBuilder {

View File

@@ -0,0 +1,78 @@
<?php declare(strict_types = 1);
namespace integration\Segments\DynamicSegments\Filters;
use MailPoet\Segments\DynamicSegments\Filters\WooFilterHelper;
class WooFilterHelperTest extends \MailPoetTest {
/** @var WooFilterHelper */
private $wooFilterHelper;
public function _before() {
parent::_before();
$this->wooFilterHelper = $this->diContainer->get(WooFilterHelper::class);
}
/**
* @dataProvider allowedStatuses
*/
public function testItCanJoinCustomersBasedOnPurchaseStatus($status) {
$customerId = $this->tester->createCustomer('customer@example.com');
$this->createOrder($customerId, $status);
$queryBuilder = $this->tester->getSubscribersQueryBuilder();
$this->wooFilterHelper->applyOrderStatusFilter($queryBuilder);
$emails = $this->tester->getSubscriberEmailsFromQueryBuilder($queryBuilder);
expect($emails)->contains('customer@example.com');
}
/**
* @dataProvider disallowedStatuses
*/
public function testItExcludesDisallowedOrderStatuses($status) {
$customerId = $this->tester->createCustomer('customer@example.com');
$this->createOrder($customerId, $status);
$queryBuilder = $this->tester->getSubscribersQueryBuilder();
$this->wooFilterHelper->applyOrderStatusFilter($queryBuilder);
$emails = $this->tester->getSubscriberEmailsFromQueryBuilder($queryBuilder);
expect($emails)->notContains('customer@example.com');
}
public function testOrderStatusesCanBeOverridden() {
$customerId = $this->tester->createCustomer('refunded@example.com');
$this->createOrder($customerId, 'wc-refunded');
$customerId2 = $this->tester->createCustomer('completed@example.com');
$this->createOrder($customerId2, 'wc-completed');
$queryBuilder = $this->tester->getSubscribersQueryBuilder();
$this->wooFilterHelper->applyOrderStatusFilter($queryBuilder, ['wc-refunded']);
$emails = $this->tester->getSubscriberEmailsFromQueryBuilder($queryBuilder);
expect($emails)->contains('refunded@example.com');
expect($emails)->notContains('completed@example.com');
}
public function allowedStatuses() {
return [
'completed' => ['wc-completed'],
'processing' => ['wc-processing'],
];
}
public function disallowedStatuses() {
return [
'refunded' => ['wc-refunded'],
'cancelled' => ['wc-cancelled'],
'on hold' => ['wc-on-hold'],
'pending' => ['wc-pending'],
'failed' => ['wc-failed'],
];
}
private function createOrder(int $customerId, string $status): int {
$order = $this->tester->createWooCommerceOrder();
$order->set_customer_id($customerId);
$order->set_status($status);
$order->save();
$this->tester->updateWooOrderStats($order->get_id());
return $order->get_id();
}
}