1
|
|
|
<?php |
2
|
|
|
require_once 'Intraface/modules/newsletter/NewsletterSubscriber.php'; |
3
|
|
|
require_once 'NewsletterStubs.php'; |
4
|
|
|
|
5
|
|
|
class FakeObserver |
6
|
|
|
{ |
7
|
|
|
function update() |
8
|
|
|
{ |
9
|
|
|
} |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
class FakeSubscriberContact |
13
|
|
|
{ |
14
|
|
|
function getId() |
15
|
|
|
{ |
16
|
|
|
return 1000; |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
class NewsletterSubscriberTest extends PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
View Code Duplication |
function setUp() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$db = MDB2::singleton(DB_DSN); |
25
|
|
|
$db->exec('TRUNCATE newsletter_subscriber'); |
26
|
|
|
$db->exec('TRUNCATE newsletter_archieve'); |
27
|
|
|
$db->exec('TRUNCATE contact'); |
28
|
|
|
$db->exec('TRUNCATE address'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function createSubscriber() |
32
|
|
|
{ |
33
|
|
|
$list = new FakeNewsletterList(); |
34
|
|
|
$list->kernel = new Stub_Kernel; |
35
|
|
|
$list->kernel->setting->set('intranet', 'contact.login_url', 'http://localhost/'); |
36
|
|
|
return new NewsletterSubscriber($list); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function testConstructionSubscriber() |
40
|
|
|
{ |
41
|
|
|
$subscriber = $this->createSubscriber(); |
42
|
|
|
$this->assertTrue(is_object($subscriber)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function testSubscribe() |
46
|
|
|
{ |
47
|
|
|
$subscriber = $this->createSubscriber(); |
48
|
|
|
$data = array('email' => '[email protected]', 'ip' => 'ip'); |
49
|
|
|
$this->assertTrue($subscriber->subscribe($data)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function testUnSubscribe() |
53
|
|
|
{ |
54
|
|
|
$subscriber = $this->createSubscriber(); |
55
|
|
|
$this->assertTrue($subscriber->unsubscribe('[email protected]')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function testOptin() |
59
|
|
|
{ |
60
|
|
|
$subscriber = $this->createSubscriber(); |
61
|
|
|
$data = array('email' => '[email protected]', 'ip' => 'ip'); |
62
|
|
|
$this->assertTrue($subscriber->subscribe($data)); |
|
|
|
|
63
|
|
|
$code = 'wrongcode'; |
64
|
|
|
$ip = 'ip'; |
65
|
|
|
|
66
|
|
|
$this->assertFalse($subscriber->optIn($code, $ip)); |
67
|
|
|
$code = $subscriber->get('code'); |
68
|
|
|
$this->assertTrue($subscriber->optIn($code, $ip)); |
69
|
|
|
$this->assertTrue($subscriber->optedIn()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
function testAddObserver() |
73
|
|
|
{ |
74
|
|
|
$subscriber = new NewsletterSubscriber(new FakeNewsletterList); |
75
|
|
|
$subscriber->addObserver(new FakeObserver); |
76
|
|
|
$this->assertEquals(1, count($subscriber->getObservers())); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function testAddContactReturnsInteger() |
80
|
|
|
{ |
81
|
|
|
$subscriber = new NewsletterSubscriber(new FakeNewsletterList); |
82
|
|
|
$this->assertTrue($subscriber->addContact(new FakeSubscriberContact) > 0); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
function testGetContactReturnsContactObject() |
86
|
|
|
{ |
87
|
|
|
$subscriber = new NewsletterSubscriber(new FakeNewsletterList); |
88
|
|
|
$contact = new FakeSubscriberContact; |
89
|
|
|
$subscriber->addContact($contact); |
90
|
|
|
$this->assertTrue(is_object($subscriber->getContact($contact->getId()))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function testDeleteSubscriber() |
94
|
|
|
{ |
95
|
|
|
$subscriber = new NewsletterSubscriber(new FakeNewsletterList); |
96
|
|
|
$this->assertTrue($subscriber->delete()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function testGetListReturnsArray() |
100
|
|
|
{ |
101
|
|
|
$subscriber = new NewsletterSubscriber(new FakeNewsletterList); |
102
|
|
|
$this->assertTrue(is_array($subscriber->getList())); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
function testGetListReturnsActiveOptedInSubscribers() |
106
|
|
|
{ |
107
|
|
|
$subscriber = $this->createSubscriber(); |
108
|
|
|
$subscriber->subscribe(array('name' => 'test1', 'email' => '[email protected]', 'ip' => '0.0.0.0')); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$subscriber = $this->createSubscriber(); |
111
|
|
|
$subscriber->subscribe(array('name' => 'test2', 'email' => '[email protected]', 'ip' => '0.0.0.0')); |
|
|
|
|
112
|
|
|
$subscriber->optin($subscriber->get('code'), '0.0.0.0'); |
113
|
|
|
|
114
|
|
|
$subscriber = $this->createSubscriber(); |
115
|
|
|
$subscriber->subscribe(array('name' => 'test3', 'email' => '[email protected]', 'ip' => '0.0.0.0')); |
|
|
|
|
116
|
|
|
$subscriber->optin($subscriber->get('code'), '0.0.0.0'); |
117
|
|
|
$subscriber->delete(); |
118
|
|
|
|
119
|
|
|
$list = $subscriber->getList(); |
120
|
|
|
|
121
|
|
|
$this->assertEquals(1, count($list)); |
122
|
|
|
$this->assertEquals('[email protected]', $list[0]['contact_email']); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.