Refactor dynamic segments get endpoint to doctrine

[MAILPOET-3177]
This commit is contained in:
Rostislav Wolny
2021-03-02 16:27:08 +01:00
committed by Veljko V
parent d42752fa99
commit bd63ae508f
4 changed files with 83 additions and 45 deletions

View File

@ -8,13 +8,15 @@ use MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder;
use MailPoet\DI\ContainerWrapper;
use MailPoet\DynamicSegments\Exceptions\ErrorSavingException;
use MailPoet\DynamicSegments\Exceptions\InvalidSegmentTypeException;
use MailPoet\DynamicSegments\Filters\UserRole;
use MailPoet\Entities\DynamicSegmentFilterEntity;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Listing\BulkActionController;
use MailPoet\Listing\Handler;
use MailPoet\Models\DynamicSegment;
use MailPoet\Models\DynamicSegmentFilter;
use MailPoet\Models\Model;
use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository;
use MailPoet\Segments\SegmentsRepository;
class DynamicSegmentsTest extends \MailPoetTest {
@ -32,47 +34,37 @@ class DynamicSegmentsTest extends \MailPoetTest {
private $listingRepository;
/** @var DynamicSegmentsResponseBuilder */
private $responseBuilder;
/** @var SegmentsRepository */
private $segmentsRepository;
public function _before() {
$this->bulkAction = ContainerWrapper::getInstance()->get(BulkActionController::class);
$this->listingHandler = ContainerWrapper::getInstance()->get(Handler::class);
$this->listingRepository = ContainerWrapper::getInstance()->get(DynamicSegmentsListingRepository::class);
$this->responseBuilder = ContainerWrapper::getInstance()->get(DynamicSegmentsResponseBuilder::class);
$this->segmentsRepository = ContainerWrapper::getInstance()->get(SegmentsRepository::class);
}
public function testGetReturnsResponse() {
$loader = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader', [
'load' => function () {
$dynamicSegment = DynamicSegment::create();
$dynamicSegment->hydrate([
'name' => 's1',
'description' => '',
]);
$dynamicSegment->setFilters([new UserRole('Editor', 'or')]);
return $dynamicSegment;
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$response = $endpoint->get(['id' => 5]);
$segment = $this->createDynamicSegmentEntity('s1', '');
$endpoint = new DynamicSegments(
$this->bulkAction,
$this->listingHandler,
$this->listingRepository,
$this->responseBuilder,
$this->segmentsRepository,
null,
null,
null
);
$response = $endpoint->get(['id' => $segment->getId()]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse');
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
expect($response->data)->equals([
'id' => null,
'name' => 's1',
'description' => '',
'segmentType' => 'userRole',
'wordpressRole' => 'Editor',
'connect' => 'or',
]);
expect($response->data['id'])->equals($segment->getId());
}
public function testGetReturnsError() {
$loader = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader', [
'load' => function () {
throw new \InvalidArgumentException('segment not found');
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, null, null, null);
$response = $endpoint->get(['id' => 5]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::SEGMENT_NOT_FOUND_RESPONSE_CODE);
@ -89,7 +81,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
})]);
$saver = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Saver', ['save' => Expected::once()]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse');
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -101,7 +93,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
})]);
$saver = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Saver', ['save' => Expected::never()]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE);
@ -120,7 +112,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
throw new ErrorSavingException('Error saving data', Model::DUPLICATE_RECORD);
})]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::SERVER_ERROR_RESPONSE_CODE);
@ -139,7 +131,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, null, null, $loader);
$response = $endpoint->trash(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -165,7 +157,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, null, null, $loader);
$response = $endpoint->restore(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -194,7 +186,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $this->segmentsRepository, null, null, $loader);
$response = $endpoint->delete(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -224,6 +216,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
$this->listingHandler,
$this->listingRepository,
$this->responseBuilder,
$this->segmentsRepository,
null,
null,
null
@ -253,4 +246,17 @@ class DynamicSegmentsTest extends \MailPoetTest {
expect(DynamicSegment::count())->equals(0);
expect(DynamicSegmentFilter::findOne($filter->id))->equals(false);
}
private function createDynamicSegmentEntity(string $name, string $description): SegmentEntity {
$segment = new SegmentEntity($name, SegmentEntity::TYPE_DYNAMIC, $description);
$dynamicFilter = new DynamicSegmentFilterEntity($segment, [
'wordpressRole' => 'editor',
'segmentType' => DynamicSegmentFilterEntity::TYPE_USER_ROLE,
]);
$segment->getDynamicFilters()->add($dynamicFilter);
$this->entityManager->persist($segment);
$this->entityManager->persist($dynamicFilter);
$this->entityManager->flush();
return $segment;
}
}