Refactor loading subscriber counts in dynamic segments API

[MAILPOET-3077]
This commit is contained in:
Rostislav Wolny
2020-09-21 12:57:06 +02:00
committed by Veljko V
parent 231b7eb2a9
commit f948f2b53a
3 changed files with 29 additions and 18 deletions

View File

@@ -13,6 +13,7 @@ use MailPoet\Listing\Handler;
use MailPoet\Models\DynamicSegment;
use MailPoet\Models\DynamicSegmentFilter;
use MailPoet\Models\Model;
use MailPoet\Segments\SegmentSubscribersRepository;
class DynamicSegmentsTest extends \MailPoetTest {
@@ -27,9 +28,13 @@ class DynamicSegmentsTest extends \MailPoetTest {
/** @var Handler */
private $listingHandler;
/** @var SegmentSubscribersRepository */
private $segmentSubscribersRepository;
public function _before() {
$this->bulkAction = ContainerWrapper::getInstance()->get(BulkActionController::class);
$this->listingHandler = ContainerWrapper::getInstance()->get(Handler::class);
$this->segmentSubscribersRepository = ContainerWrapper::getInstance()->get(SegmentSubscribersRepository::class);
}
public function testGetReturnsResponse() {
@@ -44,7 +49,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
return $dynamicSegment;
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$response = $endpoint->get(['id' => 5]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse');
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@@ -64,7 +69,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
throw new \InvalidArgumentException('segment not found');
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$response = $endpoint->get(['id' => 5]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::SEGMENT_NOT_FOUND_RESPONSE_CODE);
@@ -81,7 +86,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
})]);
$saver = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Saver', ['save' => Expected::once()]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse');
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@@ -93,7 +98,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
})]);
$saver = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Saver', ['save' => Expected::never()]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE);
@@ -112,7 +117,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
throw new ErrorSavingException('Error saving data', Model::DUPLICATE_RECORD);
})]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::SERVER_ERROR_RESPONSE_CODE);
@@ -131,7 +136,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$response = $endpoint->trash(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@@ -156,7 +161,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$response = $endpoint->restore(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@@ -184,7 +189,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$response = $endpoint->delete(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@@ -209,7 +214,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
'segment_id' => $dynamicSegment1->id,
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, null, null, null);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, null);
$response = $endpoint->bulkAction([
'action' => 'trash',
'listing' => ['group' => 'all'],