From c6d284cb32c7e1132fe32b9b5c8f07bf0d4387fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Tue, 5 Jul 2022 12:55:48 +0200 Subject: [PATCH] Move lists testing to the separate class [MAILPOET-4292] --- mailpoet/tests/integration/API/MP/APITest.php | 19 +---- .../tests/integration/API/MP/SegmentsTest.php | 80 +++++++++++++++++++ .../integration/API/MP/SubscribersTest.php | 2 + 3 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 mailpoet/tests/integration/API/MP/SegmentsTest.php diff --git a/mailpoet/tests/integration/API/MP/APITest.php b/mailpoet/tests/integration/API/MP/APITest.php index 97ea9e1bbd..1acbde675e 100644 --- a/mailpoet/tests/integration/API/MP/APITest.php +++ b/mailpoet/tests/integration/API/MP/APITest.php @@ -7,6 +7,7 @@ use Codeception\Stub\Expected; use MailPoet\API\JSON\ResponseBuilders\SubscribersResponseBuilder; use MailPoet\API\MP\v1\API; use MailPoet\API\MP\v1\CustomFields; +use MailPoet\API\MP\v1\Segments; use MailPoet\API\MP\v1\Subscribers; use MailPoet\Config\Changelog; use MailPoet\CustomFields\CustomFieldsRepository; @@ -72,6 +73,7 @@ class APITest extends \MailPoetTest { return new API( $this->diContainer->get(RequiredCustomFieldValidator::class), $this->diContainer->get(CustomFields::class), + $this->diContainer->get(Segments::class), $subscriberActions, $this->diContainer->get(Changelog::class) ); @@ -103,23 +105,6 @@ class APITest extends \MailPoetTest { ); } - public function testItGetsSegments() { - $segment = $this->getSegment(); - $result = $this->getApi()->getLists(); - expect($result)->count(1); - expect($result[0]['id'])->equals($segment->getId()); - } - - public function testItExcludesWPUsersAndWooCommerceCustomersSegmentsWhenGettingSegments() { - $defaultSegment = $this->getSegment(); - $this->getSegment('WordPress', SegmentEntity::TYPE_WP_USERS); - $this->getSegment('WooCommerce', SegmentEntity::TYPE_WC_USERS); - - $result = $this->getApi()->getLists(); - expect($result)->count(1); - expect($result[0]['id'])->equals($defaultSegment->getId()); - } - public function testItRequiresEmailAddressToAddSubscriber() { try { $this->getApi()->addSubscriber([]); diff --git a/mailpoet/tests/integration/API/MP/SegmentsTest.php b/mailpoet/tests/integration/API/MP/SegmentsTest.php new file mode 100644 index 0000000000..ea76bd9d11 --- /dev/null +++ b/mailpoet/tests/integration/API/MP/SegmentsTest.php @@ -0,0 +1,80 @@ +segmentFactory = new SegmentFactory(); + } + + public function testItGetsAllDefaultSegments(): void { + $segments = [ + $this->createOrUpdateSegment('Segment 1'), + $this->createOrUpdateSegment('Segment 2'), + ]; + + $result = $this->getApi()->getLists(); + + $this->assertCount(2, $result); + foreach ($result as $key => $item) { + $this->validateResponseItem($segments[$key], $item); + } + } + + public function testItExcludesWPUsersAndWooCommerceCustomersSegmentsWhenGettingSegments(): void { + $this->createOrUpdateSegment('WordPress', SegmentEntity::TYPE_WP_USERS); + $this->createOrUpdateSegment('WooCommerce', SegmentEntity::TYPE_WC_USERS); + $defaultSegment = $this->createOrUpdateSegment('Segment 1', SegmentEntity::TYPE_DEFAULT, 'My default segment'); + + $result = $this->getApi()->getLists(); + + $this->assertCount(1, $result); + $resultItem = reset($result); + $this->validateResponseItem($defaultSegment, $resultItem); + } + + private function getApi(): API { + return new API( + $this->makeEmpty(RequiredCustomFieldValidator::class), + $this->diContainer->get(CustomFields::class), + $this->diContainer->get(Segments::class), + $this->diContainer->get(Subscribers::class), + $this->diContainer->get(Changelog::class) + ); + } + + private function validateResponseItem(SegmentEntity $segment, array $item): void { + $this->assertEquals($segment->getId(), $item['id']); + $this->assertEquals($segment->getName(), $item['name']); + $this->assertEquals($segment->getDescription(), $item['description']); + $this->assertEquals($segment->getType(), $item['type']); + $this->assertArrayHasKey('created_at', $item); + $this->assertArrayHasKey('updated_at', $item); + $this->assertNull($item['deleted_at']); + } + + private function createOrUpdateSegment(string $name, string $type = SegmentEntity::TYPE_DEFAULT, string $description = ''): SegmentEntity { + return $this->segmentFactory + ->withName($name) + ->withType($type) + ->withDescription($description) + ->create(); + } + + public function _after() { + $this->truncateEntity(SegmentEntity::class); + } +} diff --git a/mailpoet/tests/integration/API/MP/SubscribersTest.php b/mailpoet/tests/integration/API/MP/SubscribersTest.php index 4a19310fea..012f969de5 100644 --- a/mailpoet/tests/integration/API/MP/SubscribersTest.php +++ b/mailpoet/tests/integration/API/MP/SubscribersTest.php @@ -7,6 +7,7 @@ use Codeception\Stub\Expected; use MailPoet\API\JSON\ResponseBuilders\SubscribersResponseBuilder; use MailPoet\API\MP\v1\API; use MailPoet\API\MP\v1\CustomFields; +use MailPoet\API\MP\v1\Segments; use MailPoet\API\MP\v1\Subscribers; use MailPoet\Config\Changelog; use MailPoet\Entities\SegmentEntity; @@ -62,6 +63,7 @@ class SubscribersTest extends \MailPoetTest { return new API( $this->makeEmpty(RequiredCustomFieldValidator::class), $this->diContainer->get(CustomFields::class), + $this->diContainer->get(Segments::class), $subscriberActions, $this->diContainer->get(Changelog::class) );