Use DI to create WooCommerce email
[MAILPOET-3924]
This commit is contained in:
21
lib/AutomaticEmails/AutomaticEmailFactory.php
Normal file
21
lib/AutomaticEmails/AutomaticEmailFactory.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace MailPoet\AutomaticEmails;
|
||||||
|
|
||||||
|
use MailPoet\AutomaticEmails\WooCommerce\WooCommerce;
|
||||||
|
use MailPoet\DI\ContainerWrapper;
|
||||||
|
|
||||||
|
class AutomaticEmailFactory {
|
||||||
|
/** @var ContainerWrapper */
|
||||||
|
private $container;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
ContainerWrapper $container
|
||||||
|
) {
|
||||||
|
$this->container = $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createWooCommerceEmail(): WooCommerce {
|
||||||
|
return $this->container->get(WooCommerce::class);
|
||||||
|
}
|
||||||
|
}
|
@@ -3,7 +3,6 @@
|
|||||||
namespace MailPoet\AutomaticEmails;
|
namespace MailPoet\AutomaticEmails;
|
||||||
|
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
use MailPoet\WP\Notice;
|
|
||||||
|
|
||||||
class AutomaticEmails {
|
class AutomaticEmails {
|
||||||
const FILTER_PREFIX = 'mailpoet_automatic_email_';
|
const FILTER_PREFIX = 'mailpoet_automatic_email_';
|
||||||
@@ -14,38 +13,20 @@ class AutomaticEmails {
|
|||||||
/** @var array|null */
|
/** @var array|null */
|
||||||
private $automaticEmails;
|
private $automaticEmails;
|
||||||
|
|
||||||
public $availableGroups = [
|
/** @var AutomaticEmailFactory */
|
||||||
'WooCommerce',
|
private $automaticEmailFactory;
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
WPFunctions $wp
|
WPFunctions $wp,
|
||||||
|
AutomaticEmailFactory $automaticEmailFactory
|
||||||
) {
|
) {
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
|
$this->automaticEmailFactory = $automaticEmailFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
foreach ($this->availableGroups as $group) {
|
$instance = $this->automaticEmailFactory->createWooCommerceEmail();
|
||||||
$groupClass = sprintf(
|
$instance->init();
|
||||||
'%1$s\%2$s\%2$s',
|
|
||||||
__NAMESPACE__,
|
|
||||||
$group
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!class_exists($groupClass)) {
|
|
||||||
$this->displayGroupWarning($group);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$groupInstance = new $groupClass();
|
|
||||||
|
|
||||||
if (method_exists($groupInstance, 'init')) {
|
|
||||||
$groupInstance->init();
|
|
||||||
} else {
|
|
||||||
$this->displayGroupWarning($group);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAutomaticEmails() {
|
public function getAutomaticEmails() {
|
||||||
@@ -151,12 +132,4 @@ class AutomaticEmails {
|
|||||||
|
|
||||||
$this->automaticEmails = null;
|
$this->automaticEmails = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function displayGroupWarning($group) {
|
|
||||||
$notice = sprintf('%s %s',
|
|
||||||
sprintf(__('%s automatic email group is misconfigured.', 'mailpoet'), $group),
|
|
||||||
WPFunctions::get()->__('Please contact our technical support for assistance.', 'mailpoet')
|
|
||||||
);
|
|
||||||
Notice::displayWarning($notice);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -92,6 +92,8 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder::class)->setPublic(true);
|
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder::class)->setPublic(true);
|
||||||
// Automatic emails
|
// Automatic emails
|
||||||
$container->autowire(\MailPoet\AutomaticEmails\AutomaticEmails::class)->setPublic(true);
|
$container->autowire(\MailPoet\AutomaticEmails\AutomaticEmails::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\AutomaticEmails\AutomaticEmailFactory::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\AutomaticEmails\WooCommerce\WooCommerce::class)->setPublic(true);
|
||||||
// Config
|
// Config
|
||||||
$container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);
|
$container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Config\Activator::class)->setPublic(true);
|
$container->autowire(\MailPoet\Config\Activator::class)->setPublic(true);
|
||||||
|
@@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
namespace MailPoet\API\JSON\v1;
|
namespace MailPoet\API\JSON\v1;
|
||||||
|
|
||||||
|
use MailPoet\AutomaticEmails\AutomaticEmailFactory;
|
||||||
use MailPoet\AutomaticEmails\AutomaticEmails as AutomaticEmailsController;
|
use MailPoet\AutomaticEmails\AutomaticEmails as AutomaticEmailsController;
|
||||||
|
use MailPoet\AutomaticEmails\WooCommerce\WooCommerce;
|
||||||
|
use MailPoet\WooCommerce\Helper;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class AutomaticEmailsTest extends \MailPoetTest {
|
class AutomaticEmailsTest extends \MailPoetTest {
|
||||||
@@ -13,7 +16,11 @@ class AutomaticEmailsTest extends \MailPoetTest {
|
|||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
$this->wp = new WPFunctions;
|
$this->wp = new WPFunctions;
|
||||||
$this->api = new AutomaticEmails(new AutomaticEmailsController($this->wp), $this->wp);
|
|
||||||
|
$automaticEmailFactory = $this->makeEmpty(AutomaticEmailFactory::class, [
|
||||||
|
'createWooCommerceEmail' => new WooCommerce($this->wp, new Helper()),
|
||||||
|
]);
|
||||||
|
$this->api = new AutomaticEmails(new AutomaticEmailsController($this->wp, $automaticEmailFactory), $this->wp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItRequiresProperlyFormattedRequestWhenGettingEventOptions() {
|
public function testItRequiresProperlyFormattedRequestWhenGettingEventOptions() {
|
||||||
|
@@ -2,15 +2,22 @@
|
|||||||
|
|
||||||
namespace MailPoet\AutomaticEmails;
|
namespace MailPoet\AutomaticEmails;
|
||||||
|
|
||||||
|
use MailPoet\AutomaticEmails\WooCommerce\WooCommerce;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class AutomaticEmailsTest extends \MailPoetTest {
|
class AutomaticEmailsTest extends \MailPoetTest {
|
||||||
public $wp;
|
public $wp;
|
||||||
public $AM;
|
public $AM;
|
||||||
|
|
||||||
|
/** @var AutomaticEmailFactory */
|
||||||
|
private $automaticEmailFactory;
|
||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
$this->wp = new WPFunctions();
|
$this->wp = new WPFunctions();
|
||||||
$this->AM = new AutomaticEmails($this->wp);
|
$this->automaticEmailFactory = $this->makeEmpty(AutomaticEmailFactory::class, [
|
||||||
|
'createWooCommerceEmail' => new WooCommerce(),
|
||||||
|
]);
|
||||||
|
$this->AM = new AutomaticEmails($this->wp, $this->automaticEmailFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanUnregisterAutomaticEmails() {
|
public function testItCanUnregisterAutomaticEmails() {
|
||||||
@@ -37,14 +44,6 @@ class AutomaticEmailsTest extends \MailPoetTest {
|
|||||||
expect($result)->null();
|
expect($result)->null();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItReturnsNullWhenThereAreNoRegisteredAutomaticEmails() {
|
|
||||||
$AM = $this->AM;
|
|
||||||
$AM->unregisterAutomaticEmails();
|
|
||||||
$AM->availableGroups = [];
|
|
||||||
$AM->init();
|
|
||||||
expect($AM->getAutomaticEmails())->null();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsAutomaticEmails() {
|
public function testItGetsAutomaticEmails() {
|
||||||
$this->wp->addFilter('mailpoet_automatic_email_test1', function() {
|
$this->wp->addFilter('mailpoet_automatic_email_test1', function() {
|
||||||
return [
|
return [
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
namespace MailPoet\AutomaticEmails\WooCommerce;
|
namespace MailPoet\AutomaticEmails\WooCommerce;
|
||||||
|
|
||||||
use Codeception\Util\Stub;
|
use Codeception\Util\Stub;
|
||||||
|
use MailPoet\AutomaticEmails\AutomaticEmailFactory;
|
||||||
use MailPoet\AutomaticEmails\AutomaticEmails;
|
use MailPoet\AutomaticEmails\AutomaticEmails;
|
||||||
use MailPoet\AutomaticEmails\WooCommerce\Events\AbandonedCart;
|
use MailPoet\AutomaticEmails\WooCommerce\Events\AbandonedCart;
|
||||||
use MailPoet\AutomaticEmails\WooCommerce\Events\FirstPurchase;
|
use MailPoet\AutomaticEmails\WooCommerce\Events\FirstPurchase;
|
||||||
@@ -11,13 +12,22 @@ use MailPoet\AutomaticEmails\WooCommerce\Events\PurchasedProduct;
|
|||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class WooCommerceTest extends \MailPoetTest {
|
class WooCommerceTest extends \MailPoetTest {
|
||||||
|
/** @var AutomaticEmailFactory */
|
||||||
|
private $automaticEmailFactory;
|
||||||
|
|
||||||
|
public function _before() {
|
||||||
|
$this->automaticEmailFactory = $this->makeEmpty(AutomaticEmailFactory::class, [
|
||||||
|
'createWooCommerceEmail' => new WooCommerce(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function testItRegistersAbandonedCartEvent() {
|
public function testItRegistersAbandonedCartEvent() {
|
||||||
$WC = Stub::make(new WooCommerce(), ['isWoocommerceEnabled' => true]);
|
$WC = Stub::make(new WooCommerce(), ['isWoocommerceEnabled' => true]);
|
||||||
$WC->__construct();
|
$WC->__construct();
|
||||||
$WC->init();
|
$WC->init();
|
||||||
|
|
||||||
// event is registered
|
// event is registered
|
||||||
$AM = new AutomaticEmails(new WPFunctions());
|
$AM = new AutomaticEmails(new WPFunctions(), $this->automaticEmailFactory);
|
||||||
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, AbandonedCart::SLUG);
|
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, AbandonedCart::SLUG);
|
||||||
expect($result)->notEmpty();
|
expect($result)->notEmpty();
|
||||||
}
|
}
|
||||||
@@ -28,7 +38,7 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
$WC->init();
|
$WC->init();
|
||||||
|
|
||||||
// event is registered
|
// event is registered
|
||||||
$AM = new AutomaticEmails(new WPFunctions());
|
$AM = new AutomaticEmails(new WPFunctions(), $this->automaticEmailFactory);
|
||||||
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, FirstPurchase::SLUG);
|
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, FirstPurchase::SLUG);
|
||||||
expect($result)->notEmpty();
|
expect($result)->notEmpty();
|
||||||
|
|
||||||
@@ -44,7 +54,7 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
$WC->init();
|
$WC->init();
|
||||||
|
|
||||||
// event is registered
|
// event is registered
|
||||||
$AM = new AutomaticEmails(new WPFunctions());
|
$AM = new AutomaticEmails(new WPFunctions(), $this->automaticEmailFactory);
|
||||||
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, PurchasedInCategory::SLUG);
|
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, PurchasedInCategory::SLUG);
|
||||||
expect($result)->notEmpty();
|
expect($result)->notEmpty();
|
||||||
}
|
}
|
||||||
@@ -55,7 +65,7 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
$WC->init();
|
$WC->init();
|
||||||
|
|
||||||
// event is registered
|
// event is registered
|
||||||
$AM = new AutomaticEmails(new WPFunctions());
|
$AM = new AutomaticEmails(new WPFunctions(), $this->automaticEmailFactory);
|
||||||
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, PurchasedProduct::SLUG);
|
$result = $AM->getAutomaticEmailEventBySlug(WooCommerce::SLUG, PurchasedProduct::SLUG);
|
||||||
expect($result)->notEmpty();
|
expect($result)->notEmpty();
|
||||||
|
|
||||||
@@ -69,7 +79,7 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
$WC = Stub::make(new WooCommerce(), ['isWoocommerceEnabled' => false]);
|
$WC = Stub::make(new WooCommerce(), ['isWoocommerceEnabled' => false]);
|
||||||
$WC->__construct();
|
$WC->__construct();
|
||||||
$WC->init();
|
$WC->init();
|
||||||
$AM = new AutomaticEmails(new WPFunctions());
|
$AM = new AutomaticEmails(new WPFunctions(), $this->automaticEmailFactory);
|
||||||
$result = $AM->getAutomaticEmailBySlug('woocommerce');
|
$result = $AM->getAutomaticEmailBySlug('woocommerce');
|
||||||
foreach ($result['events'] as $event) {
|
foreach ($result['events'] as $event) {
|
||||||
expect($event['actionButtonTitle'])->equals('WooCommerce is required');
|
expect($event['actionButtonTitle'])->equals('WooCommerce is required');
|
||||||
@@ -79,7 +89,7 @@ class WooCommerceTest extends \MailPoetTest {
|
|||||||
$WC = Stub::make(new WooCommerce(), ['isWoocommerceEnabled' => true]);
|
$WC = Stub::make(new WooCommerce(), ['isWoocommerceEnabled' => true]);
|
||||||
$WC->__construct();
|
$WC->__construct();
|
||||||
$WC->init();
|
$WC->init();
|
||||||
$AM = new AutomaticEmails(new WPFunctions());
|
$AM = new AutomaticEmails(new WPFunctions(), $this->automaticEmailFactory);
|
||||||
$result = $AM->getAutomaticEmailBySlug('woocommerce');
|
$result = $AM->getAutomaticEmailBySlug('woocommerce');
|
||||||
foreach ($result['events'] as $event) {
|
foreach ($result['events'] as $event) {
|
||||||
expect($event)->hasNotKey('actionButtonTitle');
|
expect($event)->hasNotKey('actionButtonTitle');
|
||||||
|
Reference in New Issue
Block a user