Refactor mailpoet_get_subscribers_listings_in_segment_handlers to direct calls

[MAILPOET-3077]
This commit is contained in:
Rostislav Wolny
2020-09-14 16:37:03 +02:00
committed by Veljko V
parent f9f8428b42
commit 1a56b2534e
6 changed files with 29 additions and 109 deletions

View File

@ -4,7 +4,6 @@ namespace MailPoet\DynamicSegments;
use MailPoet\DynamicSegments\FreePluginConnectors\AddToSubscribersFilters;
use MailPoet\DynamicSegments\FreePluginConnectors\SubscribersBulkActionHandler;
use MailPoet\DynamicSegments\FreePluginConnectors\SubscribersListingsHandlerFactory;
use MailPoet\DynamicSegments\Mappers\DBMapper;
use MailPoet\DynamicSegments\Persistence\Loading\Loader;
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
@ -20,11 +19,6 @@ class DynamicSegmentHooks {
}
public function init() {
$this->wp->addAction(
'mailpoet_get_subscribers_listings_in_segment_handlers',
[$this, 'getSubscribersListingsInSegmentsHandlers']
);
$this->wp->addAction(
'mailpoet_subscribers_listings_filters_segments',
[$this, 'addDynamicFiltersToSubscribersListingsFilters']
@ -41,11 +35,6 @@ class DynamicSegmentHooks {
);
}
public function getSubscribersListingsInSegmentsHandlers(array $handlers) {
$handlers[] = new SubscribersListingsHandlerFactory();
return $handlers;
}
public function addDynamicFiltersToSubscribersListingsFilters($segmentFilters) {
$newslettersAddSegments = new AddToSubscribersFilters(new Loader(new DBMapper()), new SubscribersCount());
return $newslettersAddSegments->add($segmentFilters);

View File

@ -1,16 +0,0 @@
<?php
namespace MailPoet\DynamicSegments\FreePluginConnectors;
use MailPoet\Listing\Handler;
use MailPoet\Models\DynamicSegment;
use MailPoet\Models\Segment;
class SubscribersListingsHandlerFactory {
public function get(Segment $segment, $data) {
if ($segment->type === DynamicSegment::TYPE_DYNAMIC) {
$listing = new Handler();
return $listingData = $listing->get('\MailPoet\Models\SubscribersInDynamicSegment', $data);
}
}
}

View File

@ -2,20 +2,17 @@
namespace MailPoet\Segments;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Listing\Handler;
use MailPoet\Models\Segment;
use MailPoet\WP\Functions as WPFunctions;
class SubscribersListings {
/** @var Handler */
private $handler;
private $wp;
public function __construct(Handler $handler, WPFunctions $wp) {
public function __construct(Handler $handler) {
$this->handler = $handler;
$this->wp = $wp;
}
public function getListingsInSegment($data) {
@ -29,16 +26,12 @@ class SubscribersListings {
private function getListings($data, Segment $segment = null) {
if (!$segment
|| in_array($segment->type, [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS], true)
|| in_array($segment->type, [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS, SegmentEntity::TYPE_WC_USERS], true)
) {
return $listingData = $this->handler->get('\MailPoet\Models\Subscriber', $data);
}
$handlers = $this->wp->applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', []);
foreach ($handlers as $handler) {
$listings = $handler->get($segment, $data);
if ($listings) {
return $listings;
}
if ($segment->type === SegmentEntity::TYPE_DYNAMIC) {
return $this->handler->get('\MailPoet\Models\SubscribersInDynamicSegment', $data);
}
throw new \InvalidArgumentException('No handler found for segment');
}

View File

@ -1,41 +0,0 @@
<?php
namespace MailPoet\DynamicSegments\FreePluginConnectors;
use MailPoet\Models\DynamicSegment;
use MailPoetVendor\Idiorm\ORM;
class SubscribersListingsHandlerFactoryTest extends \MailPoetTest {
public function testItReturnsNullWithUnknownSegment() {
$segment = DynamicSegment::create();
$segment->id = 1;
$segment->name = 'name';
$segment->type = 'unknown';
$listings = new SubscribersListingsHandlerFactory();
$result = $listings->get($segment, ['filter' => ['segment' => null]]);
expect($result)->null();
}
public function testItReturnsDataForDynamicSegment() {
$segment = DynamicSegment::createOrUpdate([
'name' => 'name',
'description' => 'desc',
'type' => DynamicSegment::TYPE_DYNAMIC,
]);
$listings = new SubscribersListingsHandlerFactory();
$result = $listings->get($segment, ['filter' => ['segment' => null]]);
expect($result)->notNull();
}
public function _before() {
$this->cleanData();
}
public function _after() {
$this->cleanData();
}
private function cleanData() {
ORM::raw_execute('TRUNCATE ' . DynamicSegment::$_table);
}
}

View File

@ -1,9 +0,0 @@
<?php
namespace MailPoet\Test\Segments;
class DynamicListingsHandlerMock {
public function get() {
}
}

View File

@ -2,22 +2,22 @@
namespace MailPoet\Segments;
require_once('DynamicListingsHandlerMock.php');
require_once(ABSPATH . 'wp-admin/includes/user.php');
use Codeception\Util\Stub;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\DynamicSegmentFilter;
use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment;
use MailPoet\WP\Functions as WPFunctions;
use MailPoet\Test\DataFactories\DynamicSegment;
use MailPoetVendor\Idiorm\ORM;
use PHPUnit\Framework\MockObject\MockObject;
class SubscribersListingsTest extends \MailPoetTest {
public $subscriber2;
public $subscriber1;
public $segment2;
public $segment1;
public $dynamicSegment;
/** @var SubscribersListings */
private $finder;
@ -26,8 +26,19 @@ class SubscribersListingsTest extends \MailPoetTest {
parent::_before();
$this->finder = ContainerWrapper::getInstance()->get(SubscribersListings::class);
$this->cleanData();
wp_insert_user([
'user_login' => 'user-role-test1',
'user_email' => 'user-role-test1@example.com',
'role' => 'editor',
'user_pass' => '12123154',
]);
$this->segment1 = Segment::createOrUpdate(['name' => 'Segment 1', 'type' => 'default']);
$this->segment2 = Segment::createOrUpdate(['name' => 'Segment 3', 'type' => 'not default']);
$dynamicSegmentFactory = new DynamicSegment();
$this->dynamicSegment = $dynamicSegmentFactory
->withName('Dynamic')
->withUserRoleFilter('editor')
->create();
$this->subscriber1 = Subscriber::createOrUpdate([
'email' => 'john@mailpoet.com',
'first_name' => 'John',
@ -57,7 +68,12 @@ class SubscribersListingsTest extends \MailPoetTest {
private function cleanData() {
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
ORM::raw_execute('TRUNCATE ' . DynamicSegmentFilter::$_table);
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
$user = get_user_by('email', 'user-role-test1@example.com');
if ($user) {
\wp_delete_user($user->ID);
}
}
public function testTryToGetListingsWithoutPassingSegment() {
@ -75,26 +91,14 @@ class SubscribersListingsTest extends \MailPoetTest {
expect($listings['items'])->notEmpty();
}
public function testGetListingsUsingFilter() {
/** @var MockObject $mock */
$mock = Stub::makeEmpty('MailPoet\Test\Segments\DynamicListingsHandlerMock', ['get']);
$mock
->expects($this->once())
->method('get')
->will($this->returnValue('dynamic listings'));
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
(new WPFunctions)->addFilter('mailpoet_get_subscribers_listings_in_segment_handlers', function () use ($mock) {
return [$mock];
});
$listings = $this->finder->getListingsInSegment(['filter' => ['segment' => $this->segment2->id]]);
expect($listings)->equals('dynamic listings');
public function testGetListingsForDynamicSegment() {
$listings = $this->finder->getListingsInSegment(['filter' => ['segment' => $this->dynamicSegment->id]]);
expect($listings['items'])->count(1);
expect($listings['items'][0]->email)->equals('user-role-test1@example.com');
}
public function testTryToGetListingsForSegmentWithoutHandler() {
$this->expectException('InvalidArgumentException');
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
$this->finder->getListingsInSegment(['filter' => ['segment' => $this->segment2->id]]);
}
}