Add tests
[PREMIUM-38]
This commit is contained in:
@ -253,6 +253,24 @@ class SegmentTest extends \MailPoetTest {
|
|||||||
expect(count($segments))->equals(1);
|
expect(count($segments))->equals(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testListingQuery() {
|
||||||
|
Segment::createOrUpdate(array(
|
||||||
|
'name' => 'name 2',
|
||||||
|
'description' => 'description 2',
|
||||||
|
'type' => 'unknown'
|
||||||
|
));
|
||||||
|
$query = Segment::listingQuery(array());
|
||||||
|
$data = $query->findMany();
|
||||||
|
expect($data)->count(1);
|
||||||
|
expect($data[0]->name)->equals('some name');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testListingQueryWithGroup() {
|
||||||
|
$query = Segment::listingQuery(array('group' => 'trash'));
|
||||||
|
$data = $query->findMany();
|
||||||
|
expect($data)->count(0);
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
\ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
\ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||||
|
10
tests/unit/Segments/FinderMock.php
Normal file
10
tests/unit/Segments/FinderMock.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
|
class FinderMock {
|
||||||
|
|
||||||
|
function getSubscriberIdsInSegment() {}
|
||||||
|
function findSubscribersInSegment() {}
|
||||||
|
|
||||||
|
}
|
148
tests/unit/Segments/SubscribersFinderTest.php
Normal file
148
tests/unit/Segments/SubscribersFinderTest.php
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Segments;
|
||||||
|
|
||||||
|
require_once('FinderMock.php');
|
||||||
|
|
||||||
|
use Codeception\Util\Stub;
|
||||||
|
use MailPoet\Models\Segment;
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Models\SubscriberSegment;
|
||||||
|
use MailPoet\WP\Hooks;
|
||||||
|
|
||||||
|
class SubscribersFinderTest extends \MailPoetTest {
|
||||||
|
|
||||||
|
function _before() {
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
||||||
|
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
|
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1', 'type' => 'default'));
|
||||||
|
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2', 'type' => 'default'));
|
||||||
|
$this->segment_3 = Segment::createOrUpdate(array('name' => 'Segment 3', 'type' => 'not default'));
|
||||||
|
$this->subscriber_1 = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'john@mailpoet.com',
|
||||||
|
'first_name' => 'John',
|
||||||
|
'last_name' => 'Doe',
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||||
|
));
|
||||||
|
$this->subscriber_2 = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'jane@mailpoet.com',
|
||||||
|
'first_name' => 'Jane',
|
||||||
|
'last_name' => 'Doe',
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||||
|
'segments' => array(
|
||||||
|
$this->segment_1->id,
|
||||||
|
),
|
||||||
|
));
|
||||||
|
$this->subscriber_3 = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'jake@mailpoet.com',
|
||||||
|
'first_name' => 'Jake',
|
||||||
|
'last_name' => 'Doe',
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED,
|
||||||
|
'segments' => array(
|
||||||
|
$this->segment_3->id,
|
||||||
|
),
|
||||||
|
));
|
||||||
|
SubscriberSegment::resubscribeToAllSegments($this->subscriber_2);
|
||||||
|
SubscriberSegment::resubscribeToAllSegments($this->subscriber_3);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetSubscribersInSegmentDefaultSegment() {
|
||||||
|
$finder = new SubscribersFinder();
|
||||||
|
$subscribers = $finder->getSubscribersByList(array(
|
||||||
|
array('id' => $this->segment_1->id, 'type' => Segment::TYPE_DEFAULT),
|
||||||
|
array('id' => $this->segment_2->id, 'type' => Segment::TYPE_DEFAULT),
|
||||||
|
));
|
||||||
|
expect($subscribers)->count(1);
|
||||||
|
expect($subscribers[$this->subscriber_2->id]['id'])->equals($this->subscriber_2->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetSubscribersNoSegment() {
|
||||||
|
$finder = new SubscribersFinder();
|
||||||
|
$subscribers = $finder->getSubscribersByList(array(
|
||||||
|
array('id' => $this->segment_1->id, 'type' => 'UNKNOWN SEGMENT'),
|
||||||
|
));
|
||||||
|
expect($subscribers)->count(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetSubscribersUsingFinder() {
|
||||||
|
$mock = Stub::makeEmpty('MailPoet\Segments\FinderMock', array('getSubscriberIdsInSegment'));
|
||||||
|
$mock
|
||||||
|
->expects($this->once())
|
||||||
|
->method('getSubscriberIdsInSegment')
|
||||||
|
->will($this->returnValue(array($this->subscriber_1)));
|
||||||
|
|
||||||
|
remove_all_filters('mailpoet_get_subscribers_in_segment_finders');
|
||||||
|
Hooks::addFilter('mailpoet_get_subscribers_in_segment_finders', function () use ($mock) {
|
||||||
|
return array($mock);
|
||||||
|
});
|
||||||
|
|
||||||
|
$finder = new SubscribersFinder();
|
||||||
|
$subscribers = $finder->getSubscribersByList(array(
|
||||||
|
array('id' => $this->segment_2->id, 'type' => ''),
|
||||||
|
));
|
||||||
|
expect($subscribers)->count(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetSubscribersUsingFinderMakesResultUnique() {
|
||||||
|
$mock = Stub::makeEmpty('MailPoet\Segments\FinderMock', array('getSubscriberIdsInSegment'));
|
||||||
|
$mock
|
||||||
|
->expects($this->exactly(2))
|
||||||
|
->method('getSubscriberIdsInSegment')
|
||||||
|
->will($this->returnValue(array($this->subscriber_1)));
|
||||||
|
|
||||||
|
remove_all_filters('mailpoet_get_subscribers_in_segment_finders');
|
||||||
|
Hooks::addFilter('mailpoet_get_subscribers_in_segment_finders', function () use ($mock) {
|
||||||
|
return array($mock);
|
||||||
|
});
|
||||||
|
|
||||||
|
$finder = new SubscribersFinder();
|
||||||
|
$subscribers = $finder->getSubscribersByList(array(
|
||||||
|
array('id' => $this->segment_2->id, 'type' => ''),
|
||||||
|
array('id' => $this->segment_2->id, 'type' => ''),
|
||||||
|
));
|
||||||
|
expect($subscribers)->count(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testFindSubscribersInSegmentInSegmentDefaultSegment() {
|
||||||
|
$finder = new SubscribersFinder();
|
||||||
|
$subscribers = $finder->findSubscribersInSegments(array($this->subscriber_2->id), array($this->segment_1->id));
|
||||||
|
expect($subscribers)->count(1);
|
||||||
|
expect($subscribers[$this->subscriber_2->id])->equals($this->subscriber_2->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testFindSubscribersInSegmentUsingFinder() {
|
||||||
|
$mock = Stub::makeEmpty('MailPoet\Segments\FinderMock', array('findSubscribersInSegment'));
|
||||||
|
$mock
|
||||||
|
->expects($this->once())
|
||||||
|
->method('findSubscribersInSegment')
|
||||||
|
->will($this->returnValue(array($this->subscriber_3)));
|
||||||
|
|
||||||
|
remove_all_filters('mailpoet_get_subscribers_in_segment_finders');
|
||||||
|
Hooks::addFilter('mailpoet_get_subscribers_in_segment_finders', function () use ($mock) {
|
||||||
|
return array($mock);
|
||||||
|
});
|
||||||
|
|
||||||
|
$finder = new SubscribersFinder();
|
||||||
|
$subscribers = $finder->findSubscribersInSegments(array($this->subscriber_3->id), array($this->segment_3->id));
|
||||||
|
expect($subscribers)->count(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testFindSubscribersInSegmentUsingFinderMakesResultUnique() {
|
||||||
|
$mock = Stub::makeEmpty('MailPoet\Segments\FinderMock', array('findSubscribersInSegment'));
|
||||||
|
$mock
|
||||||
|
->expects($this->exactly(2))
|
||||||
|
->method('findSubscribersInSegment')
|
||||||
|
->will($this->returnValue(array($this->subscriber_3)));
|
||||||
|
|
||||||
|
remove_all_filters('mailpoet_get_subscribers_in_segment_finders');
|
||||||
|
Hooks::addFilter('mailpoet_get_subscribers_in_segment_finders', function () use ($mock) {
|
||||||
|
return array($mock);
|
||||||
|
});
|
||||||
|
|
||||||
|
$finder = new SubscribersFinder();
|
||||||
|
$subscribers = $finder->findSubscribersInSegments(array($this->subscriber_3->id), array($this->segment_3->id, $this->segment_3->id));
|
||||||
|
expect($subscribers)->count(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user